Class CooldownValidator
- All Implemented Interfaces:
CommandValidator,PlayerCacheSaver,PlayerCacheManager.ExpiringPlayerCache
- Since:
- 6.2.0
- Version:
- 2.0.0
- Author:
- wisdomme
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.ultikits.ultitools.abstracts.command.validation.CommandValidator
CommandValidator.ValidationResult -
Field Summary
FieldsModifier and TypeFieldDescriptionMap of player UUID -> (method name -> cooldown end timestamp)private final intprivate static final Loggerprivate static final intprivate booleanTrue once this instance has registeredcooldownswith the livePlayerCacheManagerfor quit-based and time-based sweeping (GEN-08, D-03). -
Constructor Summary
ConstructorsConstructorDescriptionCreates a cooldown validator with no default cooldown.CooldownValidator(int defaultCooldownSeconds) Creates a cooldown validator with a default cooldown. -
Method Summary
Modifier and TypeMethodDescriptionvoidapplyCooldown(CommandContext context) Applies cooldown after command execution.static StringbindingKey(CmdCD cmdCD) The cache key of a config-bound@CmdCD: its config class and key.voidCleans up expired cooldowns to prevent memory leaks.voidclearCooldown(UUID playerId, String methodKey) Clears a specific cooldown for a player.voidclearCooldowns(UUID playerId) Clears all cooldowns for a player.private voidAttempts lazy first-use registration of this instance with the livePlayerCacheManagersingleton.private IntegergetCooldownSeconds(Method method, CommandContext context) getName()Gets the name of this validator for debugging and logging purposes.intgetOrder()Gets the order of this validator.longgetRemainingCooldown(UUID playerId, String methodKey) Gets the remaining cooldown time in seconds.private static booleanisBoundMapping(Method method, CommandContext context) voidonComplete(CommandContext context, boolean commandSucceeded) Post-action hook that applies the cooldown recorded for this invocation.private static voidreportUnresolvedBinding(CommandContext context, IllegalStateException cause) voidsavePlayerData(UUID playerId) PlayerCacheSaverhook: fired byPlayerCacheManager.onPlayerQuit(UUID)("the quit sweep") for the quitting player -- before the generic@PlayerCachefield sweep removes the (by then already-empty)cooldownsentry.voidPlayerCacheManager.ExpiringPlayerCacheopt-in: delegates to the pre-existingcleanupExpired()time-based half.unresolvedBinding(CommandContext context, Method method) Fails the command closed for a config-bound@CmdCDthis validator never had resolved -- reachable only when aCooldownValidatoris added after the module loaded, or an executor is registered outside the module loader.validate(CommandContext context) Validates the command context.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.ultikits.ultitools.abstracts.command.validation.CommandValidator
shouldValidate
-
Field Details
-
ORDER
private static final int ORDER- See Also:
-
LOGGER
-
cooldowns
Map of player UUID -> (method name -> cooldown end timestamp)saveBeforeRemove = truesosavePlayerData(UUID)-- which delegates to the pre-existingclearCooldowns(UUID)-- fires on quit; see that method's javadoc for why (GEN-08, D-03). -
playerCacheRegistered
private volatile boolean playerCacheRegisteredTrue once this instance has registeredcooldownswith the livePlayerCacheManagerfor quit-based and time-based sweeping (GEN-08, D-03).Set from
validate(CommandContext)-- the first production call this validator receives after construction -- rather than from the constructor: a barenew CooldownValidator()(the shape a unit test, orBaseCommandExecutor's own constructor, uses) must never even attempt contact with a core plugin that may not exist yet. Guarding on a liveUltiTools.getInstance()rather than an unconditional "did I try" flag means a first attempt made before the core plugin is up is retried on the next call instead of being permanently abandoned --PlayerCacheManager.tryRegister(java.lang.Object)is itself idempotent per instance, so a redundant retry after the flag is already true is simply never made. -
defaultCooldownSeconds
private final int defaultCooldownSeconds
-
-
Constructor Details
-
CooldownValidator
public CooldownValidator()Creates a cooldown validator with no default cooldown. -
CooldownValidator
public CooldownValidator(int defaultCooldownSeconds) Creates a cooldown validator with a default cooldown.- Parameters:
defaultCooldownSeconds- the default cooldown in seconds
-
-
Method Details
-
validate
Description copied from interface:CommandValidatorValidates the command context.- Specified by:
validatein interfaceCommandValidator- Parameters:
context- the command context to validate- Returns:
- the validation result
-
applyCooldown
Applies cooldown after command execution. Should be called after successful command execution.- Parameters:
context- the command context
-
onComplete
Post-action hook that applies the cooldown recorded for this invocation. Delegates toapplyCooldown(CommandContext)and is invoked only by a chain that actually ran this validator for the current dispatch -- seeCommandValidator.onComplete(CommandContext, boolean).- Specified by:
onCompletein interfaceCommandValidator- Parameters:
context- the command contextcommandSucceeded- ignored -- the cooldown applies whether the mapped method succeeded or threw- Since:
- 6.3.0
-
ensurePlayerCacheRegistered
private void ensurePlayerCacheRegistered()Attempts lazy first-use registration of this instance with the livePlayerCacheManagersingleton. Safe to call unconditionally on everyvalidate(CommandContext)invocation: a no-op onceplayerCacheRegisteredis true, and a cheap, safely-no-op-on-failure retry otherwise (see that field's javadoc). -
sweepExpired
public void sweepExpired()PlayerCacheManager.ExpiringPlayerCacheopt-in: delegates to the pre-existingcleanupExpired()time-based half. Invoked once perPlayerCacheManager.sweepExpiredEntries()pass -- the periodic sweep this instance only receives once registered (seeensurePlayerCacheRegistered()) -- independently of whether any player has quit (GEN-08, D-03).- Specified by:
sweepExpiredin interfacePlayerCacheManager.ExpiringPlayerCache- Since:
- 6.3.0
-
savePlayerData
PlayerCacheSaverhook: fired byPlayerCacheManager.onPlayerQuit(UUID)("the quit sweep") for the quitting player -- before the generic@PlayerCachefield sweep removes the (by then already-empty)cooldownsentry. Delegates to the pre-existingclearCooldowns(UUID)rather than re-deriving its predicate, giving that previously zero-caller method a real, quit-path-reached production call site (GEN-08). The interface is named for persistence, but its contract is simply "run this before the generic removal" -- there is nothing here to persist, and reusing the hook for an idempotent, already-correct cleanup method is the same predicate the generic sweep would otherwise perform standalone.- Specified by:
savePlayerDatain interfacePlayerCacheSaver- Parameters:
playerId- the UUID of the player quitting- Since:
- 6.3.0
-
clearCooldowns
Clears all cooldowns for a player.- Parameters:
playerId- the player's UUID
-
clearCooldown
Clears a specific cooldown for a player.- Parameters:
playerId- the player's UUIDmethodKey- the method key
-
getRemainingCooldown
Gets the remaining cooldown time in seconds.- Parameters:
playerId- the player's UUIDmethodKey- the method key- Returns:
- remaining seconds, or 0 if not on cooldown
-
cleanupExpired
public void cleanupExpired()Cleans up expired cooldowns to prevent memory leaks. Should be called periodically. -
isBoundMapping
- Returns:
- whether the
@CmdCDthat governsmethodis bound to a config key
-
getCooldownSeconds
-
unresolvedBinding
Fails the command closed for a config-bound@CmdCDthis validator never had resolved -- reachable only when aCooldownValidatoris added after the module loaded, or an executor is registered outside the module loader. Treating it as "no cooldown" would be a silent no-op, and throwing would escapeonCommandas Bukkit's generic internal error; instead the player gets the same message shape as a failed command, and the cause is logged at SEVERE and sent to the error report collector like any other command failure (#531 gate-1 WR-02). -
reportUnresolvedBinding
-
bindingKey
The cache key of a config-bound@CmdCD: its config class and key.- Parameters:
cmdCD- the annotation- Returns:
configClassName#key, ornullwhencmdCDis unbound- Since:
- 6.3.0
-
getOrder
public int getOrder()Description copied from interface:CommandValidatorGets the order of this validator. Lower values are executed first.- Specified by:
getOrderin interfaceCommandValidator- Returns:
- the order value
-
getName
Description copied from interface:CommandValidatorGets the name of this validator for debugging and logging purposes.- Specified by:
getNamein interfaceCommandValidator- Returns:
- the validator name
-