Solved Most efficient cooldown method?

Discussion in 'Plugin Development' started by racoonsru1e, Jan 3, 2015.

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

    racoonsru1e

    Hi. I am making a plugin where players may use certain abilities whilst having a certain armor type equipped. I am currently use Array Lists to do this but I was wondering if there was a more effective way for making cooldowns (I have had some problems with the array lists). I will paste one ability's code below:

    Code:
           private ArrayList<String> slam = new ArrayList<String>();
           @EventHandler
           public void onPlayerInteract2(final PlayerInteractEvent event){
               if(slam.contains(event.getPlayer().getName())){ event.setCancelled(true);; return;}
               final Player player = event.getPlayer();
               if(player.getInventory().getBoots().getType() == Material.DIAMOND_BOOTS){
                   if(player.getInventory().getLeggings().getType() == Material.DIAMOND_LEGGINGS){
                       if(player.getInventory().getChestplate().getType() == Material.DIAMOND_CHESTPLATE){
                           if(player.getInventory().getHelmet().getType() == Material.DIAMOND_HELMET){
                               if(player.getItemInHand().getType() == Material.DIAMOND_AXE){
                                   if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
                                       player.sendMessage(ChatColor.BLUE + "You used: " + ChatColor.GREEN + "Slam!");
                                       player.getWorld().playSound(player.getLocation(), Sound.IRONGOLEM_THROW, 1.0F, 17.0F);
                                       for(Entity e : player.getNearbyEntities(5, 2, 5)){
                                           if(e instanceof Entity || e instanceof Player){
                                               e.setVelocity(new Vector(0,1.2,0));
                                               slam.add(player.getName());
                                           }
                                       }
                                       getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                                            public void run(){
                                               player.sendMessage(ChatColor.GREEN + "You may now use Slam!");
                                                 slam.remove(event.getPlayer().getName());
                                            }
                                       }, 600);         
                                   }
                               }
                           }
                       }
                   }
               }
           }
    This does work but it cancels the event, which, as a consequence, disables the player from interacting with blocks until they are removed from the list:

    Code:
    if(slam.contains(event.getPlayer().getName())){ event.setCancelled(true);; return;}
    Is there a way that I can get around this or should I use a better method?
     
  2. Offline

    Skionz

    @racoonsru1e Don't use a scheduler, just use a Map with your player instance and the current time plus a few seconds. Make sure to clean up your map when necessary though.
     
  3. Offline

    racoonsru1e

    I have heard about that but I don't know how to do it. Can you show me?
     
  4. Offline

    Skionz

    @racoonsru1e Create a Map. Use System.currentTimeMillis
     
  5. Offline

    racoonsru1e

    I have never used HashMaps before. I have no idea what that means.

    EDIT: Found a video which showed me how to do it. It worked. Thanks for your help otherwise :D
     
  6. Offline

    Skionz

Thread Status:
Not open for further replies.

Share This Page