Moved cooldown stuff to a helper class
This commit is contained in:
63
src/cz/marwland/mc/core/util/Cooldown.java
Normal file
63
src/cz/marwland/mc/core/util/Cooldown.java
Normal file
@ -0,0 +1,63 @@
|
||||
package cz.marwland.mc.core.util;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import cz.marwland.mc.core.MarwCore;
|
||||
import cz.marwland.mc.core.features.Feature;
|
||||
|
||||
public class Cooldown {
|
||||
|
||||
private long defaultCooldown = 15000;
|
||||
private HashMap<Player, Long> cooldownMap = new HashMap<>();
|
||||
private String permissionNode = "";
|
||||
|
||||
public Cooldown(Feature f) {
|
||||
this(f.getPermissionPath() + ".cooldown");
|
||||
}
|
||||
|
||||
public Cooldown(Feature f, long defaultCooldown) {
|
||||
this(f);
|
||||
this.defaultCooldown = defaultCooldown;
|
||||
}
|
||||
|
||||
public Cooldown(String permissionNode) {
|
||||
this.permissionNode = permissionNode;
|
||||
}
|
||||
|
||||
public Cooldown(String permissionNode, long defaultCooldown) {
|
||||
this(permissionNode);
|
||||
this.defaultCooldown = defaultCooldown;
|
||||
}
|
||||
|
||||
public long timeLeft(Player p) {
|
||||
long cooldown = defaultCooldown;
|
||||
|
||||
// marw.core.randomteleport.cooldown
|
||||
String meta = UserUtil.getPlayersMeta(p, permissionNode, null);
|
||||
if (meta != null) {
|
||||
try {
|
||||
cooldown = Long.parseLong(meta) * 1000l;
|
||||
} catch (NumberFormatException ex) {
|
||||
MarwCore.getInstance().getLogger().info("ERROR: " + permissionNode + " is not a long for " + p.getName() + ": " + meta);
|
||||
}
|
||||
}
|
||||
|
||||
Long lastExecuted = cooldownMap.get(p);
|
||||
if (lastExecuted == null)
|
||||
return 0l;
|
||||
|
||||
long left = cooldown - (System.currentTimeMillis() - lastExecuted.longValue());
|
||||
return left;
|
||||
}
|
||||
|
||||
public long timeLeftSeconds(Player p) {
|
||||
return (long) Math.ceil((double) this.timeLeft(p) / 1000d);
|
||||
}
|
||||
|
||||
public void resetPlayer(Player p) {
|
||||
cooldownMap.put(p, System.currentTimeMillis());
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user