mirror of
https://github.com/SimplexDevelopment/FeelingLucky.git
synced 2025-07-12 08:08:34 +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:
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));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user