summaryrefslogtreecommitdiffstats
path: root/src/main/java/com
diff options
context:
space:
mode:
authorAizistral2022-06-24 22:06:57 +0200
committerAizistral2022-06-24 22:06:57 +0200
commitfcd74c26d6bcabe3435b19d70ba5ae8e079754b4 (patch)
treed0257b1119206957be390713d2a839ea4db8ba47 /src/main/java/com
parent3e052b0865ddf7b988d05e6589d185693ec2025b (diff)
Remake implementation of message conversion
Fixes #9
Diffstat (limited to 'src/main/java/com')
-rw-r--r--src/main/java/com/aizistral/nochatreports/mixins/MixinServerGamePacketListenerImpl.java (renamed from src/main/java/com/aizistral/nochatreports/mixins/MixinServerPlayer.java)36
1 files changed, 22 insertions, 14 deletions
diff --git a/src/main/java/com/aizistral/nochatreports/mixins/MixinServerPlayer.java b/src/main/java/com/aizistral/nochatreports/mixins/MixinServerGamePacketListenerImpl.java
index c2c2be5..88b7ba5 100644
--- a/src/main/java/com/aizistral/nochatreports/mixins/MixinServerPlayer.java
+++ b/src/main/java/com/aizistral/nochatreports/mixins/MixinServerGamePacketListenerImpl.java
@@ -2,6 +2,8 @@ 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;
@@ -11,37 +13,43 @@ 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(ServerPlayer.class)
-public abstract class MixinServerPlayer {
-
- @Shadow
- private int resolveChatTypeId(ResourceKey<ChatType> resourceKey) {
- throw new IllegalStateException("@Shadow transformation failed. Should never happen");
- }
+@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
+ * @author JFronny (original implementation)
+ * @author Aizistral
*/
- @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) {
+ @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));
- } else {
- NoChatReports.LOGGER.warn("Unexpected packet type in sendChatMessage, skipping");
+ packet = new ClientboundSystemChatPacket(component, this.resolveChatTypeID(ChatType.SYSTEM));
+
+ info.cancel();
+ this.send(packet);
}
}
- listener.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));
}
} \ No newline at end of file