mirror of
https://github.com/SimplexDevelopment/FeelingLucky.git
synced 2024-11-24 19:05:02 +00:00
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.
This commit is contained in:
parent
5b1fb352bb
commit
fe620952bc
@ -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;
|
||||
|
@ -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 + ']';
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
38
src/main/java/io/github/simplex/luck/util/CooldownTimer.java
Normal file
38
src/main/java/io/github/simplex/luck/util/CooldownTimer.java
Normal file
@ -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<UUID, Long> 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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user