Solved Cooldown help

Discussion in 'Plugin Development' started by Blackwing_Forged, Oct 7, 2017.

Thread Status:
Not open for further replies.
  1. So i made a hashmap like this
    public HashMap<UUID, Long> cooldowns = new HashMap<UUID, Long>();
    and i put players in the map like this
    cooldowns.put(p.getUniqueId(), System.currentTimeMillis());
    and i have a variable that has the seconds that i want the cooldown to be like this
    final int seconds = 60;

    my question is how do i check if the cooldown is over and do i subtract any time?
    ive looked at many many posts about these types of cooldowns and haven't gotten anywhere
     
  2. Offline

    MightyOne

    Thought about a Bukkit.getScheduler.runSyncDelayedTask ()?
     
  3. yeah ive thought about it, what would you say the differences are about both of them and their advantages and disadvantages?

    Because a lot of the threads ive read use the System.currentTimeMillis() over a scheduler

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  4. Offline

    Joey402MC

    Instead of storing the current time, store the number 60.

    Code:
    cooldowns.put(p.getUniqueId(), 60);
    1. Create a BukkitRunnable that goes off every second
    2. In that BukkitRunnable, you can use cooldowns.replace(p.getUniqueId(), cooldowns.get(p.getUniqueId()) - 1) to lower the seconds left in the cooldown by 1.
    3. Then, you can check if the cooldown is equal to 0 by using:
    Code:
      if(cooldowns.get(p.getUniqueId()) == 0){
    
    // cooldown over
    
       }  
    Here is some more information on Scheduler Programming:
    https://bukkit.gamepedia.com/Scheduler_Programming
     
  5. I know how to use schedulers, i was just wondering about the currentTimeMillis, but i guess ill end up doing the runnable
     
  6. Offline

    Side8StarLite

    @Blackwing_Forged
    current time - player time >= 60L
    Is this what you want?

    Code:
    if (System.currentTimeMillis() - HashMap#get(uuid) >= 60L)
        // do something
     
    Blackwing_Forged likes this.
  7. is that if the time is up?
     
  8. Offline

    Side8StarLite

  9. Alright thanks
     
Thread Status:
Not open for further replies.

Share This Page