Cooldowns

class Cooldowns<K : Any>(timeSource: TimeSource = SystemTimeSource)

Manages cooldowns for keys of type K.

This class provides a way to track and enforce time-based intervals between actions associated with specific keys. It is thread-safe as it uses ConcurrentHashMap internally.

Parameters

K

the type of the key used to identify cooldowns

Constructors

Link copied to clipboard
constructor(timeSource: TimeSource = SystemTimeSource)

Functions

Link copied to clipboard
fun clear(key: K)

Clears the cooldown for the specified key.

Link copied to clipboard
fun clearAll()

Clears all cooldowns currently managed by this instance.

Link copied to clipboard
fun isReady(key: K): Boolean

Returns whether the specified key is ready to be used.

Link copied to clipboard

Periodically purges expired cooldowns and executes their callbacks.

Link copied to clipboard

Returns the remaining cooldown time in milliseconds for the specified key.

Link copied to clipboard
fun set(key: K, cooldownMillis: Long, onExpiry: () -> Unit = {})

Sets the cooldown for the specified key in milliseconds.

fun set(key: K, cooldown: Duration, onExpiry: () -> Unit = {})

Sets the cooldown for the specified key using a Duration.

Link copied to clipboard
fun tryUse(key: K, cooldownMillis: Long, onExpiry: () -> Unit = {}): Boolean

Tries to use the specified key with the given cooldownMillis.

fun tryUse(key: K, cooldown: Duration, onExpiry: () -> Unit = {}): Boolean

Tries to use the specified key with the given cooldown.

Link copied to clipboard
fun PlayerCooldowns.tryUse(player: Player, cooldown: Duration): Boolean

Tries to use the cooldown for the specified player with the given cooldown.