Add listener documentation

This commit is contained in:
Jakob K
2021-04-23 21:43:47 +00:00
parent 71aa0b5988
commit 6823a608c6
2 changed files with 49 additions and 0 deletions

View 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()`

View File

@@ -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