aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/events/FirmamentEventBus.kt
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-09-01 22:21:19 +0200
committerLinnea Gräf <nea@nea.moe>2024-09-01 22:21:19 +0200
commit5ed74f2df49c93ed1617520a935078b59ad7e195 (patch)
treef0d52699fff7f820bd724902878988859bd43516 /src/main/kotlin/events/FirmamentEventBus.kt
parent816f80f862d2f5de6bc9b0ae84cd6df0da341228 (diff)
downloadFirmament-5ed74f2df49c93ed1617520a935078b59ad7e195.tar.gz
Firmament-5ed74f2df49c93ed1617520a935078b59ad7e195.tar.bz2
Firmament-5ed74f2df49c93ed1617520a935078b59ad7e195.zip
Add per compat project event listeners
[no changelog]
Diffstat (limited to 'src/main/kotlin/events/FirmamentEventBus.kt')
-rw-r--r--src/main/kotlin/events/FirmamentEventBus.kt24
1 files changed, 18 insertions, 6 deletions
diff --git a/src/main/kotlin/events/FirmamentEventBus.kt b/src/main/kotlin/events/FirmamentEventBus.kt
index ee9e6c8..71331d1 100644
--- a/src/main/kotlin/events/FirmamentEventBus.kt
+++ b/src/main/kotlin/events/FirmamentEventBus.kt
@@ -1,8 +1,7 @@
-
-
package moe.nea.firmament.events
import java.util.concurrent.CopyOnWriteArrayList
+import org.apache.commons.lang3.reflect.TypeUtils
import moe.nea.firmament.Firmament
import moe.nea.firmament.util.MC
@@ -13,18 +12,31 @@ import moe.nea.firmament.util.MC
* Subscriptions may not necessarily be delivered in the order of registering.
*/
open class FirmamentEventBus<T : FirmamentEvent> {
+ companion object {
+ val allEventBuses = mutableListOf<FirmamentEventBus<*>>()
+ }
+
+ val eventType = TypeUtils.getTypeArguments(javaClass, FirmamentEventBus::class.java)!!.values.single()
+
+ init {
+ allEventBuses.add(this)
+ }
+
data class Handler<T>(
val invocation: (T) -> Unit, val receivesCancelled: Boolean,
var knownErrors: MutableSet<Class<*>> = mutableSetOf(),
+ val label: String,
)
private val toHandle: MutableList<Handler<T>> = CopyOnWriteArrayList()
- fun subscribe(handle: (T) -> Unit) {
- subscribe(false, handle)
+ val handlers: List<Handler<T>> get() = toHandle
+
+ fun subscribe(label: String, handle: (T) -> Unit) {
+ subscribe(false, label, handle)
}
- fun subscribe(receivesCancelled: Boolean, handle: (T) -> Unit) {
- toHandle.add(Handler(handle, receivesCancelled))
+ fun subscribe(receivesCancelled: Boolean, label: String, handle: (T) -> Unit) {
+ toHandle.add(Handler(handle, receivesCancelled, label = label))
}
fun publish(event: T): T {