Development Assistance How to set a cooldown delay

Discussion in 'Plugin Help/Development/Requests' started by xTheGamerPlayz, Apr 12, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    xTheGamerPlayz

    I have looked around and found a few solutions to this. But I am too new to programming to make any sense of it. I know I need an ArrayList, but I don't know how to make the timer countdown and be able to tell them the amount of cooldown time that is left.

    Code:
    package com.thegamerplayz.mmoclasses;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    
    public class PlayerListener implements Listener {
       
        private List<Player> cd = new ArrayList<Player>();
       
        public PlayerListener(main plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
       
        @SuppressWarnings("deprecation")
        @EventHandler
         public void onInteract(PlayerInteractEvent e)
         {
          Player player = e.getPlayer();
         
            if (player.getInventory().getItemInHand().getType() == Material.BLAZE_ROD)
           {
             if ((e.getAction() == Action.RIGHT_CLICK_AIR) || (e.getAction() == Action.RIGHT_CLICK_BLOCK))
               {
               if (!cd.contains(player)){
                   double Phealth = player.getHealth() + 5;
                   if(Phealth > 20){
                       Phealth = 20;
                   player.setHealth(Phealth);
                   cd.add(player);
                   }
               }
               }
           }
         }
    }
    
    This is about as much as I could get before I was too confused to continue. If anyone knows a solution to this could they please explain it to me?
     
  2. Offline

    Lukiwoki

    This might do it.

    CODE (open)

    Code:
    package com.thegamerplayz.mmoclasses;
    import java.util.ArrayList;
    import java.util.List;
    
    import net.md_5.bungee.api.ChatColor;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    public class PlayerListener implements Listener {
      
      
        private List<Player> cd = new ArrayList<Player>();
        public PlayerListener(main plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
        @SuppressWarnings("deprecation")
        @EventHandler
         public void onInteract(PlayerInteractEvent e)
         {
          Player player = e.getPlayer();
     
            if (player.getInventory().getItemInHand().getType() == Material.BLAZE_ROD)
           {
             if ((e.getAction() == Action.RIGHT_CLICK_AIR) || (e.getAction() == Action.RIGHT_CLICK_BLOCK))
               {
               if (!cd.contains(player)){
                   double Phealth = player.getHealth() + 5;
                   if(Phealth > 20){
                       Phealth = 20;
                   player.setHealth(Phealth);
                   cd.add(player);
                   }
               }else
               {
                   player.sendMessage(ChatColor.RED + "This ability is currently on cooldown!");
               }
               }
           }
         }
        public void cooldownheal(final Player player)
        {
            Bukkit.getScheduler().scheduleSyncDelayedTask(main, new Runnable()
            {
                @Override
                public void run() {
                    cd.remove(player.getName());
                    player.sendMessage(""+ChatColor.DARK_AQUA + ChatColor.BOLD + "Your healing ability has recharged!");
                }
            }, 12*20); //12 sec cooldown. 20 ticks = 1 sec.
        }
    }
    


    I'm not 100% sure, but I believe this is going to work.

    It should do:
    There'll be a 12 second cooldown on the healing wand ability thingy, if you try to use it while it's on cooldown, a red message displaying "This ability is currently on cooldown!" will appear. When your ability has recharged, you will get the message "Your healing ability has recharged"
     
  3. It won't work because you forgot to trigger "cooldownheal". To trigger it, put 'cooldownheal(player);' under 'cd.add(player);', like so:

    if (!cd.contains(player)){
    double Phealth = player.getHealth() + 5;
    if(Phealth > 20){
    Phealth = 20;
    player.setHealth(Phealth);
    cd.add(player);
    cooldownheal(player);
    }
     
    Lukiwoki likes this.
Thread Status:
Not open for further replies.

Share This Page