From 01f39819599fd89200a44a28da5597a2308e27d0 Mon Sep 17 00:00:00 2001 From: nea Date: Tue, 16 May 2023 03:42:06 +0200 Subject: Rename event bus --- .../moe/nea/firmament/events/FirmamentEvent.kt | 36 +++++++++++++++++++++ .../moe/nea/firmament/events/FirmamentEventBus.kt | 37 ++++++++++++++++++++++ .../kotlin/moe/nea/firmament/events/NEUEvent.kt | 36 --------------------- .../kotlin/moe/nea/firmament/events/NEUEventBus.kt | 37 ---------------------- .../moe/nea/firmament/events/ParticleSpawnEvent.kt | 4 +-- .../moe/nea/firmament/events/ScreenOpenEvent.kt | 4 +-- .../events/ServerChatLineReceivedEvent.kt | 4 +-- .../firmament/events/SkyblockServerUpdateEvent.kt | 4 +-- .../moe/nea/firmament/events/WorldReadyEvent.kt | 4 +-- .../nea/firmament/events/WorldRenderLastEvent.kt | 4 +-- 10 files changed, 85 insertions(+), 85 deletions(-) create mode 100644 src/main/kotlin/moe/nea/firmament/events/FirmamentEvent.kt create mode 100644 src/main/kotlin/moe/nea/firmament/events/FirmamentEventBus.kt delete mode 100644 src/main/kotlin/moe/nea/firmament/events/NEUEvent.kt delete mode 100644 src/main/kotlin/moe/nea/firmament/events/NEUEventBus.kt (limited to 'src/main/kotlin/moe') diff --git a/src/main/kotlin/moe/nea/firmament/events/FirmamentEvent.kt b/src/main/kotlin/moe/nea/firmament/events/FirmamentEvent.kt new file mode 100644 index 0000000..6ac2a4b --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/events/FirmamentEvent.kt @@ -0,0 +1,36 @@ +package moe.nea.firmament.events + +/** + * An event that can be fired by a [FirmamentEventBus]. + * + * Typically, that event bus is implemented as a companion object + * + * ``` + * class SomeEvent : NEUEvent() { + * companion object : NEUEventBus() + * } + * ``` + */ +abstract class FirmamentEvent { + /** + * A [FirmamentEvent] that can be [cancelled] + */ + abstract class Cancellable : FirmamentEvent() { + /** + * Cancels this is event. + * + * @see cancelled + */ + fun cancel() { + cancelled = true + } + + /** + * Whether this event is cancelled. + * + * Cancelled events will bypass handlers unless otherwise specified and will prevent the action that this + * event was originally fired for. + */ + var cancelled: Boolean = false + } +} diff --git a/src/main/kotlin/moe/nea/firmament/events/FirmamentEventBus.kt b/src/main/kotlin/moe/nea/firmament/events/FirmamentEventBus.kt new file mode 100644 index 0000000..842cdf1 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/events/FirmamentEventBus.kt @@ -0,0 +1,37 @@ +package moe.nea.firmament.events + +import java.util.concurrent.CopyOnWriteArrayList +import moe.nea.firmament.Firmament + +/** + * A pubsub event bus. + * + * [subscribe] to events [publish]ed on this event bus. + * Subscriptions may not necessarily be delivered in the order or registering. + */ +open class FirmamentEventBus { + 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 FirmamentEvent.Cancellable || !event.cancelled) { + try { + function.invocation(event) + } catch (e: Exception) { + Firmament.logger.error("Caught exception during processing event $event", e) + } + } + } + return event + } + +} diff --git a/src/main/kotlin/moe/nea/firmament/events/NEUEvent.kt b/src/main/kotlin/moe/nea/firmament/events/NEUEvent.kt deleted file mode 100644 index 722ea64..0000000 --- a/src/main/kotlin/moe/nea/firmament/events/NEUEvent.kt +++ /dev/null @@ -1,36 +0,0 @@ -package moe.nea.firmament.events - -/** - * An event that can be fired by a [NEUEventBus]. - * - * Typically, that event bus is implemented as a companion object - * - * ``` - * class SomeEvent : NEUEvent() { - * companion object : NEUEventBus() - * } - * ``` - */ -abstract class NEUEvent { - /** - * A [NEUEvent] that can be [cancelled] - */ - abstract class Cancellable : NEUEvent() { - /** - * Cancels this is event. - * - * @see cancelled - */ - fun cancel() { - cancelled = true - } - - /** - * Whether this event is cancelled. - * - * Cancelled events will bypass handlers unless otherwise specified and will prevent the action that this - * event was originally fired for. - */ - var cancelled: Boolean = false - } -} diff --git a/src/main/kotlin/moe/nea/firmament/events/NEUEventBus.kt b/src/main/kotlin/moe/nea/firmament/events/NEUEventBus.kt deleted file mode 100644 index b3b9152..0000000 --- a/src/main/kotlin/moe/nea/firmament/events/NEUEventBus.kt +++ /dev/null @@ -1,37 +0,0 @@ -package moe.nea.firmament.events - -import java.util.concurrent.CopyOnWriteArrayList -import moe.nea.firmament.Firmament - -/** - * A pubsub event bus. - * - * [subscribe] to events [publish]ed on this event bus. - * Subscriptions may not necessarily be delivered in the order or registering. - */ -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) { - try { - function.invocation(event) - } catch (e: Exception) { - Firmament.logger.error("Caught exception during processing event $event", e) - } - } - } - return event - } - -} diff --git a/src/main/kotlin/moe/nea/firmament/events/ParticleSpawnEvent.kt b/src/main/kotlin/moe/nea/firmament/events/ParticleSpawnEvent.kt index bbf7289..bb73325 100644 --- a/src/main/kotlin/moe/nea/firmament/events/ParticleSpawnEvent.kt +++ b/src/main/kotlin/moe/nea/firmament/events/ParticleSpawnEvent.kt @@ -8,6 +8,6 @@ data class ParticleSpawnEvent( val position: Vec3d, val offset: Vec3d, val longDistance: Boolean, -) : NEUEvent() { - companion object : NEUEventBus() +) : FirmamentEvent() { + companion object : FirmamentEventBus() } diff --git a/src/main/kotlin/moe/nea/firmament/events/ScreenOpenEvent.kt b/src/main/kotlin/moe/nea/firmament/events/ScreenOpenEvent.kt index ee162ab..9c4a28c 100644 --- a/src/main/kotlin/moe/nea/firmament/events/ScreenOpenEvent.kt +++ b/src/main/kotlin/moe/nea/firmament/events/ScreenOpenEvent.kt @@ -2,6 +2,6 @@ package moe.nea.firmament.events import net.minecraft.client.gui.screen.Screen -data class ScreenOpenEvent(val old: Screen?, val new: Screen?) : NEUEvent.Cancellable() { - companion object : NEUEventBus() +data class ScreenOpenEvent(val old: Screen?, val new: Screen?) : FirmamentEvent.Cancellable() { + companion object : FirmamentEventBus() } diff --git a/src/main/kotlin/moe/nea/firmament/events/ServerChatLineReceivedEvent.kt b/src/main/kotlin/moe/nea/firmament/events/ServerChatLineReceivedEvent.kt index 7e8531c..345ac0e 100644 --- a/src/main/kotlin/moe/nea/firmament/events/ServerChatLineReceivedEvent.kt +++ b/src/main/kotlin/moe/nea/firmament/events/ServerChatLineReceivedEvent.kt @@ -6,8 +6,8 @@ import moe.nea.firmament.util.unformattedString /** * This event gets published whenever the client receives a chat message from the server. */ -data class ServerChatLineReceivedEvent(val text: Text) : NEUEvent.Cancellable() { - companion object : NEUEventBus() +data class ServerChatLineReceivedEvent(val text: Text) : FirmamentEvent.Cancellable() { + companion object : FirmamentEventBus() val unformattedString = text.unformattedString } diff --git a/src/main/kotlin/moe/nea/firmament/events/SkyblockServerUpdateEvent.kt b/src/main/kotlin/moe/nea/firmament/events/SkyblockServerUpdateEvent.kt index ae3227f..c505bf2 100644 --- a/src/main/kotlin/moe/nea/firmament/events/SkyblockServerUpdateEvent.kt +++ b/src/main/kotlin/moe/nea/firmament/events/SkyblockServerUpdateEvent.kt @@ -8,6 +8,6 @@ import moe.nea.firmament.util.Locraw * **N.B.:** This event may get fired multiple times while on the server (for example, first to null, then to the * correct location). */ -data class SkyblockServerUpdateEvent(val oldLocraw: Locraw?, val newLocraw: Locraw?) : NEUEvent() { - companion object : NEUEventBus() +data class SkyblockServerUpdateEvent(val oldLocraw: Locraw?, val newLocraw: Locraw?) : FirmamentEvent() { + companion object : FirmamentEventBus() } diff --git a/src/main/kotlin/moe/nea/firmament/events/WorldReadyEvent.kt b/src/main/kotlin/moe/nea/firmament/events/WorldReadyEvent.kt index 5b01258..e0bcd79 100644 --- a/src/main/kotlin/moe/nea/firmament/events/WorldReadyEvent.kt +++ b/src/main/kotlin/moe/nea/firmament/events/WorldReadyEvent.kt @@ -1,5 +1,5 @@ package moe.nea.firmament.events -class WorldReadyEvent : NEUEvent() { - companion object : NEUEventBus() +class WorldReadyEvent : FirmamentEvent() { + companion object : FirmamentEventBus() } diff --git a/src/main/kotlin/moe/nea/firmament/events/WorldRenderLastEvent.kt b/src/main/kotlin/moe/nea/firmament/events/WorldRenderLastEvent.kt index c23d923..e5fac8a 100644 --- a/src/main/kotlin/moe/nea/firmament/events/WorldRenderLastEvent.kt +++ b/src/main/kotlin/moe/nea/firmament/events/WorldRenderLastEvent.kt @@ -17,6 +17,6 @@ data class WorldRenderLastEvent( val gameRenderer: GameRenderer, val lightmapTextureManager: LightmapTextureManager, val positionMatrix: Matrix4f, -) : NEUEvent() { - companion object : NEUEventBus() +) : FirmamentEvent() { + companion object : FirmamentEventBus() } -- cgit