diff options
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/events')
8 files changed, 146 insertions, 0 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/events/NEUEvent.kt b/src/main/kotlin/moe/nea/firmament/events/NEUEvent.kt new file mode 100644 index 0000000..722ea64 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/events/NEUEvent.kt @@ -0,0 +1,36 @@ +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<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/firmament/events/NEUEventBus.kt b/src/main/kotlin/moe/nea/firmament/events/NEUEventBus.kt new file mode 100644 index 0000000..b3b9152 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/events/NEUEventBus.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 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) { + 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 new file mode 100644 index 0000000..bbf7289 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/events/ParticleSpawnEvent.kt @@ -0,0 +1,13 @@ +package moe.nea.firmament.events + +import net.minecraft.particle.ParticleEffect +import net.minecraft.util.math.Vec3d + +data class ParticleSpawnEvent( + val particleEffect: ParticleEffect, + val position: Vec3d, + val offset: Vec3d, + val longDistance: Boolean, +) : NEUEvent() { + companion object : NEUEventBus<ParticleSpawnEvent>() +} diff --git a/src/main/kotlin/moe/nea/firmament/events/ScreenOpenEvent.kt b/src/main/kotlin/moe/nea/firmament/events/ScreenOpenEvent.kt new file mode 100644 index 0000000..ee162ab --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/events/ScreenOpenEvent.kt @@ -0,0 +1,7 @@ +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<ScreenOpenEvent>() +} diff --git a/src/main/kotlin/moe/nea/firmament/events/ServerChatLineReceivedEvent.kt b/src/main/kotlin/moe/nea/firmament/events/ServerChatLineReceivedEvent.kt new file mode 100644 index 0000000..7e8531c --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/events/ServerChatLineReceivedEvent.kt @@ -0,0 +1,13 @@ +package moe.nea.firmament.events + +import net.minecraft.text.Text +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<ServerChatLineReceivedEvent>() + + 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 new file mode 100644 index 0000000..ae3227f --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/events/SkyblockServerUpdateEvent.kt @@ -0,0 +1,13 @@ +package moe.nea.firmament.events + +import moe.nea.firmament.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/firmament/events/WorldReadyEvent.kt b/src/main/kotlin/moe/nea/firmament/events/WorldReadyEvent.kt new file mode 100644 index 0000000..5b01258 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/events/WorldReadyEvent.kt @@ -0,0 +1,5 @@ +package moe.nea.firmament.events + +class WorldReadyEvent : NEUEvent() { + companion object : NEUEventBus<WorldReadyEvent>() +} diff --git a/src/main/kotlin/moe/nea/firmament/events/WorldRenderLastEvent.kt b/src/main/kotlin/moe/nea/firmament/events/WorldRenderLastEvent.kt new file mode 100644 index 0000000..c23d923 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/events/WorldRenderLastEvent.kt @@ -0,0 +1,22 @@ +package moe.nea.firmament.events + +import org.joml.Matrix4f +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 + +/** + * 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>() +} |