Event listener
Register an event listener¶
Using the listen
function¶
You can register an event listener by calling the following function anywhere you want.
listen<EventClass> {
doSomething()
}
The event instance will be passed in as it
, but you can change this:
listen<PlayerMoveEvent> { moveEvent ->
moveEvent.player.kick(literalText("Do not move!"))
broadcast("${moveEvent.player} moved :/")
}
The listen
function returns the Listener
instance, which allows you to perform operations on it later.
For example you could listen to a specific event temporarily:
val moveEventListener = listen<PlayerMoveEvent> {
it.player.kick(literalText("Do not move!"))
}
// e.g. unregister the listener after some time
taskRunLater(20 * 5) {
moveEventListener.unregister()
}
Register an existing Listener
instance¶
There is an extension function which registers a Listener
instance:
listenerInstance.register()
Unregister a Listener
¶
Just call listenerInstance.unregister()