Created
April 15, 2020 03:07
-
-
Save kBULOSU/e556bac74b87819925ac89fda194622b to your computer and use it in GitHub Desktop.
Generic Cooldown Java Library, currently using in my plugins.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Map; | |
import java.util.WeakHashMap; | |
import java.util.concurrent.TimeUnit; | |
public class Cooldown<T> { | |
private Map<T, Long> map; | |
public Cooldown() { | |
this.map = new WeakHashMap<>(); | |
} | |
public boolean isOnCooldown(T key) { | |
return this.map.containsKey(key) && this.map.get(key) >= System.currentTimeMillis(); | |
} | |
public boolean putOnCooldown(T key, int time, TimeUnit unit) { | |
if (isOnCooldown(key)) { | |
return true; | |
} else { | |
this.map.remove(key); | |
this.map.put(key, unit.toMillis(time) + System.currentTimeMillis()); | |
return false; | |
} | |
} | |
public long getRemainingTime(T key) { | |
return this.map.getOrDefault(key, System.currentTimeMillis()) - System.currentTimeMillis(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment