-
Notifications
You must be signed in to change notification settings - Fork 20
Online Player Collections
Gabriel Souza edited this page Aug 8, 2020
·
2 revisions
Online player collections is a set of collections that can be used to store online players, when the Player quits the server it's removed from the collection, calling (if specified) the WhenPlayerQuitCollectionCallback
.
Because it used Bukkit events, the functions extends Plugin (There is extension for WithPlugin as well).
typealias WhenPlayerQuitCollectionCallback = Player.() -> Unit
interface OnlinePlayerCollection : MutableCollection<Player>, KListener<Plugin> {
/**
* Adds a new Player to the collection with a callback for when the player quits the server.
*/
fun add(player: Player, whenPlayerQuit: WhenPlayerQuitCollectionCallback): Boolean {
return add(player).also {
if(it) checkRegistration()
}
}
/**
* Removes the player from the collection, calling the [WhenPlayerQuitCollectionCallback] provided.
*/
fun quit(player: Player): Boolean {
return remove(player).also {
if(it) checkRegistration()
}
}
/**
* Clear the collection calling all [WhenPlayerQuitCollectionCallback] from the Players.
*/
fun clearQuiting() {
toMutableList().forEach {
quit(it)
}
}
}
-
onlinePlayerListOf()
&onlinePlayerSetOf()
: Creates a new empty online player list or set. -
onlinePlayerListOf(vararg players: Player)
&onlinePlayerSetOf(vararg players: Player)
: Create a new online player list or set with a list of players. -
onlinePlayerListOf(vararg pair: Pair<Player, WhenPlayerQuitCollectionCallback>)
&onlinePlayerSetOf(vararg pair: Pair<Player, WhenPlayerQuitCollectionCallback>)
: Create a new online player list or set with a player list in a Pair with the callback for when the Player disconnects from the Server.
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)
}
}
}
-
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.