From b9f973538db88aff652cb7f21e04f14c72a1c036 Mon Sep 17 00:00:00 2001 From: TheRealWormbo Date: Thu, 30 May 2024 21:16:41 +0200 Subject: [PATCH] Moved Fabric chat message hook for Corporea Index to ClientPacketListener::sendChat Adds support for chat messages sent through other means than the ChatScreen, e.g. "Microphone Text Input" mod or custom UI screens that also happen to allow chatting, and is closer to how the corresponding Forge event is dispatched. --- .../fabric/mixin/client/ChatScreenMixin.java | 27 ------------------- .../client/ClientPacketListenerMixin.java | 22 +++++++++++++++ .../main/resources/botania_fabric.mixins.json | 2 +- 3 files changed, 23 insertions(+), 28 deletions(-) delete mode 100644 Fabric/src/main/java/vazkii/botania/fabric/mixin/client/ChatScreenMixin.java create mode 100644 Fabric/src/main/java/vazkii/botania/fabric/mixin/client/ClientPacketListenerMixin.java diff --git a/Fabric/src/main/java/vazkii/botania/fabric/mixin/client/ChatScreenMixin.java b/Fabric/src/main/java/vazkii/botania/fabric/mixin/client/ChatScreenMixin.java deleted file mode 100644 index f085912678..0000000000 --- a/Fabric/src/main/java/vazkii/botania/fabric/mixin/client/ChatScreenMixin.java +++ /dev/null @@ -1,27 +0,0 @@ -package vazkii.botania.fabric.mixin.client; - -import net.minecraft.client.gui.screens.ChatScreen; -import net.minecraft.client.gui.screens.Screen; -import net.minecraft.network.chat.Component; - -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - -import vazkii.botania.common.block.block_entity.corporea.CorporeaIndexBlockEntity; - -@Mixin(value = ChatScreen.class) -public abstract class ChatScreenMixin extends Screen { - private ChatScreenMixin(Component title) { - super(title); - throw new IllegalStateException(); - } - - @Inject(at = @At("HEAD"), method = "handleChatInput", cancellable = true) - public void onChat(String input, boolean addToRecentChat, CallbackInfoReturnable cir) { - if (CorporeaIndexBlockEntity.ClientHandler.onChat(this.minecraft.player, input)) { - cir.setReturnValue(true); - } - } -} diff --git a/Fabric/src/main/java/vazkii/botania/fabric/mixin/client/ClientPacketListenerMixin.java b/Fabric/src/main/java/vazkii/botania/fabric/mixin/client/ClientPacketListenerMixin.java new file mode 100644 index 0000000000..4241164965 --- /dev/null +++ b/Fabric/src/main/java/vazkii/botania/fabric/mixin/client/ClientPacketListenerMixin.java @@ -0,0 +1,22 @@ +package vazkii.botania.fabric.mixin.client; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.multiplayer.ClientPacketListener; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import vazkii.botania.common.block.block_entity.corporea.CorporeaIndexBlockEntity; + +@Mixin(ClientPacketListener.class) +public class ClientPacketListenerMixin { + @Inject(at = @At("HEAD"), method = "sendChat", cancellable = true) + private void onChat(String message, CallbackInfo ci) { + var player = Minecraft.getInstance().player; + if (CorporeaIndexBlockEntity.ClientHandler.onChat(player, message)) { + ci.cancel(); + } + } +} diff --git a/Fabric/src/main/resources/botania_fabric.mixins.json b/Fabric/src/main/resources/botania_fabric.mixins.json index 4495083924..a0393ce131 100644 --- a/Fabric/src/main/resources/botania_fabric.mixins.json +++ b/Fabric/src/main/resources/botania_fabric.mixins.json @@ -30,7 +30,7 @@ "client.AbstractTextureFabricMixin", "client.BlockModelDeserializerFabricMixin", "client.BossHealthOverlayFabricMixin", - "client.ChatScreenMixin", + "client.ClientPacketListenerMixin", "client.DebugScreenOverlayFabricMixin", "client.GuiGraphicsFabricMixin", "client.KeyboardHandlerFabricMixin",