Moved cooldown stuff to a helper class
This commit is contained in:
parent
751927349d
commit
4a2f9dd496
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());
|
||||
}
|
||||
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
package cz.marwland.mc.essentials.features;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@ -17,14 +16,13 @@ import org.bukkit.event.block.BlockCanBuildEvent;
|
||||
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
|
||||
|
||||
import cz.marwland.mc.core.features.Feature;
|
||||
import cz.marwland.mc.core.util.UserUtil;
|
||||
import cz.marwland.mc.core.util.Cooldown;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
|
||||
public class RandomTeleport extends Feature {
|
||||
|
||||
private final int MAX_TRIES = 100;
|
||||
private final long DEFAULT_COOLDOWN = 15000;
|
||||
private HashMap<Player, Long> cooldownMap = new HashMap<>();
|
||||
private Cooldown telCooldown = new Cooldown(this);
|
||||
|
||||
private static final Set<Material> INVALID_MATERIALS = new HashSet<>(Arrays.asList(
|
||||
Material.AIR,
|
||||
@ -83,13 +81,13 @@ public class RandomTeleport extends Feature {
|
||||
return true;
|
||||
}
|
||||
|
||||
long left = (long) Math.ceil((double) cooldownLeft(p) / 1000d);
|
||||
long left = telCooldown.timeLeftSeconds(p);
|
||||
if (left > 0) {
|
||||
sender.sendMessage(ChatColor.RED + "Nemuzes se nahodne teleportovat jeste " + ChatColor.YELLOW + left + ChatColor.RED + " sekund" + (left == 1 ? "u!" : (left > 4 ? "!" : "y!")));
|
||||
return true;
|
||||
}
|
||||
cooldownMap.put(p, System.currentTimeMillis());
|
||||
|
||||
|
||||
telCooldown.resetPlayer(p);
|
||||
randomTeleport(w, p);
|
||||
|
||||
return true;
|
||||
@ -168,25 +166,4 @@ public class RandomTeleport extends Feature {
|
||||
return true;
|
||||
}
|
||||
|
||||
public long cooldownLeft(Player p) {
|
||||
long cooldown = DEFAULT_COOLDOWN;
|
||||
|
||||
// marw.core.randomteleport.cooldown
|
||||
String meta = UserUtil.getPlayersMeta(p, this.getPermissionPath() + ".cooldown", "15");
|
||||
if (meta != null) {
|
||||
try {
|
||||
cooldown = Long.parseLong(meta) * 1000l;
|
||||
} catch (NumberFormatException ex) {
|
||||
plugin.getLogger().info("ERROR: " + this.getPermissionPath() + ".cooldown" + " 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user