Map

ExpirationMap have commum Map operations with a plus of some fuctions.

typealias OnExpireMapCallback<K, V> = (K, V) -> Unit

interface ExpirationMap<K, V> : MutableMap<K, V>, WithPlugin<Plugin> {
    /**
     * Returns the missing time on seconds to expire the key,
     * -1 if was not specified the expiration time before(permanent key) or
     * `null` if the key is not contained in the map.
     */
    fun missingTime(key: K): Long?

    /**
     * Set expiration time to the key and returns `true` if the key is found
     * or false if the key is not contained in the map.
     */
    fun expire(key: K, time: Long): Boolean

    /**
     * Set expiration time to the key and returns `true` if the key is found
     * or false if the key is not contained in the map.
     *
     * [time] in seconds.
     * [callback] is called when the key expires.
     */
    fun expire(key: K, time: Long, callback: OnExpireMapCallback<K, V>): Boolean

    /**
     * Associates the specified [value] with the specified [key] in the map
     * with an expiration time.
     *
     * @return the previous value associated with the key, or `null` if the key was not present in the map.
     */
    fun put(key: K, value: V, time: Long): V?

    /**
     * Associates the specified [value] with the specified [key] in the map
     * with an expiration time.
     *
     * [time] in seconds.
     * [callback] is called when the key expires.
     *
     * @return the previous value associated with the key, or `null` if the key was not present in the map.
     */
    fun put(key: K, value: V, time: Long, callback: OnExpireMapCallback<K, V>): V?
}

Creating one

ExpirationMap uses Bukkit tasks to expire the values inside, in this case, a Plugin is needed (you will find extension to WITHPLUGIN INTERFACE as well).

  • expirationMapOf(): Empty expiration map

  • expirationMapOf(expireTime: Long, vararg elements: Pair<K, V>): Expiration map with a list of element that will expire in the time provided (in seconds)

  • expirationMapOf(expireTime: Long, vararg elements: Triple<K, V, OnExpireMapCallback<K, V>>): Expiration map with a list of element in Triple togther with the OnExpireCallback.

Last updated