Cooldown timer not working

Discussion in 'Plugin Development' started by glory_fades, Jan 30, 2015.

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

    glory_fades

    so im trying to make a cooldown timer that will display the seconds you have to wait before using the ability again.

    Code:
     Random r;
    HashMap<Player, Integer> cooldownTime;
    HashMap<Player, BukkitRunnable> cooldownTask;
    @EventHandler
    public void onBlink(PlayerInteractEvent e)
    {
        final Player p = e.getPlayer();
         if ((p.getItemInHand().getType() == Material.NETHER_STAR) &&
                  (e.getAction().name().contains("RIGHT")))
                {
             if (KitHandler.getKit(p.getName()) != KitType.BLINK) {
                  return;
                }
                if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK)
                {
                    if (cooldownTime.containsKey(p))
                    {
                         p.sendMessage(ChatColor.RED + "You must wait for " + cooldownTime.get(p) + " seconds.");
                         return;
                  }
                p.setVelocity(p.getEyeLocation().getDirection().multiply(2.5D));
                p.playSound(p.getLocation(), Sound.FIREWORK_BLAST2, 1, 1);
                cooldownTime.put(p, 5);
                  cooldownTask.put(p, new BukkitRunnable() {
                          public void run()
                          {
                                  cooldownTime.put(p, cooldownTime.get(p) - 1);
                                  if (cooldownTime.get(p) == 0) {
                                          cooldownTime.remove(p);
                                          cooldownTask.remove(p);
                                          cancel();
                                  }
                          }
                  });
                
                  cooldownTask.get(p).runTaskTimer(Main.get(), 20, 20);
                }
                }
    }
    }
     
  2. Offline

    Skionz

  3. Offline

    glory_fades

    @Skionz the ability plus the cooldown deosnt work, the ability worked before
     
  4. Offline

    Skionz

    @glory_fades You are doing a lot more then you need to. Just store the Player and the current time in milliseconds as a Long in a Map. When the player right clicks check if the time stored in the Map is >= the current time. Then put the Player in the Map and add your cool down time to the current time. I'm sure you can figure out the rest.
     
  5. Offline

    Sheepii

    @gloryfades

    You could also

    1. Check if they're on the hashmap
    Code:
     if(cooldowns.containsKey(player.getName()){ 
    2. If they are not, put them on the hashmap with a final int variable of the cd
    Code:
     final int blinkcooldown = 25; 
    3. Each iteration, you could iterate through the keys, get values and then do something like this

    Code:
           
            HashMap<String, Integer> cooldowns = new HashMap<String, Integer>();
    
            for (String current : cooldowns.keySet()) {
                cooldowns.put(current, cooldowns.get(current) - 1);
                if(cooldowns.get(current).equals(0){
                    cooldowns.remove(current);
                }
            }
    
    This would avoid using multiple bukkit runnables. Just have one runnable, to catch the cooldown globally
     
  6. Offline

    Jomens235

    I used HashMap<String, Long> when I was making a cooldown, don't know if that'll help you in any way.
     
Thread Status:
Not open for further replies.

Share This Page