Skip to content

Commit

Permalink
feat(custom-events): add disableCustomEventListeners to allow for unr…
Browse files Browse the repository at this point in the history
…egistering event listeners

useful for heavier event listeners such as OpEventListener
  • Loading branch information
joshbker committed Nov 5, 2023
1 parent d332edb commit 73a7796
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 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.27</version>
<version>1.0.28</version>
</dependency>
```

Expand All @@ -29,14 +29,14 @@ maven {
url "https://repo.flyte.gg/releases"
}
implementation "gg.flyte:twilight:1.0.27"
implementation "gg.flyte:twilight:1.0.28"
```

Gradle (Kotlin DSL)
```kotlin
maven("https://repo.flyte.gg/releases")

implementation("gg.flyte:twilight:1.0.27")
implementation("gg.flyte:twilight:1.0.28")
```

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.27"
version = "1.0.28"

repositories {
mavenCentral()
Expand Down
14 changes: 13 additions & 1 deletion src/main/kotlin/gg/flyte/twilight/event/Event.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ open class CustomTwilightListener {
val events = mutableListOf<TwilightListener>()
val runnables = mutableListOf<BukkitTask>()

/**
* Unregisters this custom Twilight listener, removing all registered events and associated runnables.
* Any event handlers and scheduled tasks associated with this listener are deactivated.
*/
fun unregister() {
events.applyForEach { unregister() }
runnables.applyForEach { cancel() }
Expand Down Expand Up @@ -117,9 +121,17 @@ open class TwilightEvent(async: Boolean = false) : Event(async), Cancellable {
/**
* List of custom Twilight Event Listeners
*/
val customEvents = listOf<CustomTwilightListener>(
val customEventListeners = mutableSetOf<CustomTwilightListener>(
GUIListener,
InteractEventListener,
OpEventListener
)

/**
* Disables custom event listeners by unregistering them from the associated events.
*
* @param events The custom event listeners to disable.
*/
fun disableCustomEventListeners(vararg events: CustomTwilightListener) {
customEventListeners -= events.toSet().also { it.applyForEach { unregister() } }
}
4 changes: 4 additions & 0 deletions src/test/kotlin/gg/flyte/twilight/EventTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package gg.flyte.twilight

import gg.flyte.twilight.event.custom.admin.listener.OpEventListener
import gg.flyte.twilight.event.disableCustomEventListeners
import gg.flyte.twilight.event.event
import org.bukkit.event.player.PlayerJoinEvent

Expand All @@ -10,4 +12,6 @@ fun eventTest() {
}

listener.unregister()

disableCustomEventListeners(OpEventListener)
}

0 comments on commit 73a7796

Please sign in to comment.