Fix JsonBuilder for 1.16

This commit is contained in:
Eric 2020-06-27 13:03:46 +02:00
parent 056989437d
commit 4580319241

View File

@ -1,12 +1,12 @@
package de.epiceric.shopchest.nms; package de.epiceric.shopchest.nms;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.StringJoiner; import java.util.StringJoiner;
import java.util.UUID;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -117,6 +117,7 @@ public class JsonBuilder {
private Class<?> iChatBaseComponentClass = Utils.getNMSClass("IChatBaseComponent"); private Class<?> iChatBaseComponentClass = Utils.getNMSClass("IChatBaseComponent");
private Class<?> packetPlayOutChatClass = Utils.getNMSClass("PacketPlayOutChat"); private Class<?> packetPlayOutChatClass = Utils.getNMSClass("PacketPlayOutChat");
private Class<?> chatSerializerClass; private Class<?> chatSerializerClass;
private Class<?> chatMessageTypeClass;
public JsonBuilder(ShopChest plugin) { public JsonBuilder(ShopChest plugin) {
this.plugin = plugin; this.plugin = plugin;
@ -127,6 +128,10 @@ public class JsonBuilder {
chatSerializerClass = Utils.getNMSClass("IChatBaseComponent$ChatSerializer"); chatSerializerClass = Utils.getNMSClass("IChatBaseComponent$ChatSerializer");
} }
if (Utils.getMajorVersion() >= 16) {
chatMessageTypeClass = Utils.getNMSClass("ChatMessageType");
}
Class<?>[] requiredClasses = new Class<?>[] { Class<?>[] requiredClasses = new Class<?>[] {
iChatBaseComponentClass, packetPlayOutChatClass, chatSerializerClass iChatBaseComponentClass, packetPlayOutChatClass, chatSerializerClass
}; };
@ -222,12 +227,14 @@ public class JsonBuilder {
public void sendJson(Player p) { public void sendJson(Player p) {
try { try {
Object iChatBaseComponent = chatSerializerClass.getMethod("a", String.class).invoke(null, toString()); 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); Utils.sendPacket(plugin, packetPlayOutChat, p);
plugin.debug("Sent JSON: " + toString()); plugin.debug("Sent JSON: " + toString());
} catch (InstantiationException | InvocationTargetException | } catch (ReflectiveOperationException e) {
IllegalAccessException | NoSuchMethodException e) {
plugin.getLogger().severe("Failed to send JSON with reflection"); plugin.getLogger().severe("Failed to send JSON with reflection");
plugin.debug("Failed to send JSON with reflection: " + toString()); plugin.debug("Failed to send JSON with reflection: " + toString());
plugin.debug(e); plugin.debug(e);