-
Notifications
You must be signed in to change notification settings - Fork 18
Event as a lambda
Gabriel Souza edited this page Jan 8, 2020
·
4 revisions
The Event lambda is an Extension for Bukkit events simple to use. The goal is to replace the common event way, that you create a function, with an event parameter and @EventHandler annotation.
// MyClassListener : Listener
@EventHandler
fun onPlayerPickupItem(event: PlayerPickupItemEvent) {
event.isCancelled = true
}
// in Main class
pluginManager.registerEvents(MyClassListener())*The events is auto registered
// MyClassListener : Listener
event<PlayerPickupItemEvent>(myPlugin) {
isCancelled = true
}inline fun <reified T : Event> Listener.event(
plugin: Plugin,
priority: EventPriority = EventPriority.NORMAL,
ignoreCancelled: Boolean = true,
crossinline block: T.() -> Unit
)If you don't want to create a Listener class you can use the events lambda.
val listener = myPlugin.events {
event<PlayerPickupItemEvent> {
isCancelled = true
}
}In the case of events you don't need to add your plugin as a parameter, because the lambda block from events is an Extension from KListener.
inline fun Plugin.events(
block: KListener<*>.() -> Unit
)With this interface you can use event lambda without Plugin parameter.
class MyListenerClass(override val plugin: MyPlugin) : KListener<MyPlugin> {
init {
event<PlayerPickupItemEvent> {
isCancelled = true
}
}
}You can find the implementation and more of Event extensions here