diff options
author | nea <romangraef@gmail.com> | 2022-09-10 03:48:03 +0200 |
---|---|---|
committer | nea <romangraef@gmail.com> | 2022-09-10 03:48:03 +0200 |
commit | ec66c82198fe2d61d699d553c1254f08b43fcc65 (patch) | |
tree | b5d4efaf5d6d6699357c3afb1d6920d89d51b3b8 /src/main/kotlin/moe/nea/notenoughupdates/events | |
parent | d9353ff54c20a08e4e078e0190fc1f364b08a2d1 (diff) | |
download | Firmament-ec66c82198fe2d61d699d553c1254f08b43fcc65.tar.gz Firmament-ec66c82198fe2d61d699d553c1254f08b43fcc65.tar.bz2 Firmament-ec66c82198fe2d61d699d553c1254f08b43fcc65.zip |
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.
Diffstat (limited to 'src/main/kotlin/moe/nea/notenoughupdates/events')
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>() +} |