Class CooldownManager

java.lang.Object
com.petarmc.petarlib.cooldowns.CooldownManager

public class CooldownManager extends Object
Per-player cooldowns. Stores expiration timestamps (System.currentTimeMillis) and cleans expired entries on read.
  • Constructor Details

    • CooldownManager

      public CooldownManager()
  • Method Details

    • isOnCooldown

      public boolean isOnCooldown(UUID playerId, String cooldownId)
      Checks if the given player is still on the specified cooldown.
      Parameters:
      playerId - player UUID
      cooldownId - logical cooldown identifier
      Returns:
      true if cooldown exists and has not expired
    • isOnCooldown

      public boolean isOnCooldown(org.bukkit.entity.Player player, String cooldownId)
      Player overload of isOnCooldown(UUID, String).
      Parameters:
      player - Bukkit player (ignored if null)
      cooldownId - logical cooldown identifier
      Returns:
      true if cooldown exists and has not expired
    • getRemaining

      public long getRemaining(UUID playerId, String cooldownId)
      Returns remaining cooldown time in milliseconds for the given player and ID. Expired or missing cooldowns return 0 and are pruned from storage. Calls CooldownEndEvent when an expiration is detected.
      Parameters:
      playerId - player UUID
      cooldownId - logical cooldown identifier
      Returns:
      remaining time in ms, or 0 if expired/missing
    • getRemaining

      public long getRemaining(org.bukkit.entity.Player player, String cooldownId)
      Player overload of getRemaining(UUID, String).
      Parameters:
      player - Bukkit player (ignored if null)
      cooldownId - logical cooldown identifier
      Returns:
      remaining time in ms, or 0 if expired/missing
    • setCooldown

      public void setCooldown(UUID playerId, String cooldownId, long durationMs)
      Sets a cooldown for the given player and ID using the supplied duration. Calls CooldownStartEvent; if cancelled, no cooldown is stored.
      Parameters:
      playerId - player UUID
      cooldownId - logical cooldown identifier
      durationMs - duration in milliseconds (ignored if non-positive)
    • setCooldown

      public void setCooldown(org.bukkit.entity.Player player, String cooldownId, long durationMs)
      Parameters:
      player - Bukkit player (ignored if null)
      cooldownId - logical cooldown identifier
      durationMs - duration in milliseconds (ignored if non-positive)
    • removeCooldown

      public void removeCooldown(UUID playerId, String cooldownId)
      Removes a specific cooldown for a player, if present. Does not call end events; intended for manual invalidation.
      Parameters:
      playerId - player UUID
      cooldownId - logical cooldown identifier
    • checkAndTriggerEnd

      public boolean checkAndTriggerEnd(UUID playerId, String cooldownId)
      Checks a single cooldown for expiry, calls CooldownEndEvent, and removes it if expired.
      Parameters:
      playerId - player UUID
      cooldownId - logical cooldown identifier
      Returns:
      true if cooldown existed and was expired/removed
    • checkAndTriggerEnd

      public boolean checkAndTriggerEnd(org.bukkit.entity.Player player, String cooldownId)
      Parameters:
      player - Bukkit player (ignored if null)
      cooldownId - logical cooldown identifier
      Returns:
      true if cooldown existed and was expired/removed
    • formatRemaining

      public static String formatRemaining(long ms)
      Formats a remaining duration into mm:ss (zero-padded) format.
      Parameters:
      ms - duration in milliseconds
      Returns:
      formatted time, or 00:00 when non-positive