Cooldown wont remove player after 10s and goes to - seconds

Discussion in 'Plugin Development' started by Areoace, Mar 31, 2013.

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

    Areoace

    Code:
    package me.areoace.Survival;
     
    import java.util.HashMap;
     
    import org.bukkit.ChatColor;
    import org.bukkit.Effect;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    public class GuardianRage implements Listener{
     
        public static Main plugin;
       
     
        public GuardianRage(Main instance) {
            plugin = instance;
        }
       
        public HashMap<String, Long> cooldown = new HashMap<String, Long>();
     
        public int cooldownTime = 10;
     
        @EventHandler
            public void onPlayerInteract(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            if(ScanDiamond.isDiamondSet(player.getEquipment())){
                if (cooldown.containsKey(player.getName())) {
                    long diff = (System.currentTimeMillis() - cooldown.get(player.getName())) / 1000;
                          player.sendMessage(ChatColor.RED + "[" + ChatColor.YELLOW + "Cooldown" + ChatColor.RED + "]" + ChatColor.GREEN + "Guardians rage is on a " + ChatColor.LIGHT_PURPLE + (cooldownTime - diff) + "s"+ ChatColor.GREEN +" cooldown!");
                }else{
                Long next = cooldown.get(player.getName());
                if(next == null || next < System.currentTimeMillis()){ 
              if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
            if(player.getItemInHand().getType().getId() == 279 || player.getItemInHand().getType().getId() == 258 || player.getItemInHand().getType().getId() == 286 || player.getItemInHand().getType().getId() == 275|| player.getItemInHand().getType().getId() == 271){
                cooldown.put(player.getName(), System.currentTimeMillis() + cooldownTime);
                                player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE,
                                        120, 1));
                                player.addPotionEffect(new PotionEffect(PotionEffectType.WITHER,
                                        180, 1));
                                player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION,
                                        220, 1));
                                player.sendMessage(ChatColor.RED + "[" + ChatColor.YELLOW + "Ability" + ChatColor.RED + "]" + ChatColor.DARK_AQUA + " You have activated" +ChatColor.BOLD + " Guardians Rage!"+ ChatColor.RESET + ChatColor.DARK_PURPLE + " 15s cooldown!");
                                Sound sound = Sound.ENDERMAN_HIT;
                                player.getWorld().playEffect(player.getLocation(), Effect.STEP_SOUND,Material.DIAMOND_ORE.getId());
                                player.getWorld().playSound(player.getPlayer().getLocation(), sound, 1, 1);                                         
     
                }
                }
                }
            }
            }
        }                         
                   
        public void enabled(boolean b) {
            // TODO Auto-generated method stub
       
        }
    }
     
  2. Offline

    Tirelessly

    Yeah, because you don't tell it to remove after 10 seconds. You just say "If their name is in the map send them the time, otherwise give them stuff and add them to the map"
     
  3. Offline

    Areoace

    oops, gimme a sec, ill fix it up a bit.

    I have gotten help from this: http://forums.bukkit.org/threads/cooldowns.131042/#post-1560864
    But whenever I add my code it crashes

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  4. Offline

    Tirelessly

  5. Offline

    Areoace

    How do you do it? I mean it works with basic code, but with mine it crashes

    Code:
          package me.areoace.Survival;
           
            import java.util.HashMap;
           
            import org.bukkit.ChatColor;
    import org.bukkit.Effect;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
           
            public class GuardianRage implements Listener{
           
                public static Main plugin;
             
           
                public GuardianRage(Main instance) {
                    plugin = instance;
                }
             
                public HashMap<String, Long> cooldown = new HashMap<String, Long>();
           
                public int cooldownTime = 10;                   
     
                @EventHandler
     
                public void onPlayerLeftClick(PlayerInteractEvent event) {
                Player player = event.getPlayer();
                Action action = event.getAction();       
                if ((action == Action.RIGHT_CLICK_BLOCK) || (action == Action.RIGHT_CLICK_AIR)) {                         
                if (cooldown.containsKey(player.getName())) {
                long diff = (System.currentTimeMillis() - cooldown.get(player.getName())) / 1000;
                if (diff < cooldownTime) {
                    player.sendMessage(ChatColor.RED + "[" + ChatColor.YELLOW + "Cooldown" + ChatColor.RED + "]" + ChatColor.GREEN + "Guardians rage is on a " + ChatColor.LIGHT_PURPLE + (cooldownTime - diff) + "s"+ ChatColor.GREEN +" cooldown!");
              event.setCancelled(true);
                return;
                }
                    if(player.getItemInHand().getType().getId() == 279 || player.getItemInHand().getType().getId() == 258 || player.getItemInHand().getType().getId() == 286 || player.getItemInHand().getType().getId() == 275|| player.getItemInHand().getType().getId() == 271){
                        cooldown.put(player.getName(), System.currentTimeMillis() + cooldownTime);
                                        player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE,
                                                120, 1));
                                        player.addPotionEffect(new PotionEffect(PotionEffectType.WITHER,
                                                180, 1));
                                        player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION,
                                                220, 1));
                                        player.sendMessage(ChatColor.RED + "[" + ChatColor.YELLOW + "Ability" + ChatColor.RED + "] " + ChatColor.DARK_AQUA + " You have activated" +ChatColor.BOLD + " Guardians Rage!"+ ChatColor.RESET + ChatColor.DARK_PURPLE + " 15s cooldown!");
                                        Sound sound = Sound.ENDERMAN_HIT;
                                        player.getWorld().playEffect(player.getLocation(), Effect.STEP_SOUND,Material.DIAMOND_ORE.getId());
                                        player.getWorld().playSound(player.getPlayer().getLocation(), sound, 1, 1);                               
                cooldown.put(player.getName(), System.currentTimeMillis());
                }
                cooldown.put(player.getName(), System.currentTimeMillis());           
                }
                }
                }
               
     
     
                         
                public void enabled(boolean b) {
                    // TODO Auto-generated method stub
             
    }
            }
     
  6. Offline

    ZeusAllMighty11

    This is a mess, what is why
     
    microgeek likes this.
  7. Offline

    Areoace

    Sorry, I got a headache and am not feeling well today. I just want to get them working asap rather than waiting forever. I might of missed something, but im not sure what.

    EDIT: Error for updated code

    2013-04-01 15:39:30 [INFO] Done (2.736s)! For help, type "help" or "?"
    2013-04-01 15:39:31 [INFO] areoace logged in with entity id 251 at ([world] 496.77937489838945, 69.0, 356.4773393012179)
    2013-04-01 15:39:33 [WARNING] [WorldEdit] No compatible nms block class found.
    2013-04-01 15:39:33 [SEVERE] Could not pass event PlayerInteractEvent to AMod v1.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    at org.bukkit.craftbukkit.v1_5_R2.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:181)
    at org.bukkit.craftbukkit.v1_5_R2.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:151)
    at net.minecraft.server.v1_5_R2.PlayerConnection.a(PlayerConnection.java:615)
    at net.minecraft.server.v1_5_R2.Packet15Place.handle(SourceFile:58)
    at net.minecraft.server.v1_5_R2.NetworkManager.b(NetworkManager.java:292)
    at net.minecraft.server.v1_5_R2.PlayerConnection.d(PlayerConnection.java:113)
    at net.minecraft.server.v1_5_R2.ServerConnection.b(SourceFile:35)
    at net.minecraft.server.v1_5_R2.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.v1_5_R2.MinecraftServer.r(MinecraftServer.java:580)
    at net.minecraft.server.v1_5_R2.DedicatedServer.r(DedicatedServer.java:225)
    at net.minecraft.server.v1_5_R2.MinecraftServer.q(MinecraftServer.java:476)
    at net.minecraft.server.v1_5_R2.MinecraftServer.run(MinecraftServer.java:409)
    at net.minecraft.server.v1_5_R2.ThreadServerApplication.run(SourceFile:573)
    Caused by: java.lang.NullPointerException
    at me.areoace.Survival.NoEnchantT.onEntityDamageByEntityEvent(NoEnchantT.java:23)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    ... 16 more

    Anyone?...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  8. Offline

    KoffiePatje

    Perhaps it is because you ask the system time in milliseconds and use the cooldown time as seconds...?

    Code:
    cooldown.put(player.getName(), System.currentTimeMillis() + cooldownTime);
    
    My guess is that it should be something like this, correct me if i'm wrong ;)

    Code:
    cooldown.put(player.getName(), System.currentTimeMillis() + ( cooldownTime * 1000 ) );
    
     
  9. Offline

    Areoace

    No =( still not working. I don't know where I've gone wrong.
     
  10. Offline

    microgeek

    Half of the kids on this forum code a mess, it's a shame.
     
  11. Offline

    Areoace

    Half the people on this forum are dicks, it's a shame.

    So are you here to help or not.

    Don't worry got it to work. Thanks for the help...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  12. Offline

    ZeusAllMighty11

    Areoace

    You won't take 5 minutes to clean up your code, so that's your problem.
     
  13. Offline

    microgeek

    I'm here to help people that deserve help.
     
  14. Offline

    Areoace

    Your right I didn't take 5m, I took over 2 hours.
     
Thread Status:
Not open for further replies.

Share This Page