From fe620952bc7fff13bf52c8760673f38717fbfd39 Mon Sep 17 00:00:00 2001 From: Paldiu Date: Sun, 17 Apr 2022 14:48:05 -0500 Subject: [PATCH] Add CooldownTimer Added the Cooldown Timer for when a user uses a Rabbit's Foot. This restricts a user from using rabbit's feet consecutively by forcing a 30 second cooldown between uses. --- .../io/github/simplex/luck/FeelingLucky.java | 1 + .../simplex/luck/listener/PlayerListener.java | 40 ++++++++++++++++++- .../simplex/luck/player/PlayerHandler.java | 6 +++ .../simplex/luck/util/CooldownTimer.java | 38 ++++++++++++++++++ 4 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 src/main/java/io/github/simplex/luck/util/CooldownTimer.java diff --git a/src/main/java/io/github/simplex/luck/FeelingLucky.java b/src/main/java/io/github/simplex/luck/FeelingLucky.java index 257bc1a..bdbd2d4 100644 --- a/src/main/java/io/github/simplex/luck/FeelingLucky.java +++ b/src/main/java/io/github/simplex/luck/FeelingLucky.java @@ -3,6 +3,7 @@ package io.github.simplex.luck; import io.github.simplex.luck.listener.*; import io.github.simplex.luck.player.PlayerConfig; import io.github.simplex.luck.player.PlayerHandler; +import io.github.simplex.luck.util.CooldownTimer; import io.github.simplex.luck.util.LuckCMD; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; diff --git a/src/main/java/io/github/simplex/luck/listener/PlayerListener.java b/src/main/java/io/github/simplex/luck/listener/PlayerListener.java index 6ff8fbe..74fcf6b 100644 --- a/src/main/java/io/github/simplex/luck/listener/PlayerListener.java +++ b/src/main/java/io/github/simplex/luck/listener/PlayerListener.java @@ -3,6 +3,7 @@ package io.github.simplex.luck.listener; import io.github.simplex.lib.MiniComponent; import io.github.simplex.luck.FeelingLucky; import io.github.simplex.luck.player.Luck; +import io.github.simplex.luck.util.CooldownTimer; import io.github.simplex.luck.util.SpecialFootItem; import net.kyori.adventure.text.Component; import org.bukkit.Bukkit; @@ -19,9 +20,15 @@ import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; -public record PlayerListener(FeelingLucky plugin) implements Listener { +import java.util.Objects; + +public final class PlayerListener implements Listener { + private final FeelingLucky plugin; + private final CooldownTimer timer; + public PlayerListener(FeelingLucky plugin) { this.plugin = plugin; + this.timer = new CooldownTimer(); Bukkit.getServer().getPluginManager().registerEvents(this, plugin); } @@ -32,15 +39,24 @@ public record PlayerListener(FeelingLucky plugin) implements Listener { SpecialFootItem special = new SpecialFootItem(); Player player = event.getPlayer(); Luck luck = plugin.getHandler().getLuckContainer(player); + + if (timer.onCooldown(player)) { + player.sendMessage(MiniComponent.err("That feature can only be used once every 30 seconds.")); + player.sendMessage(MiniComponent.info("You have " + timer.remaining(player) + " seconds remaining.")); + return; + } + if (action.isRightClick() && player.getInventory().getItemInMainHand().isSimilar(foot)) { if (foot.getItemMeta().equals(special.meta()) || foot.equals(special.get())) { luck.setMultiplier(luck.multiplier() + 1); + player.sendMessage(MiniComponent.info("Your luck multiplier has increased by 1!")); } double rng = Luck.RNG().nextDouble(2.0, 5.0); player.getInventory().remove(player.getInventory().getItemInMainHand()); luck.addTo(rng); plugin.getHandler().updatePlayer(player, luck); - player.sendMessage(Component.empty().content("Your luck has been increased by " + rng + " points.")); + timer.setCooldown(player.getUniqueId(), System.currentTimeMillis()); + player.sendMessage(MiniComponent.info("Your luck has been increased by " + rng + " points.")); } } @@ -67,4 +83,24 @@ public record PlayerListener(FeelingLucky plugin) implements Listener { } } } + + @Override + public boolean equals(Object obj) { + if (obj == this) return true; + if (obj == null || obj.getClass() != this.getClass()) return false; + var that = (PlayerListener) obj; + return Objects.equals(this.plugin, that.plugin); + } + + @Override + public int hashCode() { + return Objects.hash(plugin); + } + + @Override + public String toString() { + return "PlayerListener[" + + "plugin=" + plugin + ']'; + } + } diff --git a/src/main/java/io/github/simplex/luck/player/PlayerHandler.java b/src/main/java/io/github/simplex/luck/player/PlayerHandler.java index 61ddd60..8879fea 100644 --- a/src/main/java/io/github/simplex/luck/player/PlayerHandler.java +++ b/src/main/java/io/github/simplex/luck/player/PlayerHandler.java @@ -6,6 +6,7 @@ import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerLoginEvent; +import org.bukkit.event.player.PlayerQuitEvent; import java.util.HashMap; import java.util.Map; @@ -52,4 +53,9 @@ public class PlayerHandler implements Listener { playerLuckMap.put(player, container); } + + @EventHandler + public void clearContainer(PlayerQuitEvent event) { + playerLuckMap.remove(event.getPlayer()); + } } diff --git a/src/main/java/io/github/simplex/luck/util/CooldownTimer.java b/src/main/java/io/github/simplex/luck/util/CooldownTimer.java new file mode 100644 index 0000000..3f938a2 --- /dev/null +++ b/src/main/java/io/github/simplex/luck/util/CooldownTimer.java @@ -0,0 +1,38 @@ +package io.github.simplex.luck.util; + +import io.github.simplex.lib.MiniComponent; +import net.kyori.adventure.text.Component; +import org.bukkit.entity.Player; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.TimeUnit; + +public class CooldownTimer { + private final Map cooldowns = new HashMap<>(); + + public static final long DEFAULT_COOLDOWN = 30L; + + public void setCooldown(UUID playerUUID, long time) { + if (time < 1) { + cooldowns.remove(playerUUID); + } else { + cooldowns.put(playerUUID, time); + } + } + + public long getCooldown(UUID uuid) { + return cooldowns.getOrDefault(uuid, 0L); + } + + public long remaining(Player player) { + long timeLeft = System.currentTimeMillis() - getCooldown(player.getUniqueId()); + return TimeUnit.MILLISECONDS.toSeconds(timeLeft) - DEFAULT_COOLDOWN; + } + + public boolean onCooldown(Player player) { + long remaining = System.currentTimeMillis() - getCooldown(player.getUniqueId()); + return (!(TimeUnit.MILLISECONDS.toSeconds(remaining) >= DEFAULT_COOLDOWN)); + } +}