summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAizistral2022-06-23 21:27:36 +0200
committerAizistral2022-06-23 21:27:36 +0200
commit3066d552e45ee62d2de08025dcebd360a156b4c3 (patch)
tree0dfda56f9b7e1dee174681b006614c4fc6badf8e
parentfd0e3cccfbbecdeb20aab4da8072faa1324d3ff5 (diff)
Remake implementation of message conversion
Fixes #9
-rw-r--r--src/main/java/com/aizistral/nochatreports/mixins/MixinServerGamePacketListenerImpl.java55
-rw-r--r--src/main/java/com/aizistral/nochatreports/mixins/MixinServerPlayer.java39
-rw-r--r--src/main/resources/nochatreports.mixins.json2
3 files changed, 56 insertions, 40 deletions
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..229977c
--- /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<ChatType> resourceKey) {
+ var self = (ServerGamePacketListenerImpl) (Object) this;
+ Registry<ChatType> registry = self.player.level.registryAccess().registryOrThrow(Registry.CHAT_TYPE_REGISTRY);
+ return registry.getId(registry.get(resourceKey));
+ }
+
+}
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 68749f6..0000000
--- a/src/main/java/com/aizistral/nochatreports/mixins/MixinServerPlayer.java
+++ /dev/null
@@ -1,39 +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
- protected abstract int resolveChatTypeId(ResourceKey<ChatType> resourceKey);
-
- @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, resolveChatTypeId(ChatType.SYSTEM));
- } else {
- NoChatReports.LOGGER.warn("Unexpected packet type in sendChatMessage, skipping");
- }
- }
- listener.send(packet);
- }
-
-}
diff --git a/src/main/resources/nochatreports.mixins.json b/src/main/resources/nochatreports.mixins.json
index 1acbd3a..237791b 100644
--- a/src/main/resources/nochatreports.mixins.json
+++ b/src/main/resources/nochatreports.mixins.json
@@ -7,7 +7,7 @@
"mixins": [
"MixinPlayer",
"MixinServerboundChatPacket",
- "MixinServerPlayer"
+ "MixinServerGamePacketListenerImpl"
],
"client": [
"MixinLocalPlayer",