Package dev.demeng.pluginbase.cooldown
Class CooldownMap<T>
java.lang.Object
dev.demeng.pluginbase.cooldown.CooldownMap<T>
- Type Parameters:
T- The type
A self-populating map of cooldown instances.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> @NotNull CooldownMap<T>Creates a new collection with the cooldown properties defined by the base instance.longReturns the number of milliseconds that have elapsed since the cooldown for the given key was last tested.@NotNull CooldownGets the internal cooldown instance associated with the given key.getAll()Gets the cooldowns contained within this collection.@NotNull OptionalLonggetLastTested(T key) Returns the timestamp at which the cooldown for the given key was last tested, as milliseconds since the epoch.voidStores a custom cooldown instance for the given key, replacing any existing entry.longremainingMillis(T key) Returns the number of milliseconds remaining on the cooldown for the given key.longremainingTime(T key, @NotNull TimeUnit unit) Returns the remaining cooldown time for the given key expressed in the specified time unit.voidResets the cooldown for the given key so that it behaves as if it was never tested.voidsetLastTested(T key, long time) Manually sets the last tested timestamp for the cooldown associated with the given key.booleanTests whether the given key is off cooldown and, if so, records the current time as the last tested instant.booleantestSilently(T key) Tests whether the given key is off cooldown without recording the current time.
-
Method Details
-
create
Creates a new collection with the cooldown properties defined by the base instance.- Parameters:
base- The cooldown to base off- Returns:
- A new collection
-
get
Gets the internal cooldown instance associated with the given key.The inline Cooldown methods in this class should be used to access the functionality of the cooldown as opposed to calling the methods directly via the instance returned by this method.
- Parameters:
key- The key- Returns:
- A cooldown instance
-
put
Stores a custom cooldown instance for the given key, replacing any existing entry.The provided cooldown must have the same duration as the base cooldown, since the expiring cache is configured around that duration.
- Parameters:
key- The key to associate the cooldown withcooldown- The cooldown instance to store- Throws:
IllegalArgumentException- If the cooldown's duration differs from the base cooldown's duration
-
getAll
Gets the cooldowns contained within this collection.- Returns:
- The backing map
-
test
Tests whether the given key is off cooldown and, if so, records the current time as the last tested instant. Delegates toCooldown.test().- Parameters:
key- The key to test- Returns:
trueif the key is not currently on cooldown
-
testSilently
Tests whether the given key is off cooldown without recording the current time. Use this when you need to check cooldown status without consuming it. Delegates toCooldown.testSilently().- Parameters:
key- The key to test- Returns:
trueif the key is not currently on cooldown
-
elapsed
Returns the number of milliseconds that have elapsed since the cooldown for the given key was last tested. Delegates toCooldown.elapsed().- Parameters:
key- The key to check- Returns:
- Milliseconds elapsed since the last test
-
reset
Resets the cooldown for the given key so that it behaves as if it was never tested. Delegates toCooldown.reset().- Parameters:
key- The key whose cooldown should be reset
-
remainingMillis
Returns the number of milliseconds remaining on the cooldown for the given key. Returns0if the key is not currently on cooldown. Delegates toCooldown.remainingMillis().- Parameters:
key- The key to check- Returns:
- Remaining cooldown time in milliseconds, or
0if not on cooldown
-
remainingTime
Returns the remaining cooldown time for the given key expressed in the specified time unit. Returns0if the key is not currently on cooldown. Delegates toCooldown.remainingTime(TimeUnit).- Parameters:
key- The key to checkunit- The time unit to express the result in- Returns:
- Remaining cooldown time in the given unit, or
0if not on cooldown
-
getLastTested
Returns the timestamp at which the cooldown for the given key was last tested, as milliseconds since the epoch. Returns an emptyOptionalLongif the cooldown has never been tested. Delegates toCooldown.getLastTested().- Parameters:
key- The key to check- Returns:
- The last tested timestamp, or an empty optional if never tested
-
setLastTested
Manually sets the last tested timestamp for the cooldown associated with the given key. Delegates toCooldown.setLastTested(long).- Parameters:
key- The key whose cooldown should be updatedtime- The timestamp to set, as milliseconds since the epoch
-