From d202ef5439a180efecc27ab4695b1a12febb041f Mon Sep 17 00:00:00 2001 From: nea Date: Mon, 4 Sep 2023 20:22:51 +0200 Subject: Make chat events use fabric events [no changelog] --- .../firmament/events/registration/ChatEvents.kt | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/main/kotlin/moe/nea/firmament/events/registration/ChatEvents.kt (limited to 'src/main/kotlin/moe/nea/firmament/events/registration') 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 + * + * 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)) + } + }) +} -- cgit