aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/events
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/events')
-rw-r--r--src/main/kotlin/moe/nea/firmament/events/AllowChatEvent.kt20
-rw-r--r--src/main/kotlin/moe/nea/firmament/events/ClientChatLineReceivedEvent.kt21
-rw-r--r--src/main/kotlin/moe/nea/firmament/events/FirmamentEventBus.kt4
-rw-r--r--src/main/kotlin/moe/nea/firmament/events/ModifyChatEvent.kt25
-rw-r--r--src/main/kotlin/moe/nea/firmament/events/ProcessChatEvent.kt25
-rw-r--r--src/main/kotlin/moe/nea/firmament/events/ServerChatLineReceivedEvent.kt19
-rw-r--r--src/main/kotlin/moe/nea/firmament/events/registration/ChatEvents.kt42
7 files changed, 114 insertions, 42 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/events/AllowChatEvent.kt b/src/main/kotlin/moe/nea/firmament/events/AllowChatEvent.kt
new file mode 100644
index 0000000..4704217
--- /dev/null
+++ b/src/main/kotlin/moe/nea/firmament/events/AllowChatEvent.kt
@@ -0,0 +1,20 @@
+/*
+ * SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe>
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+package moe.nea.firmament.events
+
+import moe.nea.firmament.util.unformattedString
+import net.minecraft.text.Text
+
+/**
+ * Filter whether the user should see a chat message altogether. May or may not be called for every chat packet sent by
+ * the server. When that quality is desired, consider [ProcessChatEvent] instead.
+ */
+data class AllowChatEvent(val text: Text) : FirmamentEvent.Cancellable() {
+ val unformattedString = text.unformattedString
+
+ companion object : FirmamentEventBus<AllowChatEvent>()
+}
diff --git a/src/main/kotlin/moe/nea/firmament/events/ClientChatLineReceivedEvent.kt b/src/main/kotlin/moe/nea/firmament/events/ClientChatLineReceivedEvent.kt
deleted file mode 100644
index 0345afc..0000000
--- a/src/main/kotlin/moe/nea/firmament/events/ClientChatLineReceivedEvent.kt
+++ /dev/null
@@ -1,21 +0,0 @@
-/*
- * SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe>
- *
- * SPDX-License-Identifier: GPL-3.0-or-later
- */
-
-package moe.nea.firmament.events
-
-import net.minecraft.text.Text
-import moe.nea.firmament.util.unformattedString
-
-/**
- * Published just before a message is added to the chat gui. Cancelling only prevents rendering, not logging to the
- * console.
- */
-data class ClientChatLineReceivedEvent(val text: Text) : FirmamentEvent.Cancellable() {
- val unformattedString = text.unformattedString
- var replaceWith: Text = text
-
- companion object : FirmamentEventBus<ClientChatLineReceivedEvent>()
-}
diff --git a/src/main/kotlin/moe/nea/firmament/events/FirmamentEventBus.kt b/src/main/kotlin/moe/nea/firmament/events/FirmamentEventBus.kt
index fcf9dcb..9bdcc26 100644
--- a/src/main/kotlin/moe/nea/firmament/events/FirmamentEventBus.kt
+++ b/src/main/kotlin/moe/nea/firmament/events/FirmamentEventBus.kt
@@ -20,10 +20,10 @@ open class FirmamentEventBus<T : FirmamentEvent> {
private val toHandle: MutableList<Handler<T>> = CopyOnWriteArrayList()
fun subscribe(handle: (T) -> Unit) {
- subscribe(handle, false)
+ subscribe(false, handle)
}
- fun subscribe(handle: (T) -> Unit, receivesCancelled: Boolean) {
+ fun subscribe(receivesCancelled: Boolean, handle: (T) -> Unit) {
toHandle.add(Handler(handle, receivesCancelled))
}
diff --git a/src/main/kotlin/moe/nea/firmament/events/ModifyChatEvent.kt b/src/main/kotlin/moe/nea/firmament/events/ModifyChatEvent.kt
new file mode 100644
index 0000000..b5a53bc
--- /dev/null
+++ b/src/main/kotlin/moe/nea/firmament/events/ModifyChatEvent.kt
@@ -0,0 +1,25 @@
+/*
+ * SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe>
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+package moe.nea.firmament.events
+
+import moe.nea.firmament.util.unformattedString
+import net.minecraft.text.Text
+
+/**
+ * Allow modification of a chat message before it is sent off to the user. Intended for display purposes.
+ */
+data class ModifyChatEvent(val originalText: Text) : FirmamentEvent() {
+ var unformattedString = originalText.unformattedString
+ private set
+ var replaceWith: Text = originalText
+ set(value) {
+ field = value
+ unformattedString = value.unformattedString
+ }
+
+ companion object : FirmamentEventBus<ModifyChatEvent>()
+}
diff --git a/src/main/kotlin/moe/nea/firmament/events/ProcessChatEvent.kt b/src/main/kotlin/moe/nea/firmament/events/ProcessChatEvent.kt
new file mode 100644
index 0000000..413c209
--- /dev/null
+++ b/src/main/kotlin/moe/nea/firmament/events/ProcessChatEvent.kt
@@ -0,0 +1,25 @@
+/*
+ * SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe>
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+package moe.nea.firmament.events
+
+import moe.nea.firmament.util.unformattedString
+import net.minecraft.text.Text
+
+/**
+ * Behaves like [AllowChatEvent], but is triggered even when cancelled by other mods. Intended for data collection.
+ * Make sure to subscribe to cancellable events as well when using.
+ */
+data class ProcessChatEvent(val text: Text, val wasExternallyCancelled: Boolean) : FirmamentEvent.Cancellable() {
+ val unformattedString = text.unformattedString
+
+ init {
+ if (wasExternallyCancelled)
+ cancelled = true
+ }
+
+ companion object : FirmamentEventBus<ProcessChatEvent>()
+}
diff --git a/src/main/kotlin/moe/nea/firmament/events/ServerChatLineReceivedEvent.kt b/src/main/kotlin/moe/nea/firmament/events/ServerChatLineReceivedEvent.kt
deleted file mode 100644
index b8b262d..0000000
--- a/src/main/kotlin/moe/nea/firmament/events/ServerChatLineReceivedEvent.kt
+++ /dev/null
@@ -1,19 +0,0 @@
-/*
- * SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe>
- *
- * SPDX-License-Identifier: GPL-3.0-or-later
- */
-
-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.
- * This event is cancellable, but should not get cancelled. Use [ClientChatLineReceivedEvent] for that instead. */
-data class ServerChatLineReceivedEvent(val text: Text) : FirmamentEvent.Cancellable() {
- val unformattedString = text.unformattedString
-
- companion object : FirmamentEventBus<ServerChatLineReceivedEvent>()
-}
diff --git a/src/main/kotlin/moe/nea/firmament/events/registration/ChatEvents.kt b/src/main/kotlin/moe/nea/firmament/events/registration/ChatEvents.kt
new file mode 100644
index 0000000..90a665a
--- /dev/null
+++ b/src/main/kotlin/moe/nea/firmament/events/registration/ChatEvents.kt
@@ -0,0 +1,42 @@
+/*
+ * SPDX-FileCopyrightText: 2023 Linnea Gräf <nea@nea.moe>
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+package moe.nea.firmament.events.registration
+
+import moe.nea.firmament.events.AllowChatEvent
+import moe.nea.firmament.events.ModifyChatEvent
+import moe.nea.firmament.events.ProcessChatEvent
+import net.fabricmc.fabric.api.client.message.v1.ClientReceiveMessageEvents
+import net.minecraft.text.Text
+
+private var lastReceivedMessage: Text? = null
+
+fun registerFirmamentChatEvents() {
+ ClientReceiveMessageEvents.ALLOW_CHAT.register(ClientReceiveMessageEvents.AllowChat { message, signedMessage, sender, params, receptionTimestamp ->
+ lastReceivedMessage = message
+ !ProcessChatEvent.publish(ProcessChatEvent(message, false)).cancelled
+ && !AllowChatEvent.publish(AllowChatEvent(message)).cancelled
+ })
+ ClientReceiveMessageEvents.ALLOW_GAME.register(ClientReceiveMessageEvents.AllowGame { message, overlay ->
+ lastReceivedMessage = message
+ overlay || (!ProcessChatEvent.publish(ProcessChatEvent(message, false)).cancelled &&
+ !AllowChatEvent.publish(AllowChatEvent(message)).cancelled)
+ })
+ ClientReceiveMessageEvents.MODIFY_GAME.register(ClientReceiveMessageEvents.ModifyGame { message, overlay ->
+ if (overlay) message
+ else ModifyChatEvent.publish(ModifyChatEvent(message)).replaceWith
+ })
+ ClientReceiveMessageEvents.GAME_CANCELED.register(ClientReceiveMessageEvents.GameCanceled { message, overlay ->
+ if (!overlay && lastReceivedMessage !== message) {
+ ProcessChatEvent.publish(ProcessChatEvent(message, true))
+ }
+ })
+ ClientReceiveMessageEvents.CHAT_CANCELED.register(ClientReceiveMessageEvents.ChatCanceled { message, signedMessage, sender, params, receptionTimestamp ->
+ if (lastReceivedMessage !== message) {
+ ProcessChatEvent.publish(ProcessChatEvent(message, true))
+ }
+ })
+}