Skip to content

Commit

Permalink
feat: Make event function return TwilightListener to allow for unregi…
Browse files Browse the repository at this point in the history
…stering, add event javadoc, bump ver 17
  • Loading branch information
joshbker committed Oct 30, 2023
1 parent 6c539cf commit cf00d31
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Maven
<dependency>
<groupId>gg.flyte</groupId>
<artifactId>twilight</artifactId>
<version>1.0.16</version>
<version>1.0.17</version>
</dependency>
```

Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = "gg.flyte"
version = "1.0.16"
version = "1.0.17"

repositories {
mavenCentral()
Expand Down
41 changes: 30 additions & 11 deletions src/main/kotlin/gg/flyte/twilight/event/Event.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <reified T : Event> 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)
}
4 changes: 3 additions & 1 deletion src/test/kotlin/gg/flyte/twilight/EventTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import gg.flyte.twilight.event.event
import org.bukkit.event.player.PlayerJoinEvent

fun eventTest() {
event<PlayerJoinEvent> {
val listener = event<PlayerJoinEvent> {
joinMessage = null
player.sendMessage("Welcome!")
}

listener.unregister()
}

0 comments on commit cf00d31

Please sign in to comment.