All Downloads are FREE. Search and download functionalities are using the official Maven repository.

walkmc.DelayManager.kt Maven / Gradle / Ivy

package walkmc

import org.bukkit.entity.*
import walkmc.cache.*
import walkmc.extensions.*
import walkmc.time.*
import kotlin.time.*

/**
 * A simple cooldown registry used to delay some actions of players.
 */
object DelayManager : Cache() {
	
	/**
	 * Submits a new player cooldown to the specified key and delay.
	 */
	fun put(player: Player, key: String, duration: Duration): Delay {
		val delay = Delay(duration)
		put(NamespacedKey.of(player.uuid.toString(), key), delay)
		return delay
	}
	
	/**
	 * Locates a player cooldown with the specified key.
	 */
	fun locate(player: Player, key: String): Delay? = locate(NamespacedKey.of(player.uuid.toString(), key))
	
	/**
	 * Verifies if the cooldown of the specified player is expired.
	 * Will returns true if the player not contains a cooldown with the specified key.
	 */
	fun isExpired(player: Player, key: String): Boolean = locate(player, key)?.isExpired ?: true
	
	/**
	 * Removes a cooldown of the specified player.
	 */
	fun remove(player: Player, key: String) = remove(NamespacedKey.of(player.uuid.toString(), key))
	
	override fun get(key: NamespacedKey): Delay? = super.get(key)?.apply {
		if (isExpired)
			remove(key)
	}
	
	override fun locate(key: NamespacedKey): Delay? = super.locate(key)?.apply {
		if (isExpired)
			remove(key)
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy