aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/notenoughupdates/events
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/moe/nea/notenoughupdates/events')
-rw-r--r--src/main/kotlin/moe/nea/notenoughupdates/events/NEUEvent.kt7
-rw-r--r--src/main/kotlin/moe/nea/notenoughupdates/events/NEUEventBus.kt26
-rw-r--r--src/main/kotlin/moe/nea/notenoughupdates/events/NEUScreenEvents.kt23
-rw-r--r--src/main/kotlin/moe/nea/notenoughupdates/events/ScreenOpenEvent.kt7
4 files changed, 40 insertions, 23 deletions
diff --git a/src/main/kotlin/moe/nea/notenoughupdates/events/NEUEvent.kt b/src/main/kotlin/moe/nea/notenoughupdates/events/NEUEvent.kt
new file mode 100644
index 0000000..278282e
--- /dev/null
+++ b/src/main/kotlin/moe/nea/notenoughupdates/events/NEUEvent.kt
@@ -0,0 +1,7 @@
+package moe.nea.notenoughupdates.events
+
+abstract class NEUEvent {
+ abstract class Cancellable : NEUEvent() {
+ var cancelled: Boolean = false
+ }
+}
diff --git a/src/main/kotlin/moe/nea/notenoughupdates/events/NEUEventBus.kt b/src/main/kotlin/moe/nea/notenoughupdates/events/NEUEventBus.kt
new file mode 100644
index 0000000..eba71bc
--- /dev/null
+++ b/src/main/kotlin/moe/nea/notenoughupdates/events/NEUEventBus.kt
@@ -0,0 +1,26 @@
+package moe.nea.notenoughupdates.events
+
+import java.util.concurrent.CopyOnWriteArrayList
+
+open class NEUEventBus<T : NEUEvent> {
+ data class Handler<T>(val invocation: (T) -> Unit, val receivesCancelled: Boolean)
+
+ private val toHandle: MutableList<Handler<T>> = CopyOnWriteArrayList()
+ fun subscribe(handle: (T) -> Unit) {
+ subscribe(handle, false)
+ }
+
+ fun subscribe(handle: (T) -> Unit, receivesCancelled: Boolean) {
+ toHandle.add(Handler(handle, receivesCancelled))
+ }
+
+ fun publish(event: T): T {
+ for (function in toHandle) {
+ if (function.receivesCancelled || event !is NEUEvent.Cancellable || !event.cancelled) {
+ function.invocation(event)
+ }
+ }
+ return event
+ }
+
+}
diff --git a/src/main/kotlin/moe/nea/notenoughupdates/events/NEUScreenEvents.kt b/src/main/kotlin/moe/nea/notenoughupdates/events/NEUScreenEvents.kt
deleted file mode 100644
index 64edda0..0000000
--- a/src/main/kotlin/moe/nea/notenoughupdates/events/NEUScreenEvents.kt
+++ /dev/null
@@ -1,23 +0,0 @@
-package moe.nea.notenoughupdates.events
-
-import moe.nea.notenoughupdates.events.NEUScreenEvents.OnScreenOpen
-import net.fabricmc.fabric.api.event.EventFactory
-import net.minecraft.client.gui.screen.Screen
-import net.minecraft.client.MinecraftClient
-
-object NEUScreenEvents {
- fun interface OnScreenOpen {
- /**
- * Called when a new Screen is opened via [MinecraftClient.setScreen]. If [new] is null, this corresponds to closing a [Screen].
- * @return true to prevent this event from happening.
- */
- fun onScreenOpen(old: Screen?, new: Screen?): Boolean
- }
-
- val SCREEN_OPEN = EventFactory.createArrayBacked(OnScreenOpen::class.java) { arr ->
- OnScreenOpen { old, new ->
- return@OnScreenOpen arr.asSequence().any { it.onScreenOpen(old, new) }
- }
- }
-
-}
diff --git a/src/main/kotlin/moe/nea/notenoughupdates/events/ScreenOpenEvent.kt b/src/main/kotlin/moe/nea/notenoughupdates/events/ScreenOpenEvent.kt
new file mode 100644
index 0000000..793f066
--- /dev/null
+++ b/src/main/kotlin/moe/nea/notenoughupdates/events/ScreenOpenEvent.kt
@@ -0,0 +1,7 @@
+package moe.nea.notenoughupdates.events
+
+import net.minecraft.client.gui.screen.Screen
+
+data class ScreenOpenEvent(val old: Screen?, val new: Screen?) : NEUEvent.Cancellable() {
+ companion object : NEUEventBus<ScreenOpenEvent>()
+}