From fcd74c26d6bcabe3435b19d70ba5ae8e079754b4 Mon Sep 17 00:00:00 2001 From: Aizistral Date: Fri, 24 Jun 2022 22:06:57 +0200 Subject: Remake implementation of message conversion Fixes #9 --- .../mixins/MixinServerGamePacketListenerImpl.java | 55 ++++++++++++++++++++++ .../nochatreports/mixins/MixinServerPlayer.java | 47 ------------------ src/main/resources/nochatreports.mixins.json | 2 +- 3 files changed, 56 insertions(+), 48 deletions(-) create mode 100644 src/main/java/com/aizistral/nochatreports/mixins/MixinServerGamePacketListenerImpl.java delete mode 100644 src/main/java/com/aizistral/nochatreports/mixins/MixinServerPlayer.java diff --git a/src/main/java/com/aizistral/nochatreports/mixins/MixinServerGamePacketListenerImpl.java b/src/main/java/com/aizistral/nochatreports/mixins/MixinServerGamePacketListenerImpl.java new file mode 100644 index 0000000..88b7ba5 --- /dev/null +++ b/src/main/java/com/aizistral/nochatreports/mixins/MixinServerGamePacketListenerImpl.java @@ -0,0 +1,55 @@ +package com.aizistral.nochatreports.mixins; + +import com.aizistral.nochatreports.NoChatReports; +import com.aizistral.nochatreports.handlers.NoReportsConfig; + +import net.minecraft.core.Registry; +import net.minecraft.network.chat.ChatDecoration; +import net.minecraft.network.chat.ChatType; +import net.minecraft.network.chat.Component; +import net.minecraft.network.protocol.Packet; +import net.minecraft.network.protocol.game.ClientboundPlayerChatPacket; +import net.minecraft.network.protocol.game.ClientboundSystemChatPacket; +import net.minecraft.resources.ResourceKey; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.server.network.ServerGamePacketListenerImpl; +import net.minecraft.server.network.ServerPlayerConnection; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.Redirect; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(ServerGamePacketListenerImpl.class) +public abstract class MixinServerGamePacketListenerImpl implements ServerPlayerConnection { + + /** + * @reason Convert player message to system message if mod is configured respectively. + * This allows to circumvent signature check on client, as it only checks player messages. + * @author JFronny (original implementation) + * @author Aizistral + */ + + @Inject(method = "send", at = @At("HEAD"), cancellable = true) + private void onSend(Packet packet, CallbackInfo info) { + if (NoReportsConfig.convertsToGameMessage()) { + if (packet instanceof ClientboundPlayerChatPacket chat) { + Component component = chat.unsignedContent().orElse(chat.signedContent()); + component = ChatDecoration.withSender("chat.type.text").decorate(component, chat.sender()); + packet = new ClientboundSystemChatPacket(component, this.resolveChatTypeID(ChatType.SYSTEM)); + + info.cancel(); + this.send(packet); + } + } + } + + private int resolveChatTypeID(ResourceKey resourceKey) { + var self = (ServerGamePacketListenerImpl) (Object) this; + Registry registry = self.player.level.registryAccess().registryOrThrow(Registry.CHAT_TYPE_REGISTRY); + return registry.getId(registry.get(resourceKey)); + } + +} \ No newline at end of file diff --git a/src/main/java/com/aizistral/nochatreports/mixins/MixinServerPlayer.java b/src/main/java/com/aizistral/nochatreports/mixins/MixinServerPlayer.java deleted file mode 100644 index c2c2be5..0000000 --- a/src/main/java/com/aizistral/nochatreports/mixins/MixinServerPlayer.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.aizistral.nochatreports.mixins; - -import com.aizistral.nochatreports.NoChatReports; -import com.aizistral.nochatreports.handlers.NoReportsConfig; -import net.minecraft.network.chat.ChatDecoration; -import net.minecraft.network.chat.ChatType; -import net.minecraft.network.chat.Component; -import net.minecraft.network.protocol.Packet; -import net.minecraft.network.protocol.game.ClientboundPlayerChatPacket; -import net.minecraft.network.protocol.game.ClientboundSystemChatPacket; -import net.minecraft.resources.ResourceKey; -import net.minecraft.server.level.ServerPlayer; -import net.minecraft.server.network.ServerGamePacketListenerImpl; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Redirect; - -@Mixin(ServerPlayer.class) -public abstract class MixinServerPlayer { - - @Shadow - private int resolveChatTypeId(ResourceKey resourceKey) { - throw new IllegalStateException("@Shadow transformation failed. Should never happen"); - } - - /** - * @reason Convert player message to system message if mod is configured respectively. - * This allows to circumvent signature check on client, as it only checks player messages. - * @author JFronny - */ - - @Redirect(method = "sendChatMessage(Lnet/minecraft/network/chat/PlayerChatMessage;Lnet/minecraft/network/chat/ChatSender;Lnet/minecraft/resources/ResourceKey;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerGamePacketListenerImpl;send(Lnet/minecraft/network/protocol/Packet;)V")) - void extractChatMessage(ServerGamePacketListenerImpl listener, Packet packet) { - if (NoReportsConfig.convertsToGameMessage()) { - if (packet instanceof ClientboundPlayerChatPacket chat) { - Component component = chat.unsignedContent().orElse(chat.signedContent()); - component = ChatDecoration.withSender("chat.type.text").decorate(component, chat.sender()); - packet = new ClientboundSystemChatPacket(component, this.resolveChatTypeId(ChatType.SYSTEM)); - } else { - NoChatReports.LOGGER.warn("Unexpected packet type in sendChatMessage, skipping"); - } - } - listener.send(packet); - } - -} \ No newline at end of file diff --git a/src/main/resources/nochatreports.mixins.json b/src/main/resources/nochatreports.mixins.json index 708b947..f38f470 100644 --- a/src/main/resources/nochatreports.mixins.json +++ b/src/main/resources/nochatreports.mixins.json @@ -8,7 +8,7 @@ "mixins": [ "MixinPlayer", "MixinServerboundChatPacket", - "MixinServerPlayer" + "MixinServerGamePacketListenerImpl" ], "client": [ "MixinLocalPlayer", -- cgit v1.2.3