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>

public class KVTickCoolDownHandler<T,K> extends Object implements 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

    Constructors
    Constructor
    Description
    KVTickCoolDownHandler(org.bukkit.plugin.java.JavaPlugin executingPlugin, long cooldown)
    Constructs a new KVTickCoolDownHandler instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Cleans up expired cooldown entries.
    long
    Gets the remaining cooldown for the specified object and action.
    long
    Gets the specific cooldown duration for a given object and action.
    long
    Returns the total maximum cooldown duration for any action in this handler.
    boolean
    onCoolDown(T t, K k)
    Checks whether the given object is currently on cooldown for a specific action identified by the key.
    void
    putOnCoolDown(T t, K k)
    Puts the specified object on cooldown for a specific action identified by the key.
    void
    Removes the cooldown for the specified object and action.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • KVTickCoolDownHandler

      public KVTickCoolDownHandler(org.bukkit.plugin.java.JavaPlugin executingPlugin, long cooldown)
      Constructs a new KVTickCoolDownHandler instance.
      Parameters:
      executingPlugin - The Bukkit plugin instance executing this handler.
      cooldown - The total duration of the cooldown in ticks.
  • Method Details

    • putOnCoolDown

      public void putOnCoolDown(T t, K k)
      Description copied from interface: IKVCoolDownHandler
      Puts 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:
      putOnCoolDown in interface IKVCoolDownHandler<T,K>
      Parameters:
      t - Object to set cooldown for.
      k - Key identifying the type of cooldown.
    • getSpecificCoolDown

      public long getSpecificCoolDown(T t, K k)
      Description copied from interface: IKVCoolDownHandler
      Gets 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:
      getSpecificCoolDown in interface IKVCoolDownHandler<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

      public boolean onCoolDown(T t, K k)
      Description copied from interface: IKVCoolDownHandler
      Checks 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:
      onCoolDown in interface IKVCoolDownHandler<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

      public long getRemainingCoolDown(T t, K k)
      Description copied from interface: IKVCoolDownHandler
      Gets 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:
      getRemainingCoolDown in interface IKVCoolDownHandler<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: IKVCoolDownHandler
      Returns 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:
      getTotalCoolDown in interface IKVCoolDownHandler<T,K>
      Returns:
      The maximum cooldown duration in this handler.
    • removeCooldown

      public void removeCooldown(T t, K k)
      Description copied from interface: IKVCoolDownHandler
      Removes the cooldown for the specified object and action.

      Usage Example:

       
       playerActionCooldown.removeCooldown(player, "jump");
       
       
      Specified by:
      removeCooldown in interface IKVCoolDownHandler<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.