Cleanup ability context

This commit is contained in:
Laurin
2025-12-01 19:45:49 +01:00
parent 7634694b2e
commit ac185e35ce
5 changed files with 147 additions and 73 deletions

View File

@@ -8,43 +8,61 @@ class CooldownManager {
private val cooldowns = ConcurrentHashMap<UUID, ConcurrentHashMap<String, Long>>()
fun startCooldown(player: Player, key: String, seconds: Int) {
val expiryTime = System.currentTimeMillis() + (seconds * 1000L)
cooldowns.computeIfAbsent(player.uniqueId) { ConcurrentHashMap() }[key] = expiryTime
fun startCooldown(
player: Player,
key: String,
seconds: Int
) {
val expiryTime = System.currentTimeMillis() + ( seconds * 1000L )
cooldowns.computeIfAbsent( player.uniqueId ) { ConcurrentHashMap() }[ key ] = expiryTime
}
fun isOnCooldown(player: Player, key: String): Boolean {
fun isOnCooldown(
player: Player,
key: String
): Boolean
{
val playerCooldowns = cooldowns[player.uniqueId] ?: return false
val expiryTime = playerCooldowns[key] ?: return false
if (System.currentTimeMillis() >= expiryTime) {
playerCooldowns.remove(key)
if (playerCooldowns.isEmpty()) {
cooldowns.remove(player.uniqueId)
}
if ( System.currentTimeMillis() >= expiryTime )
{
playerCooldowns.remove( key )
if ( playerCooldowns.isEmpty() ) cooldowns.remove( player.uniqueId )
return false
}
return true
}
fun getRemainingSeconds(player: Player, key: String): Double {
fun getRemainingSeconds(
player: Player,
key: String
): Double
{
val playerCooldowns = cooldowns[player.uniqueId] ?: return 0.0
val expiryTime = playerCooldowns[key] ?: return 0.0
val remaining = (expiryTime - System.currentTimeMillis()) / 1000.0
return if (remaining > 0) remaining else 0.0
val remaining = ( expiryTime - System.currentTimeMillis() ) / 1000.0
return if ( remaining > 0 ) remaining else 0.0
}
fun clearCooldown(player: Player, key: String) {
cooldowns[player.uniqueId]?.remove(key)
fun clearCooldown(
player: Player,
key: String
) {
cooldowns[player.uniqueId]?.remove( key )
}
fun clearAllCooldowns(player: Player) {
cooldowns.remove(player.uniqueId)
fun clearAllCooldowns(
player: Player
) {
cooldowns.remove( player.uniqueId )
}
fun clearAll() {
fun clearAll()
{
cooldowns.clear()
}
}