From 45803192414e6013eb439c493cf7c1158fc6e93a Mon Sep 17 00:00:00 2001 From: Eric Date: Sat, 27 Jun 2020 13:03:46 +0200 Subject: [PATCH] Fix JsonBuilder for 1.16 --- .../de/epiceric/shopchest/nms/JsonBuilder.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/epiceric/shopchest/nms/JsonBuilder.java b/src/main/java/de/epiceric/shopchest/nms/JsonBuilder.java index 3bd80ee..1cfe7ae 100644 --- a/src/main/java/de/epiceric/shopchest/nms/JsonBuilder.java +++ b/src/main/java/de/epiceric/shopchest/nms/JsonBuilder.java @@ -1,12 +1,12 @@ package de.epiceric.shopchest.nms; -import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.StringJoiner; +import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -117,6 +117,7 @@ public class JsonBuilder { private Class iChatBaseComponentClass = Utils.getNMSClass("IChatBaseComponent"); private Class packetPlayOutChatClass = Utils.getNMSClass("PacketPlayOutChat"); private Class chatSerializerClass; + private Class chatMessageTypeClass; public JsonBuilder(ShopChest plugin) { this.plugin = plugin; @@ -127,6 +128,10 @@ public class JsonBuilder { chatSerializerClass = Utils.getNMSClass("IChatBaseComponent$ChatSerializer"); } + if (Utils.getMajorVersion() >= 16) { + chatMessageTypeClass = Utils.getNMSClass("ChatMessageType"); + } + Class[] requiredClasses = new Class[] { iChatBaseComponentClass, packetPlayOutChatClass, chatSerializerClass }; @@ -222,12 +227,14 @@ public class JsonBuilder { public void sendJson(Player p) { try { Object iChatBaseComponent = chatSerializerClass.getMethod("a", String.class).invoke(null, toString()); - Object packetPlayOutChat = packetPlayOutChatClass.getConstructor(iChatBaseComponentClass).newInstance(iChatBaseComponent); + Object packetPlayOutChat = Utils.getMajorVersion() < 16 + ? packetPlayOutChatClass.getConstructor(iChatBaseComponentClass).newInstance(iChatBaseComponent) + : packetPlayOutChatClass.getConstructor(iChatBaseComponentClass, chatMessageTypeClass, UUID.class) + .newInstance(iChatBaseComponent, chatMessageTypeClass.getField("CHAT").get(null), UUID.randomUUID()); Utils.sendPacket(plugin, packetPlayOutChat, p); plugin.debug("Sent JSON: " + toString()); - } catch (InstantiationException | InvocationTargetException | - IllegalAccessException | NoSuchMethodException e) { + } catch (ReflectiveOperationException e) { plugin.getLogger().severe("Failed to send JSON with reflection"); plugin.debug("Failed to send JSON with reflection: " + toString()); plugin.debug(e);