Solved Item cooldown not working

Discussion in 'Plugin Development' started by BB_Blocks, Oct 12, 2020.

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

    BB_Blocks

    I have this so far.
    Code:
    HashMap<UUID, Double> cooldown = new HashMap<UUID, Double>();
    
    
    @EventHandler
        public void onGunShoot(PlayerInteractEvent e)
        {
            Player p = e.getPlayer();
          
            ItemStack gun = p.getInventory().getItemInMainHand();
           
           
    if(gun.getType().equals(Material.IRON_PICKAXE) && gun.getItemMeta().getDisplayName().equals("AK-47") && e.getAction().equals(Action.RIGHT_CLICK_AIR))
            {
                double cooldownTime = 0.5;
              
                if(cooldown.containsKey(p.getUniqueId()))
                {
                    double secondsLeft = ((cooldown.get(p.getUniqueId()) / 1000) + cooldownTime) - (System.currentTimeMillis() / 1000);
                  
                    if(secondsLeft > 0)
                    {
                        e.setCancelled(true);
                        p.sendMessage(secondsLeft + "");
                    }
                    else
                    {
                        Snowball s = (Snowball) p.launchProjectile(Snowball.class);
                        s.setVelocity(p.getLocation().getDirection().multiply(3.50));
                      
                        cooldown.put(p.getUniqueId(), (double) System.currentTimeMillis());
                    }
                }
                else
                {
                    Snowball s = (Snowball) p.launchProjectile(Snowball.class);
                    s.setVelocity(p.getLocation().getDirection().multiply(3.50));
    
                    cooldown.put(p.getUniqueId(), (double) System.currentTimeMillis());
                }
    }
    
    It works and shoots but it shoots like every 2-3 seconds instead of the wanted 0.5 seconds.
    I've tried several things and can't get it so I was hoping one of you can.
     
    Last edited: Oct 12, 2020
  2. Offline

    Strahan

    You are way overcomplicating this. Also you aren't checking if the item has meta; if they swing a regular iron pick it'll crash the plugin. I'd do:
    Code:
    Map<UUID, Long> cooldown = new HashMap<>();
    
    @EventHandler
    public void onGunShoot(PlayerInteractEvent e) {
      Player p = e.getPlayer();
      ItemStack gun = p.getInventory().getItemInHand();
      if (!gun.hasItemMeta() || !gun.getItemMeta().hasDisplayName()) return;
      if (!gun.getItemMeta().getDisplayName().equalsIgnoreCase("ak-47")) return;
      if (cooldown.containsKey(p.getUniqueId()) && cooldown.get(p.getUniqueId()) > System.currentTimeMillis()) return;
    
      Snowball s = (Snowball) p.launchProjectile(Snowball.class);
      s.setVelocity(p.getLocation().getDirection().multiply(3.50));
      cooldown.put(p.getUniqueId(), System.currentTimeMillis() + [plugin instance].getConfig().getInt("cooldowns.ak47", 500));
    }
    Don't forget to clean your map at player quit. I'd also not hard code the half second limit, I'd read it from config as done above so you don't have to recompile if you change your mind later.
     
  3. Offline

    BB_Blocks

    I still have yet to learn how to use config files but thank you for the help. It works perfectly now :D
     
    Last edited: Oct 12, 2020
    Strahan likes this.
Thread Status:
Not open for further replies.

Share This Page