Help with cooldown - CombatCrits

Discussion in 'Plugin Development' started by GizmoRay, Jan 7, 2013.

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

    GizmoRay

    Hello Bukkiteers,
    I'm in the process of updating a plugin of mine, CombatCrits, however I've run into a slight delay with the cooldown (no pun intended) section of the plugin. I've attempted to use several methods to delay the action from being used repeatedly, but nothing will work. What I'm looking for is a simple method to add a cooldown to an action, thus limiting spamming of the aforementioned action.

    Example:
    Code:
    public void onPlayerAnimate(PlayerAnimationEvent event) {
            Player p = event.getPlayer();
     
              if(event.getPlayer().hasPermission("combatcrits.dash") || p.isOp()) {
                  if(p.getItemInHand().getTypeId() == 34 && getTargets(p) instanceof LivingEntity && getTarget(p) instanceof Player){
    //                  Location loc = p.getLocation();
                      if(getTarget(p).getLocation().getX()-p.getLocation().getX() <= 10 || p.getLocation().getBlockZ()-getTarget(p).getLocation().getBlockZ() <= 10) {
                      ((Player) getTarget(p)).addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 100, 1000));
                      ((Player) getTarget(p)).addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 20, 100));
                      ((Player) getTarget(p)).addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 50, 2));
                      p.teleport(getTargets(p).getLocation());
                      ((Player) getTarget(p)).damage(5);
                      p.sendMessage(ChatColor.BLUE + "Dashed to " +((Player) getTarget(p)).getName());
                      ((Player) getTarget(p)).sendMessage(ChatColor.AQUA + p.getName() + " dashed to you!");
    //Delay for 4 seconds after this has happened, preventing the player from spamming the action.
                      }
                  }
              }
            }
    Thanks for any help you can provide! <3
     
  2. Offline

    fireblast709

    HashMap<String, Long>. In this HashMap, you store the current System.currentTimeMillis() + delay in milliseconds. Then, you need to check
    1. if the player is in the HashMap (if he/she already used that)
    2. if the retrieved value is smaller than the current System.currentTimeMillis()
     
  3. Offline

    GizmoRay

    Perfect! Thank you! <3
     
Thread Status:
Not open for further replies.

Share This Page