aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/notenoughupdates/events
diff options
context:
space:
mode:
authornea <romangraef@gmail.com>2022-09-28 12:45:56 +0200
committernea <romangraef@gmail.com>2022-09-28 12:45:56 +0200
commit4d73331a449f0b0647066f7dde0628730fe0e178 (patch)
tree047f463e13d14ea6cf9c8b37602a756f6880f9a0 /src/main/kotlin/moe/nea/notenoughupdates/events
parentec66c82198fe2d61d699d553c1254f08b43fcc65 (diff)
downloadFirmament-4d73331a449f0b0647066f7dde0628730fe0e178.tar.gz
Firmament-4d73331a449f0b0647066f7dde0628730fe0e178.tar.bz2
Firmament-4d73331a449f0b0647066f7dde0628730fe0e178.zip
Fairy souls
Diffstat (limited to 'src/main/kotlin/moe/nea/notenoughupdates/events')
-rw-r--r--src/main/kotlin/moe/nea/notenoughupdates/events/NEUEvent.kt29
-rw-r--r--src/main/kotlin/moe/nea/notenoughupdates/events/NEUEventBus.kt13
-rw-r--r--src/main/kotlin/moe/nea/notenoughupdates/events/ServerChatLineReceivedEvent.kt13
-rw-r--r--src/main/kotlin/moe/nea/notenoughupdates/events/SkyblockServerUpdateEvent.kt13
-rw-r--r--src/main/kotlin/moe/nea/notenoughupdates/events/WorldRenderLastEvent.kt22
5 files changed, 89 insertions, 1 deletions
diff --git a/src/main/kotlin/moe/nea/notenoughupdates/events/NEUEvent.kt b/src/main/kotlin/moe/nea/notenoughupdates/events/NEUEvent.kt
index 278282e..81dc6fc 100644
--- a/src/main/kotlin/moe/nea/notenoughupdates/events/NEUEvent.kt
+++ b/src/main/kotlin/moe/nea/notenoughupdates/events/NEUEvent.kt
@@ -1,7 +1,36 @@
package moe.nea.notenoughupdates.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<SomeEvent>()
+ * }
+ * ```
+ */
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/notenoughupdates/events/NEUEventBus.kt b/src/main/kotlin/moe/nea/notenoughupdates/events/NEUEventBus.kt
index eba71bc..7eb0ecd 100644
--- a/src/main/kotlin/moe/nea/notenoughupdates/events/NEUEventBus.kt
+++ b/src/main/kotlin/moe/nea/notenoughupdates/events/NEUEventBus.kt
@@ -1,7 +1,14 @@
package moe.nea.notenoughupdates.events
import java.util.concurrent.CopyOnWriteArrayList
+import moe.nea.notenoughupdates.NotEnoughUpdates
+/**
+ * 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<T : NEUEvent> {
data class Handler<T>(val invocation: (T) -> Unit, val receivesCancelled: Boolean)
@@ -17,7 +24,11 @@ open class NEUEventBus<T : NEUEvent> {
fun publish(event: T): T {
for (function in toHandle) {
if (function.receivesCancelled || event !is NEUEvent.Cancellable || !event.cancelled) {
- function.invocation(event)
+ try {
+ function.invocation(event)
+ } catch (e: Exception) {
+ NotEnoughUpdates.logger.error("Caught exception during processing event $event", e)
+ }
}
}
return event
diff --git a/src/main/kotlin/moe/nea/notenoughupdates/events/ServerChatLineReceivedEvent.kt b/src/main/kotlin/moe/nea/notenoughupdates/events/ServerChatLineReceivedEvent.kt
new file mode 100644
index 0000000..681ec6c
--- /dev/null
+++ b/src/main/kotlin/moe/nea/notenoughupdates/events/ServerChatLineReceivedEvent.kt
@@ -0,0 +1,13 @@
+package moe.nea.notenoughupdates.events
+
+import net.minecraft.text.Text
+import moe.nea.notenoughupdates.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<ServerChatLineReceivedEvent>()
+
+ val unformattedString = text.unformattedString
+}
diff --git a/src/main/kotlin/moe/nea/notenoughupdates/events/SkyblockServerUpdateEvent.kt b/src/main/kotlin/moe/nea/notenoughupdates/events/SkyblockServerUpdateEvent.kt
new file mode 100644
index 0000000..541b4c6
--- /dev/null
+++ b/src/main/kotlin/moe/nea/notenoughupdates/events/SkyblockServerUpdateEvent.kt
@@ -0,0 +1,13 @@
+package moe.nea.notenoughupdates.events
+
+import moe.nea.notenoughupdates.util.Locraw
+
+/**
+ * This event gets published whenever `/locraw` is queried and HyPixel returns a location different to the old one.
+ *
+ * **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<SkyblockServerUpdateEvent>()
+}
diff --git a/src/main/kotlin/moe/nea/notenoughupdates/events/WorldRenderLastEvent.kt b/src/main/kotlin/moe/nea/notenoughupdates/events/WorldRenderLastEvent.kt
new file mode 100644
index 0000000..c230c9c
--- /dev/null
+++ b/src/main/kotlin/moe/nea/notenoughupdates/events/WorldRenderLastEvent.kt
@@ -0,0 +1,22 @@
+package moe.nea.notenoughupdates.events
+
+import net.minecraft.client.render.Camera
+import net.minecraft.client.render.GameRenderer
+import net.minecraft.client.render.LightmapTextureManager
+import net.minecraft.client.util.math.MatrixStack
+import net.minecraft.util.math.Matrix4f
+
+/**
+ * This event is called after all world rendering is done, but before any GUI rendering (including hand) has been done.
+ */
+data class WorldRenderLastEvent(
+ val matrices: MatrixStack,
+ val tickDelta: Float,
+ val renderBlockOutline: Boolean,
+ val camera: Camera,
+ val gameRenderer: GameRenderer,
+ val lightmapTextureManager: LightmapTextureManager,
+ val positionMatrix: Matrix4f,
+) : NEUEvent() {
+ companion object : NEUEventBus<WorldRenderLastEvent>()
+}