Cooldown doesnt work plz help

Discussion in 'Spigot Plugin Development' started by Overwact77, Jun 11, 2018.

  1. my code:
    Code (Java):
    public class vainch implements Listener {
        private HashMap<UUID, Long> cooldown = new HashMap<UUID, Long>();
        private int cooldowntime = 7;
         
        @EventHandler
         public void onPlayerToggleSneakEvent(PlayerToggleSneakEvent event) {
         Player player = event.getPlayer();
              if(player.isSneaking()) {
                  org.bukkit.inventory.ItemStack Boots = player.getInventory().getBoots();
                    if (Boots  != null)
                    {
                      if (Boots.getType() == Material.IRON_BOOTS)
                      {
                          if (cooldown.containsKey(player.getUniqueId())) {
                                Long seconedsleft = ((cooldown.get(player.getUniqueId()) / 1000) + cooldowntime) - (System.currentTimeMillis() / 1000);
                                if(seconedsleft > 0) {
                                    player.sendMessage(ChatColor.DARK_RED + "you can do this in" + seconedsleft + "sec");
                                }
                                }else {
                              player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 120, 10), true);
                                PacketPlayOutEntityEquipment handPacket = new PacketPlayOutEntityEquipment(player.getEntityId(), 0, null);
                                PacketPlayOutEntityEquipment helmetPacket = new PacketPlayOutEntityEquipment(player.getEntityId(), 1, null);
                                PacketPlayOutEntityEquipment chestPacket = new PacketPlayOutEntityEquipment(player.getEntityId(), 2, null);
                                PacketPlayOutEntityEquipment legPacket = new PacketPlayOutEntityEquipment(player.getEntityId(), 3, null);
                                PacketPlayOutEntityEquipment bootsPacket = new PacketPlayOutEntityEquipment(player.getEntityId(), 4, null);
                                for(org.bukkit.entity.Entity ent : player.getNearbyEntities(10, 10, 10)) {
                                    if(!(ent instanceof Player) || ent == player) continue;
                                    Player reciever = (Player) ent;
                                    ((CraftPlayer)reciever).getHandle().playerConnection.sendPacket(handPacket);
                                    ((CraftPlayer)reciever).getHandle().playerConnection.sendPacket(helmetPacket);
                                    ((CraftPlayer)reciever).getHandle().playerConnection.sendPacket(chestPacket);
                                    ((CraftPlayer)reciever).getHandle().playerConnection.sendPacket(legPacket);
                                    ((CraftPlayer)reciever).getHandle().playerConnection.sendPacket(bootsPacket);
                               }
                            }
                     
                       
                       
                                 
                                 
                                 
                       
     
  2. What is it doing? Any errors? Did you register your event handler? Tried adding debug logs to see how far your logic is getting?
     
  3. at this point in your code:
    Code (Java):
    if(!(ent instanceof Player) || ent == player) continue;
    you check if the entity is not an instance of a player. It may be more effective to instead do
    Code (Java):

    if(ent instanceof Player) {
           Player reciever = (Player) ent;
           ((CraftPlayer)reciever).getHandle().playerConnection.sendPacket(handPacket);
           ((CraftPlayer)reciever).getHandle().playerConnection.sendPacket(helmetPacket);
           ((CraftPlayer)reciever).getHandle().playerConnection.sendPacket(chestPacket);
           ((CraftPlayer)reciever).getHandle().playerConnection.sendPacket(legPacket);
           ((CraftPlayer)reciever).getHandle().playerConnection.sendPacket(bootsPacket);
    }
     
    As checking the opposite with a continue has the same effect, and check
    Code (Java):
    ent.equals(player)
    instead of
    Code (Java):
    ent == player
    , as this will use the entity's own specific method for detecting if it is the same entity or not instead of relying on the memory location of the objects (very unreliable for objects).
    Also, you don't appear to have any place in your code where you RESET your cooldown. You should set this at the end of your packet sending.

    As a side note, you may find it more convenient to work with the LocalTime / LocalDateTime objects.
     
  4. It seems that you never add a player to your "cooldown" Map.
    Also, you are never removing them either; meaning once they're added to this map, they will never be able to use this feature again, because of how your code is structured:
    You check if a player is in the map
    if this is the case, telll them how long until their cooldown yet is and end the method
    otherwise (they are not in the list) give them desired effects

    I hope the fix is obvious enough.
     
    • Agree Agree x 1
  5. you should put at the end:
    Code (Java):
    cooldown.put(player.getUniqueId(), System.currentTimeMillis());
     
    #5 TheGaming999, Jun 11, 2018
    Last edited: Jun 11, 2018
    • Agree Agree x 1