Class KVTickCoolDownHandler<T,K>
java.lang.Object
tk.airshipcraft.commonlib.utils.cooldowns.KVTickCoolDownHandler<T,K>
- Type Parameters:
T- The type of objects that cooldowns are assigned to.K- The type of key identifying different cooldowns.
- All Implemented Interfaces:
IKVCoolDownHandler<T,K>
Implementation of
IKVCoolDownHandler using Minecraft ticks as the time unit.
Allows managing cooldowns for different types of actions or events, identified by unique keys, for various objects.- Since:
- 2023-10-11
- Version:
- 1.0.1
- Author:
- Maxopoly, notzune
-
Constructor Summary
ConstructorsConstructorDescriptionKVTickCoolDownHandler(org.bukkit.plugin.java.JavaPlugin executingPlugin, long cooldown) Constructs a newKVTickCoolDownHandlerinstance. -
Method Summary
Modifier and TypeMethodDescriptionvoidCleans up expired cooldown entries.longgetRemainingCoolDown(T t, K k) Gets the remaining cooldown for the specified object and action.longgetSpecificCoolDown(T t, K k) Gets the specific cooldown duration for a given object and action.longReturns the total maximum cooldown duration for any action in this handler.booleanonCoolDown(T t, K k) Checks whether the given object is currently on cooldown for a specific action identified by the key.voidputOnCoolDown(T t, K k) Puts the specified object on cooldown for a specific action identified by the key.voidremoveCooldown(T t, K k) Removes the cooldown for the specified object and action.
-
Constructor Details
-
KVTickCoolDownHandler
public KVTickCoolDownHandler(org.bukkit.plugin.java.JavaPlugin executingPlugin, long cooldown) Constructs a newKVTickCoolDownHandlerinstance.- Parameters:
executingPlugin- The Bukkit plugin instance executing this handler.cooldown- The total duration of the cooldown in ticks.
-
-
Method Details
-
putOnCoolDown
Description copied from interface:IKVCoolDownHandlerPuts the specified object on cooldown for a specific action identified by the key.Usage Example:
IKVCoolDownHandler<Player, String> playerActionCooldown = ...; playerActionCooldown.putOnCoolDown(player, "jump");- Specified by:
putOnCoolDownin interfaceIKVCoolDownHandler<T,K> - Parameters:
t- Object to set cooldown for.k- Key identifying the type of cooldown.
-
getSpecificCoolDown
Description copied from interface:IKVCoolDownHandlerGets the specific cooldown duration for a given object and action. This method is useful for retrieving individual cooldown times when an object has multiple cooldowns for different actions.Usage Example:
long actionCooldown = playerActionCooldown.getSpecificCoolDown(player, "jump"); System.out.println("Specific cooldown for jumping: " + actionCooldown);- Specified by:
getSpecificCoolDownin interfaceIKVCoolDownHandler<T,K> - Parameters:
t- The object to get the cooldown for.k- The key identifying the cooldown.- Returns:
- The specific cooldown duration for the given action, or 0 if not on cooldown.
-
onCoolDown
Description copied from interface:IKVCoolDownHandlerChecks whether the given object is currently on cooldown for a specific action identified by the key.Usage Example:
if (playerActionCooldown.onCoolDown(player, "jump")) { System.out.println("Player is on cooldown for jumping!"); }- Specified by:
onCoolDownin interfaceIKVCoolDownHandler<T,K> - Parameters:
t- Object to check.k- Key identifying the type of cooldown.- Returns:
- True if the object is on cooldown for the specified key, false otherwise.
-
getRemainingCoolDown
Description copied from interface:IKVCoolDownHandlerGets the remaining cooldown for the specified object and action. The meaning and scale (like ticks or seconds) of the returned value are dependent on the implementation. It should not be less than 0 or exceed the maximum cooldown.Usage Example:
long remainingCooldown = playerActionCooldown.getRemainingCoolDown(player, "jump"); System.out.println("Remaining cooldown for jumping: " + remainingCooldown);- Specified by:
getRemainingCoolDownin interfaceIKVCoolDownHandler<T,K> - Parameters:
t- Object to get cooldown for.k- Key identifying the type of cooldown.- Returns:
- Remaining cooldown time for the specified action, or 0 if not on cooldown.
-
getTotalCoolDown
public long getTotalCoolDown()Description copied from interface:IKVCoolDownHandlerReturns the total maximum cooldown duration for any action in this handler. The meaning of this duration is specific to the implementation.Usage Example:
long totalCooldown = playerActionCooldown.getTotalCoolDown(); System.out.println("Total cooldown duration for any action: " + totalCooldown);- Specified by:
getTotalCoolDownin interfaceIKVCoolDownHandler<T,K> - Returns:
- The maximum cooldown duration in this handler.
-
removeCooldown
Description copied from interface:IKVCoolDownHandlerRemoves the cooldown for the specified object and action.Usage Example:
playerActionCooldown.removeCooldown(player, "jump");- Specified by:
removeCooldownin interfaceIKVCoolDownHandler<T,K> - Parameters:
t- Object to remove cooldown for.k- Key identifying the type of cooldown.
-
cleanupExpiredEntries
public void cleanupExpiredEntries()Cleans up expired cooldown entries. This method should be called periodically to prevent memory leaks. It removes any entries that have exceeded their cooldown period.
-