Files
GameModes-SpeedHG/src/main/kotlin/club/mcscrims/speedhg/ability/CooldownManager.kt
2025-12-01 19:45:49 +01:00

69 lines
1.6 KiB
Kotlin

package club.mcscrims.speedhg.ability
import org.bukkit.entity.Player
import java.util.UUID
import java.util.concurrent.ConcurrentHashMap
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 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 )
return false
}
return true
}
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
}
fun clearCooldown(
player: Player,
key: String
) {
cooldowns[player.uniqueId]?.remove( key )
}
fun clearAllCooldowns(
player: Player
) {
cooldowns.remove( player.uniqueId )
}
fun clearAll()
{
cooldowns.clear()
}
}