Initialisation complète de la bibliothèque betterMcCommands (DSL, arguments typés, cycle de vie des événements, injection dynamique CommandMap)
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
package fr.luc.bettermccommands.cooldown;
|
||||
|
||||
import fr.luc.bettermccommands.api.CommandNode;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* Gère les temps de recharge (Cooldowns) par commande et par joueur.
|
||||
*/
|
||||
public class CooldownManager {
|
||||
|
||||
private final Map<String, Map<UUID, Instant>> cooldowns = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* Vérifie si l'émetteur est actuellement soumis à un temps de recharge pour le nœud de commande donné.
|
||||
*
|
||||
* @param node Le nœud de commande.
|
||||
* @param sender L'émetteur testé.
|
||||
* @return true si l'émetteur est en cooldown, sinon false.
|
||||
*/
|
||||
public boolean isOnCooldown(CommandNode node, CommandSender sender) {
|
||||
if (!(sender instanceof Player player)) {
|
||||
return false; // La console n'a pas de cooldown
|
||||
}
|
||||
if (node.getCooldown() == null || node.getCooldown().isZero() || node.getCooldown().isNegative()) {
|
||||
return false;
|
||||
}
|
||||
if (node.getCooldownBypassPermission() != null && player.hasPermission(node.getCooldownBypassPermission())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Map<UUID, Instant> nodeMap = cooldowns.get(node.getFullName());
|
||||
if (nodeMap == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Instant expiration = nodeMap.get(player.getUniqueId());
|
||||
if (expiration == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Instant.now().isAfter(expiration)) {
|
||||
nodeMap.remove(player.getUniqueId());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calcule le temps restant avant expiration du cooldown pour un émetteur.
|
||||
*
|
||||
* @param node Le nœud de commande.
|
||||
* @param sender L'émetteur testé.
|
||||
* @return La durée restante, ou {@link Duration#ZERO} si aucun cooldown n'est actif.
|
||||
*/
|
||||
public Duration getRemainingCooldown(CommandNode node, CommandSender sender) {
|
||||
if (!(sender instanceof Player player)) {
|
||||
return Duration.ZERO;
|
||||
}
|
||||
|
||||
Map<UUID, Instant> nodeMap = cooldowns.get(node.getFullName());
|
||||
if (nodeMap == null) {
|
||||
return Duration.ZERO;
|
||||
}
|
||||
|
||||
Instant expiration = nodeMap.get(player.getUniqueId());
|
||||
if (expiration == null) {
|
||||
return Duration.ZERO;
|
||||
}
|
||||
|
||||
Instant now = Instant.now();
|
||||
if (now.isAfter(expiration)) {
|
||||
nodeMap.remove(player.getUniqueId());
|
||||
return Duration.ZERO;
|
||||
}
|
||||
|
||||
return Duration.between(now, expiration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Applique le temps de recharge configuré sur le nœud à l'émetteur.
|
||||
*
|
||||
* @param node Le nœud de commande.
|
||||
* @param sender L'émetteur.
|
||||
*/
|
||||
public void applyCooldown(CommandNode node, CommandSender sender) {
|
||||
if (!(sender instanceof Player player)) {
|
||||
return;
|
||||
}
|
||||
Duration duration = node.getCooldown();
|
||||
if (duration == null || duration.isZero() || duration.isNegative()) {
|
||||
return;
|
||||
}
|
||||
|
||||
cooldowns.computeIfAbsent(node.getFullName(), k -> new ConcurrentHashMap<>())
|
||||
.put(player.getUniqueId(), Instant.now().plus(duration));
|
||||
}
|
||||
|
||||
/**
|
||||
* Réinitialise / supprime le cooldown d'un joueur pour une commande donnée.
|
||||
*
|
||||
* @param node Le nœud de commande.
|
||||
* @param player Le joueur.
|
||||
*/
|
||||
public void resetCooldown(CommandNode node, Player player) {
|
||||
Map<UUID, Instant> nodeMap = cooldowns.get(node.getFullName());
|
||||
if (nodeMap != null) {
|
||||
nodeMap.remove(player.getUniqueId());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Nettoie l'ensemble des temps de recharge enregistrés.
|
||||
*/
|
||||
public void clearAll() {
|
||||
cooldowns.clear();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user