From ec66c82198fe2d61d699d553c1254f08b43fcc65 Mon Sep 17 00:00:00 2001 From: nea Date: Sat, 10 Sep 2022 03:48:03 +0200 Subject: Move Eventhandling around. I still want to use fabric still "array backed" events, but these here are just a bit easier to use from kotlin. --- .../moe/nea/notenoughupdates/events/NEUEvent.kt | 7 ++++++ .../moe/nea/notenoughupdates/events/NEUEventBus.kt | 26 ++++++++++++++++++++++ .../nea/notenoughupdates/events/NEUScreenEvents.kt | 23 ------------------- .../nea/notenoughupdates/events/ScreenOpenEvent.kt | 7 ++++++ 4 files changed, 40 insertions(+), 23 deletions(-) create mode 100644 src/main/kotlin/moe/nea/notenoughupdates/events/NEUEvent.kt create mode 100644 src/main/kotlin/moe/nea/notenoughupdates/events/NEUEventBus.kt delete mode 100644 src/main/kotlin/moe/nea/notenoughupdates/events/NEUScreenEvents.kt create mode 100644 src/main/kotlin/moe/nea/notenoughupdates/events/ScreenOpenEvent.kt (limited to 'src/main/kotlin/moe/nea/notenoughupdates/events') 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 { + data class Handler(val invocation: (T) -> Unit, val receivesCancelled: Boolean) + + private val toHandle: MutableList> = 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() +} -- cgit