Cooldowns?

Discussion in 'Plugin Development' started by Acer_Mortem, Jan 2, 2013.

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

    Acer_Mortem

    I have a plugin in which a player left-clicks with an emerald and an explosion occurs at the targeted block. Is there anyway I can add a cooldown until the effect can be used again?
     
  2. Offline

    Tirelessly

    Capture the time in a hashmap using System.currentTimeMillis() then compare it.
     
  3. Offline

    Acer_Mortem

    What do you mean by that? Sorry, I'm new to coding.
     
  4. Offline

    Muxon

    Code:JAVA
    1. //First of all declare a HashMap
    2. HashMap<String, Long> countdown = new HashMap<String, Long>();
    3. //Then when the explosion is called in your plugin put the player in the Hashmap and the time he triggers it.
    4. countdown.put(player.getName(), System.currentTimeMillis());
    5. //So now the player is stored and the time when he triggers the explosion, too. Let's use it, to compare if he allready waited 10 sec
    6. if(countdown.get(player.getName()) - System.currentTimeMillis() >= 10000){
    7. //do some thing because he allready waited 10sec (10000 milli-seconds)
    8. }else{
    9. //what a badass
    10. player.sendMessage("You are a badass!");
    11. }

    @fireblast709 is that what he asked?:D
     
  5. Offline

    fireblast709

    lolz at the small note. Well it is a code description of what Tirelessly ment, so yea, this is the cooldown. Though you might want to change
    Code:java
    1. if(countdown.get(player.getName()) - System.currentTimeMillis() >= 10000){
    into
    Code:java
    1. if(System.currentTimeMillis() - (countdown.contains(player.getName()) ? countdown.get(player.getName()) : 0) >= 10000){
     
    Muxon likes this.
  6. Offline

    Acer_Mortem

    Doesn't seem to work =/. It just says "You are a Badass!"
     
  7. Offline

    Muxon

    Acer_Mortem
    I know the problem but for better helping purposes: could you please post your code snippet where the hashmap thing is now?:)
     
  8. Offline

    Acer_Mortem

    Here's the code with your suggestion:
    Code:
    import java.util.HashMap;
     
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
     
     
    public class MyPlayerListener implements Listener {
        public static NoSpammer plugin;
       
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event){
            Player player = event.getPlayer();
            int blockId = player.getItemInHand().getType().getId();
            if(blockId == 388){
                Block block = player.getTargetBlock(null, 10);
                Location location = block.getLocation();
                World world = player.getWorld();
                world.createExplosion(location, 5);
                HashMap<String, Long> countdown = new HashMap<String, Long>();
                countdown.put(player.getName(), System.currentTimeMillis());
                if(countdown.get(player.getName()) - System.currentTimeMillis() >= 10000){
                }else{
                player.sendMessage("You are a badass!");
                }
            }else if(blockId == 369){
                    Block block = player.getTargetBlock(null, 10);
                    Location location = block.getLocation();
                    World world = player.getWorld();
                    world.strikeLightning(location);
            }
        }
    }
    
    And here is the code with FireBlast709's suggestion:
    Code:
    import java.util.HashMap;
     
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
     
     
    public class MyPlayerListener implements Listener {
        public static NoSpammer plugin;
       
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event){
            Player player = event.getPlayer();
            int blockId = player.getItemInHand().getType().getId();
            if(blockId == 388){
                Block block = player.getTargetBlock(null, 10);
                Location location = block.getLocation();
                World world = player.getWorld();
                world.createExplosion(location, 5);
                HashMap<String, Long> countdown = new HashMap<String, Long>();
                countdown.put(player.getName(), System.currentTimeMillis());
                if(System.currentTimeMillis() - (countdown.contains(player.getName()) ? countdown.get(player.getName()) : 0) >= 10000){
                }else{
                player.sendMessage("You are a badass!");
                }
            }else if(blockId == 369){
                    Block block = player.getTargetBlock(null, 10);
                    Location location = block.getLocation();
                    World world = player.getWorld();
                    world.strikeLightning(location);
            }
        }
    }
    
    In FireBlast's code, there is an error saying "the method contains(string) is undefined..."
     
  9. Offline

    fireblast709

    Acer_Mortem sorry my bad, containsKey(player.getName())
     
  10. Offline

    Acer_Mortem

    Nothing happens =/

    It just says "You are a Badass!"

    No cooldown or anything =/
     
  11. Offline

    Muxon

    Code:JAVA
    1. public class MyPlayerListener implements Listener {
    2. public static NoSpammer plugin;
    3. //declare the HashMap there, because we need only one instance!
    4. private HashMap<String, Long> countdown = new HashMap<String, Long>();
    5.  
    6. @EventHandler
    7. public void onPlayerInteract(PlayerInteractEvent event){
    8. Player player = event.getPlayer();
    9. int blockId = player.getItemInHand().getType().getId();
    10. if(blockId == 388){
    11. //if the player is not in the HashMap put him in there!
    12. if(!countdown.containsKey(player.getName())){
    13. countdown.put(player.getName(), System.currentTimeMillis());
    14. Block block = player.getTargetBlock(null, 10);
    15. Location location = block.getLocation();
    16. World world = player.getWorld();
    17. world.createExplosion(location, 5);
    18. //else if he is, compare if he is in there 10000 milli-sec
    19. }else{
    20. if(countdown.get(player.getName()) - System.currentTimeMillis() >= 10000){
    21. player.sendMessage("You waited ten seconds!");
    22. //if he is in there for 10000 milli-sec remove him, because we do not longer need him!
    23. countdown.remove(player.getName());
    24. Block block = player.getTargetBlock(null, 10);
    25. Location location = block.getLocation();
    26. World world = player.getWorld();
    27. world.createExplosion(location, 5);
    28. }else{
    29. //if not, he is still a badass
    30. player.sendMessage("You are a badass!");
    31. }
    32. }
    33. }else if(blockId == 369){
    34. Block block = player.getTargetBlock(null, 10);
    35. Location location = block.getLocation();
    36. World world = player.getWorld();
    37. world.strikeLightning(location);
    38. }
    39. }
    40. }

    Yaaargh Captain Longbeard says it is not tested, but it should work fine!
     
  12. Offline

    Acer_Mortem

    ^ Hm, seems like the cooldown starts but never ends xD

    and every time I use the emerald again to trigger the explosive, it just keeps saying "You are a badass!"

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

    Muxon

    Code:JAVA
    1. import java.util.HashMap;
    2.  
    3. import org.bukkit.Location;
    4. import org.bukkit.World;
    5. import org.bukkit.block.Block;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.player.PlayerInteractEvent;
    10.  
    11. public class MyPlayerListener implements Listener {
    12. public static NoSpammer plugin;
    13. //declare the HashMap there, because we need only one instance!
    14. private HashMap<String, Long> countdown = new HashMap<String, Long>();
    15.  
    16. @EventHandler
    17. public void onPlayerInteract(PlayerInteractEvent event){
    18. Player player = event.getPlayer();
    19. int blockId = player.getItemInHand().getType().getId();
    20. if(blockId == 388){
    21. //if the player is not in the HashMap put him in there!
    22. if(!countdown.containsKey(player.getName())){
    23. countdown.put(player.getName(), System.currentTimeMillis());
    24. Block block = player.getTargetBlock(null, 10);
    25. Location location = block.getLocation();
    26. World world = player.getWorld();
    27. world.createExplosion(location, 5);
    28. //else if he is, compare if he is in there 10000 milli-sec
    29. }else{
    30. if(countdown.get(System.currentTimeMillis() - countdown.get(player.getName())) > 10000){
    31. player.sendMessage("You waited ten seconds!");
    32. //if he is in there for 10000 milli-sec remove him, because we do not longer need him!
    33. countdown.remove(player.getName());
    34. Block block = player.getTargetBlock(null, 10);
    35. Location location = block.getLocation();
    36. World world = player.getWorld();
    37. world.createExplosion(location, 5);
    38. }else if(System.currentTimeMillis() - countdown.get(player.getName()) < 10000){
    39. //if not, he is still a badass
    40. player.sendMessage("You are a badass!");
    41. }
    42. }
    43. }
    44. }
    45. }

    Sorry for some logical errors but i'm very tired :eek:
    btw if the plugin keeps saying you are a badass you should listen ;)
    fireblast709 hardcorefixed!
     
  14. Offline

    fireblast709

    Muxon your stored System.currentTimeMillis() - the actual System.currentTimeMillis() will always be negative
    Acer_Mortem use my code, with the change of .contains to .containsKey
     
  15. Offline

    Acer_Mortem

    fireblast709 Your code allows the explosion to occur. The only noticable thing is "You are a Badass!"
    Muxon Is there anyway you could re-enable the explosion to occur? Because then it would work.
     
  16. Offline

    Muxon

    I'm quite sure that both, fireblast and Muxon's code, should work now.
    I edited my code above so try it again :)
     
  17. Offline

    fireblast709

    What about it?

    Syntax errors ;3

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

    Muxon

    Syntax error in fireblasts post: Should be: Syntax error! :D
    Updated code in post above!
     
  19. Offline

    Acer_Mortem

    Muxon Same error?
    Any code that actually works?
    Btw, thanks for all the help guys =) It's really appreciated.
     
  20. Offline

    Muxon

    Show Spoiler

    Code:JAVA
    1.  
    2. import java.util.HashMap;
    3.  
    4. import org.bukkit.Location;
    5. import org.bukkit.World;
    6. import org.bukkit.block.Block;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.player.PlayerInteractEvent;
    11.  
    12. public class MyPlayerListener implements Listener {
    13. public static NoSpammer plugin;
    14. //declare the HashMap there, because we need only one instance!
    15. private HashMap<String, Long> countdown = new HashMap<String, Long>();
    16.  
    17. @EventHandler
    18. public void onPlayerInteract(PlayerInteractEvent event){
    19. Player player = event.getPlayer();
    20. int blockId = player.getItemInHand().getType().getId();
    21. if(blockId == 388){
    22. //if the player is not in the HashMap put him in there!
    23. if(!countdown.containsKey(player.getName())){
    24. countdown.put(player.getName(), System.currentTimeMillis());
    25. Block block = player.getTargetBlock(null, 10);
    26. Location location = block.getLocation();
    27. World world = player.getWorld();
    28. world.createExplosion(location, 5);
    29. //else if he is, compare if he is in there 10000 milli-sec
    30. }else{
    31. if(countdown.get(System.currentTimeMillis() - countdown.get(player.getName())) > 10000){
    32. player.sendMessage("You waited ten seconds!");
    33. //if he is in there for 10000 milli-sec remove him, because we do not longer need him!
    34. countdown.remove(player.getName());
    35. Block block = player.getTargetBlock(null, 10);
    36. Location location = block.getLocation();
    37. World world = player.getWorld();
    38. world.createExplosion(location, 5);
    39. }else if(System.currentTimeMillis() - countdown.get(player.getName()) < 10000){
    40. //if not, he is still a badass
    41. player.sendMessage("You are a badass!");
    42. }
    43. }
    44. }
    45. }
    46. }
    47.  



    Finally this should work now
     
  21. Offline

    fireblast709

    Acer_Mortem
    Code:java
    1. import java.util.HashMap;
    2.  
    3. import org.bukkit.Location;
    4. import org.bukkit.World;
    5. import org.bukkit.block.Block;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.player.PlayerInteractEvent;
    10.  
    11.  
    12. public class MyPlayerListener implements Listener {
    13. public static NoSpammer plugin;
    14.  
    15. HashMap<String, Long> countdown = new HashMap<String, Long>();
    16.  
    17. @EventHandler
    18. public void onPlayerInteract(PlayerInteractEvent event){
    19. Player player = event.getPlayer();
    20. int blockId = player.getItemInHand().getType().getId();
    21. Block block = player.getTargetBlock(null, 10);
    22. if(block == null) return;
    23.  
    24. if(blockId == 388){
    25. if(System.currentTimeMillis() - (countdown.containsKey(player.getName()) ? countdown.get(player.getName()) : 0) >= 10000)
    26. {
    27. Location location = block.getLocation();
    28. World world = player.getWorld();
    29. world.createExplosion(location, 5);
    30. countdown.put(player.getName(), System.currentTimeMillis());
    31. }
    32. else
    33. {
    34. player.sendMessage("You are a badass!");
    35. }
    36. }
    37. else if(blockId == 369)
    38. {
    39. Location location = block.getLocation();
    40. World world = player.getWorld();
    41. world.strikeLightning(location);
    42. }
    43. }
    44. }
     
  22. Offline

    Acer_Mortem

    *sigh* Once again, doesn't work =( Explosive goes once, but the countdown doesn't end. Maybe is there a way to freeze the player so that he cannot use the item again?

    OMG! THIS ONE WORKS! THANK YOU SO MUCH! *BIG HUGS*

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

    fireblast709

    Acer_Mortem nope works fine with me. Make sure you wait the 10 seconds. Nvm you found out yourself
     
  24. Offline

    Muxon

    fireblast709
    Show Spoiler

    [​IMG]
     
  25. Offline

    fireblast709

    chasechocolate and Muxon like this.
  26. Offline

    Acer_Mortem

    *Hugs hand*
     
  27. Offline

    moo3oo3oo3

    fireblast or muxon?
     
  28. Offline

    Acer_Mortem

Thread Status:
Not open for further replies.

Share This Page