Advertisement
BingoRufus

Cooldown class

Nov 17th, 2020
1,442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.69 KB | None | 0 0
  1. import java.util.HashMap;
  2.  
  3. public class Cooldown<T> {
  4.  
  5.     protected final HashMap<T, Long> cooldowns = new HashMap<>();
  6.     private final long cooldownTime;
  7.  
  8.     public Cooldown(long cooldownTimeMillis) {
  9.         this.cooldownTime = cooldownTimeMillis;
  10.     }
  11.  
  12.     public boolean isOnCooldown(T t) {
  13.         if (!cooldowns.containsKey(t)) return false;
  14.         return getTimeRemaining(t) > 0;
  15.  
  16.     }
  17.  
  18.     public long getTimeRemaining(T t) {
  19.         if (!cooldowns.containsKey(t)) return -1;
  20.         return cooldownTime - (System.currentTimeMillis() - cooldowns.get(t));
  21.     }
  22.  
  23.     public void addToCooldown(T t) {
  24.         cooldowns.put(t, System.currentTimeMillis());
  25.     }
  26. }
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement