Solved Event Cooldowns

Discussion in 'Plugin Development' started by racoonsru1e, Dec 14, 2013.

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

    racoonsru1e

    I am struggling to work out how to add a cooldown to this event using an arraylist. I have probably messed everything up but here is the code:

    Code:
    private ArrayList<String> cooldown = new ArrayList<String>();
        @EventHandler (priority = EventPriority. HIGH)
        public void onPlayerStomp(final PlayerInteractEvent event){
          if(cooldown.contains(event.getPlayer().getName())) { event.setCancelled(true); }
          Player player = event.getPlayer();
          if(player.getInventory().getBoots().getType() == Material.LEATHER_BOOTS){
                if(player.getInventory().getLeggings().getType() == Material.LEATHER_LEGGINGS){
                    if(player.getInventory().getChestplate().getType() == Material.LEATHER_CHESTPLATE){
                        if(player.getInventory().getHelmet().getType() == Material.LEATHER_HELMET){
              if (event.getAction() == Action. RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK ){
              player.sendMessage(ChatColor.BLUE + "You used" + ChatColor.GREEN + " Leap!" );
              player.setVelocity(player.getLocation().getDirection().multiply(1.7));
              player.getWorld().playSound(player.getLocation(), Sound.FIZZ, 1.0F, 17.0F);
              cooldown.add(event.getPlayer().getName());
              Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
                  public void run(){
                  cooldown.remove(event.getPlayer().getName());
                  }}, 10*20);
                              }
                        }
                    }     
                }
              }
        }
    It is basically just a leap skill when you are wearing Leather Armor. Everything in it works except the cooldown.

    Hope someone can help :)
     
  2. Offline

    felixfritz

    This should technically work. Is it just that the cooldown isn't working at all? Or the names are not being removed? What happens when you try to print out the contents of the cooldown-list inside and outside the sync delayed task?
     
  3. Offline

    racoonsru1e

    felixfritz The cooldown does not work at all. It just keeps activating the skill. I changed it to this and it sent the message when the cooldown was over, but it didnt not stop me from using the event.
    Code:
    private ArrayList<String> cooldown = new ArrayList<String>();
        public static Main plugin = Main.plugin;
        @EventHandler
        public void onPlayerStomp(final PlayerInteractEvent event){
          if(cooldown.contains(event.getPlayer().getName())) { event.setCancelled(true); }
          final Player player = event.getPlayer();
          if(player.getInventory().getBoots().getType() == Material.LEATHER_BOOTS){
                if(player.getInventory().getLeggings().getType() == Material.LEATHER_LEGGINGS){
                    if(player.getInventory().getChestplate().getType() == Material.LEATHER_CHESTPLATE){
                        if(player.getInventory().getHelmet().getType() == Material.LEATHER_HELMET){
              if (event.getAction() == Action. RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK ){
              player.sendMessage(ChatColor.BLUE + "You used" + ChatColor.GREEN + " Leap!" );
              player.setVelocity(player.getLocation().getDirection().multiply(1.7));
              player.getWorld().playSound(player.getLocation(), Sound.FIZZ, 1.0F, 17.0F);
              cooldown.add(event.getPlayer().getName());
            getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                  public void run(){
                  player.sendMessage("You may now use Leap!");
                  cooldown.remove(event.getPlayer().getName());
                  }
                  }, 120);
                              }
                        }
                    }       
                }
              }
        }
     
  4. Offline

    felixfritz

    Could you print out the contents of the ArrayList after adding, before removing and after removing the name?
    Code:java
    1. for(String s : cooldown) System.out.println(s);
     
  5. Offline

    racoonsru1e

    It didn't change anything.
    Code:
    cooldown.add(event.getPlayer().getName());
              for(String s : cooldown) System.out.println(s);
            getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                  public void run(){
                  player.sendMessage("You may now use Leap!");
                  for(String s : cooldown) System.out.println(s);
                  cooldown.remove(event.getPlayer().getName());
                  for(String s : cooldown) System.out.println(s);
     
  6. Offline

    felixfritz

    So it didn't even add the name to the list?
     
  7. Offline

    racoonsru1e

    Well it started saying my name in console
     
  8. Offline

    felixfritz

    Oh gosh, I'm an idiot, sorry. When you cancel the event that doesn't mean that it "leaves" the method, which means it cancels the event, but still goes through the whole set up you've set up. So you could just add a return; right after cancelling the event.
     
  9. Offline

    racoonsru1e

    I didn't cancel the event?
    Where would I put the return?
     
  10. Offline

    felixfritz

    You DID cancel the event, but just cancelling doesn't mean that it cancels everything else you're trying to do. It's just another simple method. So if you just add return; right after event.setCancelled(true); you should be fine.
     
    areqpl likes this.
  11. Offline

    racoonsru1e

    Thank you so much! Problem solved :D
     
    felixfritz likes this.
Thread Status:
Not open for further replies.

Share This Page