[Solved] Hashmap cooldown issues

Discussion in 'Plugin Development' started by Turtlegasm, Aug 12, 2012.

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

    Turtlegasm

    I had this working a while back but had to go back to an old backup because i'm an idiot but now I can't remember how it worked before...It's not ticking down from 5 seconds every time the event occurs ingame it says it's at 5 seconds still...Can anyone help? Code:
    Code:java
    1. package com.github.Turtlegasm;
    2.  
    3. import java.util.HashMap;
    4. import java.util.logging.Logger;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.block.Action;
    11. import org.bukkit.event.block.BlockPlaceEvent;
    12. import org.bukkit.event.player.PlayerInteractEvent;
    13. public class CheeseListener implements Listener{
    14. Logger log = Logger.getLogger("Minecraft");
    15.  
    16.  
    17. private HashMap<String, Long> COOLDOWN = new HashMap<String, Long>();
    18. public int COOLDOWN_TIME = 5;
    19.  
    20. @EventHandler
    21. public void onPlayerInteractEvent(PlayerInteractEvent evt)
    22. {
    23.  
    24. if(evt.getPlayer().getItemInHand().getTypeId() == Material.GOLD_HOE.getId()){
    25. Player p = evt.getPlayer();
    26. COOLDOWN.put(p.getName(), System.currentTimeMillis());
    27. if (p.hasPermission("cheese.hoe")&& COOLDOWN.containsKey(p.getName())){
    28. long diff = (System.currentTimeMillis() - COOLDOWN.get(p.getName())) / 1000;
    29. if (diff < COOLDOWN_TIME) {
    30. p.sendMessage(ChatColor.DARK_AQUA+ "You must wait " + (COOLDOWN_TIME - diff) + " seconds before you can do that again!");
    31. return;
    32. }
    33. COOLDOWN.put(p.getName(), System.currentTimeMillis());
    34.  
    35.  
    36.  
    37. evt.getPlayer().getWorld().strikeLightning(evt.getPlayer().getTargetBlock(null, 200).getLocation());
    38. System.out.print(evt.getPlayer().getName()+ " struck lightning");
    39. COOLDOWN.put(p.getName(), System.currentTimeMillis());
    40.  
    41. }
    42.  
    43. }
     
  2. Offline

    Phil2812

    Code:java
    1.  
    2. COOLDOWN.put(p.getName(), System.currentTimeMillis());
    3. if (p.hasPermission("cheese.hoe")&& COOLDOWN.containsKey(p.getName())){
    4. long diff = (System.currentTimeMillis() - COOLDOWN.get(p.getName())) / 1000;
    5. if (diff < COOLDOWN_TIME) {
    6. p.sendMessage(ChatColor.DARK_AQUA+ "You must wait " + (COOLDOWN_TIME - diff) + " seconds before you can do that again!");
    7. return;
    8. }
    9. COOLDOWN.put(p.getName(), System.currentTimeMillis());
    10.  


    I guess it is because you actually reset the cooldown right before you check it so it will never be over the moment you check it. And once again after you have checked it. Remove the first line of the code I posted in your code and you should be fine.
    You can use [ syntax = java ] [ / syntax] (without the whitespaces) for java code.
     
  3. Offline

    Turtlegasm

    still no luck I will post full class just in case :|

    Code:java
    1. package com.github.Turtlegasm;
    2.  
    3. import java.util.HashMap;
    4. import java.util.logging.Logger;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.block.Action;
    11. import org.bukkit.event.block.BlockPlaceEvent;
    12. import org.bukkit.event.player.PlayerInteractEvent;
    13. public class CheeseListener implements Listener{
    14. Logger log = Logger.getLogger("Minecraft");
    15.  
    16.  
    17. private HashMap<String, Long> COOLDOWN = new HashMap<String, Long>();
    18. public int COOLDOWN_TIME = 5;
    19.  
    20. @EventHandler
    21. public void onPlayerInteractEvent(PlayerInteractEvent evt)
    22. {
    23.  
    24. if(evt.getPlayer().getItemInHand().getTypeId() == Material.GOLD_HOE.getId()){
    25. Player p = evt.getPlayer();
    26. COOLDOWN.put(p.getName(), System.currentTimeMillis());
    27. if (p.hasPermission("cheese.hoe")&& COOLDOWN.containsKey(p.getName())){
    28. long diff = (System.currentTimeMillis() - COOLDOWN.get(p.getName())) / 1000;
    29. if (diff < COOLDOWN_TIME) {
    30. p.sendMessage(ChatColor.DARK_AQUA+ "You must wait " + (COOLDOWN_TIME - diff) + " seconds before you can do that again!");
    31. return;
    32. }
    33. COOLDOWN.put(p.getName(), System.currentTimeMillis());
    34.  
    35.  
    36.  
    37. evt.getPlayer().getWorld().strikeLightning(evt.getPlayer().getTargetBlock(null, 200).getLocation());
    38. System.out.print(evt.getPlayer().getName()+ " struck lightning");
    39. COOLDOWN.put(p.getName(), System.currentTimeMillis());// tried with and without this still doesnt go down from5
    40.  
    41. }
    42.  
    43. }
    44. if(evt.getAction()==Action.RIGHT_CLICK_BLOCK){
    45. Player p = evt.getPlayer();
    46. if(evt.getClickedBlock().getType() == Material.SPONGE){
    47. p.setFoodLevel(20);
    48. p.sendMessage(ChatColor.YELLOW + "You ate some cheese...Yum!");
    49.  
    50. }
    51.  
    52.  
    53. }
    54.  
    55. }
    56.  
    57. @EventHandler
    58. public void onBlockPlace(BlockPlaceEvent bpevt) {
    59. if(bpevt.getBlock().getType() == Material.SPONGE){
    60. bpevt.getPlayer().sendMessage(ChatColor.YELLOW + "You placed Cheese! It's an infinite source of food.");
    61.  
    62. }
    63.  
    64. }
    65. } [ /syntax]
     
  4. Offline

    Njol

    Read his post again. You should remove the first COOLDOWN.put(...) line ;)
     
  5. Offline

    Phil2812

    Turtlegasm
    Like Njol said I wanted you to remove line 26 in your posted code.
    Because it sets the cooldown right before you check it. Because of this diff should be close to 0 every check.
     
  6. Offline

    Turtlegasm

    I tried but it didn't work so I put it back
     
  7. Offline

    Phil2812

    Remove it, as it doesn't make sense. You might have another error, though.
    When you remove it, what happens and what should happen?
     
  8. Offline

    Turtlegasm

    Sorry, I'm sleepy...
    When I remove it, using the gold hoe does nothing (sends no message to player, console or strikes lightning)
     
  9. Offline

    Phil2812

    You removed the wrong one? You should not remove the one at the end but the one in line 26. Sure you did that? Because your comment is at the wrong line.
     
  10. Offline

    Turtlegasm

    I removed the one between these
    Player p = evt.getPlayer();

    if (p.hasPermission("cheese.hoe")){
     
  11. Offline

    Phil2812

    Okay because you commented that you removed it at line 39. I will look into this code in a few minutes.
     
  12. Offline

    Turtlegasm

    Yeah i made that comment before I think
     
  13. Offline

    Phil2812

    Okay I looked at it and this works for me:

    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerInteractEvent(PlayerInteractEvent evt)
    4. {
    5. if(evt.getPlayer().getItemInHand().getTypeId() == Material.GOLD_HOE.getId())
    6. {
    7. Player p = evt.getPlayer();
    8. if (COOLDOWN.containsKey(p.getName()))
    9. {
    10. long diff = (System.currentTimeMillis() - COOLDOWN.get(p.getName())) / 1000;
    11. if (diff < COOLDOWN_TIME)
    12. {
    13. p.sendMessage(ChatColor.DARK_AQUA+ "You must wait " + (COOLDOWN_TIME - diff) + " seconds before you can do that again!");
    14. return;
    15. }
    16. }
    17. evt.getPlayer().getWorld().strikeLightning(evt.getPlayer().getTargetBlock(null, 200).getLocation());
    18. System.out.print(evt.getPlayer().getName()+ " struck lightning");
    19. COOLDOWN.put(p.getName(), System.currentTimeMillis());
    20. }
    21. }
    22.  
     
    Ibix13 and Turtlegasm like this.
  14. Offline

    Turtlegasm

    Testing now...
    Edit: Oh god I love you thanks so much!
     
  15. Offline

    Ibix13

    Thank god you made this :D I have been trying to find a way to make a cooldown with messages for like 12 HOURS!!! THANK YOU
     
Thread Status:
Not open for further replies.

Share This Page