From 72fd444c78c17773d3611d082b388f9f0bfa9888 Mon Sep 17 00:00:00 2001 From: Rothes <449181985@qq.com> Date: Wed, 14 Feb 2024 17:41:00 +0800 Subject: [PATCH] Fix signed message check on PandaSpigot --- .../server/chat/PlayerChatHelper.java | 120 +++++++++--------- 1 file changed, 61 insertions(+), 59 deletions(-) diff --git a/src/main/java/me/rothes/protocolstringreplacer/packetlisteners/server/chat/PlayerChatHelper.java b/src/main/java/me/rothes/protocolstringreplacer/packetlisteners/server/chat/PlayerChatHelper.java index 3e6a39d..af4b3d9 100644 --- a/src/main/java/me/rothes/protocolstringreplacer/packetlisteners/server/chat/PlayerChatHelper.java +++ b/src/main/java/me/rothes/protocolstringreplacer/packetlisteners/server/chat/PlayerChatHelper.java @@ -55,78 +55,80 @@ public class PlayerChatHelper { version = Version.V19_4; } - try { - messageBodyClass = MinecraftReflection.getMinecraftClass("network.chat.SignedMessageBody"); - } catch (Throwable ignored) {} - - try { - // 1.19.1+ - playerChatMessageClass = MinecraftReflection.getMinecraftClass("network.chat.PlayerChatMessage"); - for (Field declaredField : playerChatMessageClass.getDeclaredFields()) { - if (declaredField.getType() == Optional.class) { - declaredField.setAccessible(true); - playerChatMessageComponentField = declaredField; - break; + if (version != Version.V8_0_TO_V18_2) { + try { + messageBodyClass = MinecraftReflection.getMinecraftClass("network.chat.SignedMessageBody"); + } catch (Throwable ignored) {} + + try { + // 1.19.1+ + playerChatMessageClass = MinecraftReflection.getMinecraftClass("network.chat.PlayerChatMessage"); + for (Field declaredField : playerChatMessageClass.getDeclaredFields()) { + if (declaredField.getType() == Optional.class) { + declaredField.setAccessible(true); + playerChatMessageComponentField = declaredField; + break; + } } - } - Class chatMessageTypeClass = MinecraftReflection.getMinecraftClass("network.chat.ChatMessageType"); - - for (Field field : PacketType.Play.Server.CHAT.getPacketClass().getDeclaredFields()) { - Class declaringClass = field.getType().getDeclaringClass(); - if (declaringClass == chatMessageTypeClass) { - chatMessageTypeSubClass = field.getType(); - setupNameFields(chatMessageTypeSubClass); - } else if (messageBodyClass != null && declaringClass == messageBodyClass) { - // 1.19.3 only - messageBodySubClass = field.getType(); - for (Field declaredField : messageBodySubClass.getDeclaredFields()) { - if (declaredField.getType() == String.class) { - messageStringField = declaredField; - messageStringField.setAccessible(true); - break; + Class chatMessageTypeClass = MinecraftReflection.getMinecraftClass("network.chat.ChatMessageType"); + + for (Field field : PacketType.Play.Server.CHAT.getPacketClass().getDeclaredFields()) { + Class declaringClass = field.getType().getDeclaringClass(); + if (declaringClass == chatMessageTypeClass) { + chatMessageTypeSubClass = field.getType(); + setupNameFields(chatMessageTypeSubClass); + } else if (messageBodyClass != null && declaringClass == messageBodyClass) { + // 1.19.3 only + messageBodySubClass = field.getType(); + for (Field declaredField : messageBodySubClass.getDeclaredFields()) { + if (declaredField.getType() == String.class) { + messageStringField = declaredField; + messageStringField.setAccessible(true); + break; + } } - } - } else if (field.getType() == playerChatMessageClass) { - chatMessageField = field; - chatMessageField.setAccessible(true); - - for (Field declaredField : playerChatMessageClass.getDeclaredFields()) { - if (declaredField.getType() == messageBodyClass) { - messageBodyField = declaredField; - messageBodyField.setAccessible(true); - - Class messageContentClass = MinecraftReflection.getMinecraftClass("network.chat.ChatMessageContent"); - for (Field messageContentClassDeclaredField : messageBodyClass.getDeclaredFields()) { - if (messageContentClassDeclaredField.getType() == messageContentClass) { - messageContentField = messageContentClassDeclaredField; - messageContentField.setAccessible(true); - - for (Field contentClassDeclaredField : messageContentClass.getDeclaredFields()) { - if (contentClassDeclaredField.getType() == String.class) { - stringField = contentClassDeclaredField; - stringField.setAccessible(true); - } else if (contentClassDeclaredField.getType() == MinecraftReflection.getIChatBaseComponentClass()) { - componentField = contentClassDeclaredField; - componentField.setAccessible(true); + } else if (field.getType() == playerChatMessageClass) { + chatMessageField = field; + chatMessageField.setAccessible(true); + + for (Field declaredField : playerChatMessageClass.getDeclaredFields()) { + if (declaredField.getType() == messageBodyClass) { + messageBodyField = declaredField; + messageBodyField.setAccessible(true); + + Class messageContentClass = MinecraftReflection.getMinecraftClass("network.chat.ChatMessageContent"); + for (Field messageContentClassDeclaredField : messageBodyClass.getDeclaredFields()) { + if (messageContentClassDeclaredField.getType() == messageContentClass) { + messageContentField = messageContentClassDeclaredField; + messageContentField.setAccessible(true); + + for (Field contentClassDeclaredField : messageContentClass.getDeclaredFields()) { + if (contentClassDeclaredField.getType() == String.class) { + stringField = contentClassDeclaredField; + stringField.setAccessible(true); + } else if (contentClassDeclaredField.getType() == MinecraftReflection.getIChatBaseComponentClass()) { + componentField = contentClassDeclaredField; + componentField.setAccessible(true); + } } } } } } - } + } } - } - if (chatMessageTypeSubClass == null) { - // Not 1.19.1+, it's 1.19 - chatSenderClass = MinecraftReflection.getMinecraftClass("network.chat.ChatSender"); - setupNameFields(chatSenderClass); + if (chatMessageTypeSubClass == null) { + // Not 1.19.1+, it's 1.19 + chatSenderClass = MinecraftReflection.getMinecraftClass("network.chat.ChatSender"); + setupNameFields(chatSenderClass); + } + } catch (Throwable t) { + ProtocolStringReplacer.warn("Unable to init PlayerChatHelper. PlayerChat packet handle may not work.", t); } - } catch (Throwable t) { - ProtocolStringReplacer.warn("Unable to init PlayerChatHelper. PlayerChat packet handle may not work.", t); } }