From 4a2f9dd49613973c0b670fea31ea6055f03d9f23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Bro=C4=8Dko?= Date: Thu, 13 Sep 2018 22:48:05 +0200 Subject: [PATCH] Moved cooldown stuff to a helper class --- src/cz/marwland/mc/core/util/Cooldown.java | 63 +++++++++++++++++++ .../essentials/features/RandomTeleport.java | 33 ++-------- 2 files changed, 68 insertions(+), 28 deletions(-) create mode 100644 src/cz/marwland/mc/core/util/Cooldown.java diff --git a/src/cz/marwland/mc/core/util/Cooldown.java b/src/cz/marwland/mc/core/util/Cooldown.java new file mode 100644 index 0000000..70f48f8 --- /dev/null +++ b/src/cz/marwland/mc/core/util/Cooldown.java @@ -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 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()); + } + +} diff --git a/src/cz/marwland/mc/essentials/features/RandomTeleport.java b/src/cz/marwland/mc/essentials/features/RandomTeleport.java index c63a914..418b7f5 100644 --- a/src/cz/marwland/mc/essentials/features/RandomTeleport.java +++ b/src/cz/marwland/mc/essentials/features/RandomTeleport.java @@ -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 cooldownMap = new HashMap<>(); + private Cooldown telCooldown = new Cooldown(this); private static final Set 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; - } - }