Map

typealias WhenPlayerQuitMapCallback<V> = Player.(V) -> Unit

class OnlinePlayerMap<V>(override val plugin: Plugin) : HashMap<Player, V>(), KListener<Plugin> {

    /**
     * Puts a Player to the map with a [value] and a callback for when the player quits the server.
     */
    fun put(key: Player, value: V, whenPlayerQuit: WhenPlayerQuitMapCallback<V>): V? {
        whenQuit.put(key, whenPlayerQuit)
        return put(key, value).also {
            checkRegistration()
        }
    }

    /**
     * Removes the player from the map, calling the [WhenPlayerQuitMapCallback] provided.
     */
    fun quit(player: Player) {
        remove(player)?.also {
            whenQuit.remove(player)?.also { block ->
                block.invoke(player, it)
            }
            checkRegistration()
        }
    }

    /**
     * Clear the map calling all [WhenPlayerQuitMapCallback] from the Players.
     */
    fun clearQuiting() {
        keys.toMutableList().forEach {
            quit(it)
        }
    }
}

Creating new one

  • onlinePlayerMapOf(): Creates a new empty online player map.

  • onlinePlayerListOf(vararg players: Pair<Player, V>) : Create a new online player map with a list of players and the values.

  • onlinePlayerListOf(vararg pair: Triple<Player, V, WhenPlayerQuitMapCallback<V>): Create a new online player map with a Player and the value list in a Triple with the callback for when the Player disconnects from the Server.

Last updated