From cf00d317689f13146f1db4a569a44bea1865659a Mon Sep 17 00:00:00 2001 From: Josh Date: Mon, 30 Oct 2023 17:10:02 +0000 Subject: [PATCH] feat: Make event function return TwilightListener to allow for unregistering, add event javadoc, bump ver 17 --- README.md | 6 +-- build.gradle.kts | 2 +- .../kotlin/gg/flyte/twilight/event/Event.kt | 41 ++++++++++++++----- .../kotlin/gg/flyte/twilight/EventTest.kt | 4 +- 4 files changed, 37 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index acbe540..a7748b6 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Maven gg.flyte twilight - 1.0.16 + 1.0.17 ``` @@ -29,14 +29,14 @@ maven { url "https://repo.flyte.gg/releases" } -implementation "gg.flyte:twilight:1.0.16" +implementation "gg.flyte:twilight:1.0.17" ``` Gradle (Kotlin DSL) ```kotlin maven("https://repo.flyte.gg/releases") -implementation("gg.flyte:twilight:1.0.16") +implementation("gg.flyte:twilight:1.0.17") ``` Certain features of Twilight require configuration, which can be done via the Twilight class. To setup a Twilight class instance, you can use the `twilight` method as shown below: diff --git a/build.gradle.kts b/build.gradle.kts index ca0ee76..32ed732 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,7 +5,7 @@ plugins { } group = "gg.flyte" -version = "1.0.16" +version = "1.0.17" repositories { mavenCentral() diff --git a/src/main/kotlin/gg/flyte/twilight/event/Event.kt b/src/main/kotlin/gg/flyte/twilight/event/Event.kt index 249dc00..ddf5d46 100644 --- a/src/main/kotlin/gg/flyte/twilight/event/Event.kt +++ b/src/main/kotlin/gg/flyte/twilight/event/Event.kt @@ -3,20 +3,39 @@ package gg.flyte.twilight.event import gg.flyte.twilight.Twilight import org.bukkit.event.Event import org.bukkit.event.EventPriority +import org.bukkit.event.HandlerList import org.bukkit.event.Listener +/** + * Creates a Bukkit event listener and registers it to listen for a specific type of event. + * + * This function simplifies the process of creating and registering a Bukkit event listener. + * + * @param T The type of event to listen for, specified as a reified type parameter. + * @param priority The priority at which the listener should be called, default is EventPriority.NORMAL. + * @param callback A lambda function to be executed when the specified event occurs. + * @return A `TwilightListener` instance, which can be used to later unregister the listener. + */ inline fun event( priority: EventPriority = EventPriority.NORMAL, crossinline callback: T.() -> Unit, -) { - Twilight.plugin.server.pluginManager.registerEvent( - T::class.java, // tell bukkit what event we're listening to - object: Listener {}, // the listener but we don't need it - priority, // the event priority, can be changed in the code but by default its 'normal' - { _, event -> // the 'opening' of the event executor - if (event is T) callback(event) // event executor body - }, - Twilight.plugin, // pass the instance to bukkit, so it knows what plugin should listen to the event - true - ) +): TwilightListener = + TwilightListener().apply { + Twilight.plugin.server.pluginManager.registerEvent( + T::class.java, // tell bukkit what event we're listening to + this, // the listener but we don't need it + priority, // the event priority, can be changed in the code but by default its 'normal' + { _, event -> // the 'opening' of the event executor + if (event is T) callback(event) // event executor body + }, + Twilight.plugin, // pass the instance to bukkit, so it knows what plugin should listen to the event + true + ) + } + +/** + * Layer on top of Bukkit's Listener to allow for easy unregistering. Returned by the above function. + */ +class TwilightListener : Listener { + fun unregister() = HandlerList.unregisterAll(this) } \ No newline at end of file diff --git a/src/test/kotlin/gg/flyte/twilight/EventTest.kt b/src/test/kotlin/gg/flyte/twilight/EventTest.kt index 9473a49..3e92699 100644 --- a/src/test/kotlin/gg/flyte/twilight/EventTest.kt +++ b/src/test/kotlin/gg/flyte/twilight/EventTest.kt @@ -4,8 +4,10 @@ import gg.flyte.twilight.event.event import org.bukkit.event.player.PlayerJoinEvent fun eventTest() { - event { + val listener = event { joinMessage = null player.sendMessage("Welcome!") } + + listener.unregister() } \ No newline at end of file