Add listener documentation
This commit is contained in:
47
guide/docs/extensions/listener.md
Normal file
47
guide/docs/extensions/listener.md
Normal file
@@ -0,0 +1,47 @@
|
||||
## Register an event listener
|
||||
|
||||
### Using the `listen` function
|
||||
|
||||
You can register an event listener by calling the following function anywhere you want.
|
||||
|
||||
```kotlin
|
||||
listen<EventClass> {
|
||||
doSomething()
|
||||
}
|
||||
```
|
||||
|
||||
The event instance will be passed in as `it`, but you can change this:
|
||||
|
||||
```kotlin
|
||||
listen<PlayerMoveEvent> { moveEvent ->
|
||||
moveEvent.player.kick("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:
|
||||
|
||||
```kotlin
|
||||
val moveEventListener = listen<PlayerMoveEvent> {
|
||||
it.player.kick("Do not move!")
|
||||
}
|
||||
|
||||
// e.g. unregister the listener after some time
|
||||
taskRunLater(20 * 5) {
|
||||
moveEventListener.unregister()
|
||||
}
|
||||
```
|
||||
|
||||
### Registering an existing `Listener` instance
|
||||
|
||||
There is an extension functions which registers a `Listener` instance:
|
||||
|
||||
```kotlin
|
||||
listenerInstance.register()
|
||||
```
|
||||
|
||||
## Unregister a `Listener`
|
||||
|
||||
Just call `listenerInstance.unregister()`
|
@@ -31,3 +31,5 @@ nav:
|
||||
- Starting a new project: setup/project.md
|
||||
- Setup using Gradle: setup/gradle.md
|
||||
- Entrypoint to your plugin: setup/entrypoint.md
|
||||
- Extensions:
|
||||
- Event listener: extensions/listener.md
|
||||
|
Reference in New Issue
Block a user