Skip to content

Instantly share code, notes, and snippets.

@kBULOSU
Created April 15, 2020 03:07
Show Gist options
  • Save kBULOSU/e556bac74b87819925ac89fda194622b to your computer and use it in GitHub Desktop.
Save kBULOSU/e556bac74b87819925ac89fda194622b to your computer and use it in GitHub Desktop.
Generic Cooldown Java Library, currently using in my plugins.
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