Solved Cooldown

Discussion in 'Plugin Development' started by peti446, Sep 27, 2013.

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

    peti446

    Hi i have this code for cooldwon:
    Code:java
    1.  
    2. public HashMap<String, Long> cooldowns = new HashMap<String, Long>();
    3.  
    4. int cooldownTime = 86400;
    5. if(cooldowns.containsKey(p.getName())) {
    6. long remeaningseconds = ((cooldowns.get(sender.getName()) / 1000) + cooldownTime) - (System.currentTimeMillis() / 1000);
    7. if(segundosrestabtes > 0){
    8. int days= (int)(((double)segundosrestabtes / 5184000D - (double)(segundosrestabtes / 518400L)) * 60D);
    9. int houre= (int)(((double)segundosrestabtes / 216000D - (double)(segundosrestabtes / 216000L)) * 60D);
    10. int minutes = (int)(((double)segundosrestabtes / 3600D - (double)(segundosrestabtes / 3600L)) * 60D);
    11. int seconds = (int)(((double)segundosrestabtes / 60D - (double)(segundosrestabtes / 60L)) * 60D);
    12. if(remeaningseconds - 216000L > 0L){
    13. // do somethings
    14. return true;
    15. } else if(remeaningseconds - 3600L > 0L){
    16. // do somethings
    17. return true;
    18. } else if(remeaningseconds - 60L > 0){
    19. // do somethings
    20. return true;
    21. } else {
    22. // do somethings
    23. return true;
    24. }
    25. }
    26. }
    27. cooldowns.put(p.getName(), System.currentTimeMillis());
    28. //Command her!

    But when i restart server the cooldown restart to 0 seconds?
    Why can i solve this ??
     
  2. Offline

    1Achmed1

    I don't recommend you use them a lot but this video may help:
     
  3. Offline

    Loogeh

    peti446 The reason why it clears it when the server restarts/reloads is that all hashmaps, lists, etc are cleared when it restarts/reloads. To make it continue through a restart you have to save the remaining time of the cooldown somewhere, say, a database or a file. To learn how to save hashmaps to a file there are tons of threads already about that, just use the search bar.
     
    Gater12 likes this.
  4. Offline

    CakeProgrammer

    create a ArrayList<String> are storge the player name, then check if he's on the ArrayList
    if he's this means he on cooldown, if he not do what you want and add him to the ArrayList^^
    then use DelayedTask and choose time and then remove him from the list
     
  5. Offline

    peti446

    Thx It works now :D
     
  6. Offline

    stonar96

    1Achmed1
    The guy in the video use an AsyncRepeatingTask and then he calls Bukkit methodes. That´s not a good model.
     
  7. Offline

    1Achmed1

    stonar96 It may not be good - AT ALL - but it gets the job done
     
  8. Offline

    stonar96

    Code:java
    1. getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    2. public void run() {
    3. //code
    4. }
    5. }, <ticks delay>L, <ticks repeat>L);
     
Thread Status:
Not open for further replies.

Share This Page