<file_sep>package ru.ovrays.example

import ru.ovrays.bukkitcooldowns.Cooldowns

import org.bukkit.entity.Player
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.entity.EntityDamageEvent
import org.bukkit.event.player.PlayerChangedWorldEvent
import org.bukkit.plugin.java.JavaPlugin



class ExamplePlugin : JavaPlugin(), Listener {
    override fun onEnable() {
        this.server.pluginManager.registerEvents(this, this)
    }

    @EventHandler
    fun onWorldChange(e: PlayerChangedWorldEvent) {
        Cooldowns.setCooldown(e.player, "Protect", 5000) // Time in ms
    }

    @EventHandler
    fun onEntityDamage(e: EntityDamageEvent) {
        if(e.entity !is Player) return
        val player = e.entity as Player
        if (Cooldowns.tryCooldown(player, "Protect")) {
            // This code will be executed if cooldown does not exist or expired
        } else {
            e.isCancelled = true
            player.sendMessage("You are protected from being damaged! (${(Cooldowns.getCooldown(player, "ProtectCooldown") as Long / 1000)} seconds left)")
        }
    }
}
<file_sep># BukkitCooldowns
Cooldown system for Bukkit/Spigot 1.12.2+
<file_sep>package ru.ovrays.bukkitcooldowns

import com.google.common.collect.HashBasedTable
import org.bukkit.entity.Player



object Cooldowns {
    private val cooldowns = HashBasedTable.create<Any, Any, Any>()

    /**
     * Update a cooldown for the specified player.
     * @param player - the player.
     * @param key - cooldown to update.
     * @param delay - number of milliseconds until the cooldown will expire again.
     * @return The previous number of milliseconds until expiration.
     */
    fun setCooldown(player: Player, key: String, delay: Long): Long {
        return calculateRemainder(
            cooldowns.put(player.name, key, System.currentTimeMillis() + delay) as Long?
        )
    }

    /**
     * Retrieve the number of milliseconds left until a given cooldown expires.
     *
     *
     * Check for a negative value to determine if a given cooldown has expired. <br></br>
     * Cooldowns that have never been defined will return [Long.MIN_VALUE].
     * @param player - the player.
     * @param key - cooldown to locate.
     * @return Number of milliseconds until the cooldown expires.
     */
    fun getCooldown(player: Player, key: String): Long? {
        return calculateRemainder(cooldowns.get(player.name, key) as Long? ?: return null)
    }

    /**
     * Determine if a given cooldown has expired. If it has, refresh the cooldown. If not, do nothing.
     * @param player - the player.
     * @param key - cooldown to update.
     * @return TRUE if the cooldown was expired/unset, FALSE otherwise.
     */
    fun tryCooldown(player: Player, key: String): Boolean {
        val cooldown = getCooldown(player, key)

        return if(cooldown != null)
            cooldown <= 0
        else
            true
    }

    private fun calculateRemainder(expireTime: Long?): Long {
        return if (expireTime != null) expireTime - System.currentTimeMillis() else java.lang.Long.MIN_VALUE
    }
}


