From 6fb80712be016cb427b34d030779e90f6275a826 Mon Sep 17 00:00:00 2001 From: Elikill58 Date: Mon, 20 May 2024 22:52:16 +0200 Subject: [PATCH] Fix #133 --- .../java/me/dadus33/chatitem/ChatItem.java | 7 +- .../v1/PacketEditingChatManager.java | 6 +- .../hook/AdventureComponentManager.java | 3 +- .../v1/basecomp/hook/ComponentNMSManager.java | 5 +- .../hook/IChatBaseComponentManager.java | 26 +------ .../v1/basecomp/hook/PCMComponentManager.java | 4 +- .../v1/listeners/ChatPacketManager.java | 14 ---- .../chatitem/commands/ChatItemCommand.java | 3 +- .../chatitem/listeners/InventoryListener.java | 4 +- .../dadus33/chatitem/platform/IPlatform.java | 11 +++ .../chatitem/platform/hook/PaperPlatform.java | 44 +++++++++++ .../platform/hook/SpigotPlatform.java | 77 +++++++++++++++++++ .../dadus33/chatitem/utils/PacketUtils.java | 31 ++------ .../java/me/dadus33/chatitem/utils/Utils.java | 10 +++ .../me/dadus33/chatitem/utils/Version.java | 20 ++--- 15 files changed, 173 insertions(+), 92 deletions(-) diff --git a/src/main/java/me/dadus33/chatitem/ChatItem.java b/src/main/java/me/dadus33/chatitem/ChatItem.java index cf18c745..092c45d9 100644 --- a/src/main/java/me/dadus33/chatitem/ChatItem.java +++ b/src/main/java/me/dadus33/chatitem/ChatItem.java @@ -38,7 +38,6 @@ import me.dadus33.chatitem.platform.hook.SpigotPlatform; import me.dadus33.chatitem.playernamer.PlayerNamerManager; import me.dadus33.chatitem.utils.Colors; -import me.dadus33.chatitem.utils.PacketUtils; import me.dadus33.chatitem.utils.ReflectionUtils; import me.dadus33.chatitem.utils.SemVer; import me.dadus33.chatitem.utils.Utils; @@ -46,7 +45,7 @@ public class ChatItem extends JavaPlugin { - private static final IPlatform platform = PacketUtils.IS_PAPER ? new PaperPlatform() : new SpigotPlatform(); + private static final IPlatform platform = Utils.IS_PAPER ? new PaperPlatform() : new SpigotPlatform(); public static IPlatform getPlatform() { return platform; } @@ -98,12 +97,12 @@ private void chooseManagers() { case "all": this.chatManager.add(new PacketEditingChatManager(this)); this.chatManager.add(new ChatListenerChatManager(this)); - if(PacketUtils.IS_PAPER) + if(Utils.IS_PAPER) this.chatManager.add(new PaperChatManager(this)); getLogger().info("Manager automatically chosen: " + getVisualChatManagers()); break; case "auto": - if(PacketUtils.IS_PAPER) + if(Utils.IS_PAPER) this.chatManager.add(new PaperChatManager(this)); else if (getPluginThatRequirePacket().stream().map(pm::getPlugin).anyMatch(Objects::nonNull) && Version.getVersion().isNewerThan(Version.V1_7)) this.chatManager.add(new PacketEditingChatManager(this)); diff --git a/src/main/java/me/dadus33/chatitem/chatmanager/v1/PacketEditingChatManager.java b/src/main/java/me/dadus33/chatitem/chatmanager/v1/PacketEditingChatManager.java index 3c5e6cfe..07b76b62 100644 --- a/src/main/java/me/dadus33/chatitem/chatmanager/v1/PacketEditingChatManager.java +++ b/src/main/java/me/dadus33/chatitem/chatmanager/v1/PacketEditingChatManager.java @@ -80,7 +80,9 @@ public static Object createSystemChatPacket(String json, Object old) throws Exce Object packet = internalCreateSystemChatPacket(json, old); if(packet != null) return packet; - packet = internalCreateSystemChatPacket(PacketUtils.ICB_FROM_JSON.invoke(null, json), old); + Object icb = ChatItem.getPlatform().jsonToBaseComponent(json); + if(icb != null) + packet = internalCreateSystemChatPacket(icb, old); if(packet != null) return packet; ChatItem.getInstance().getLogger().warning("Can't create a new packet for json " + json); @@ -89,7 +91,7 @@ public static Object createSystemChatPacket(String json, Object old) throws Exce private static Object internalCreateSystemChatPacket(Object obj, Object old) throws Exception { Class packetClass = PacketUtils.getNmsClass("ClientboundPlayerChatPacket", "network.protocol.game.", "ClientboundSystemChatPacket", "PacketPlayOutChat"); - Class chatMessageTypeClass = PacketUtils.isClassExist("net.minecraft.network.chat.ChatMessageType") ? PacketUtils.getNmsClass("ChatMessageType", "network.chat.") : null; + Class chatMessageTypeClass = Utils.isClassExist("net.minecraft.network.chat.ChatMessageType") ? PacketUtils.getNmsClass("ChatMessageType", "network.chat.") : null; Constructor betterOne = null; Object[] betterParam = null; for (Constructor cons : packetClass.getDeclaredConstructors()) { diff --git a/src/main/java/me/dadus33/chatitem/chatmanager/v1/basecomp/hook/AdventureComponentManager.java b/src/main/java/me/dadus33/chatitem/chatmanager/v1/basecomp/hook/AdventureComponentManager.java index cb51de05..10e275f7 100644 --- a/src/main/java/me/dadus33/chatitem/chatmanager/v1/basecomp/hook/AdventureComponentManager.java +++ b/src/main/java/me/dadus33/chatitem/chatmanager/v1/basecomp/hook/AdventureComponentManager.java @@ -22,6 +22,7 @@ import me.dadus33.chatitem.hook.DiscordSrvSupport; import me.dadus33.chatitem.utils.Messages; import me.dadus33.chatitem.utils.PacketUtils; +import me.dadus33.chatitem.utils.Utils; import net.kyori.adventure.audience.Audience; import net.kyori.adventure.key.Key; import net.kyori.adventure.nbt.api.BinaryTagHolder; @@ -78,7 +79,7 @@ public Object manageItem(Player p, Chat chat, ChatItemPacket packet, ItemStack i String itemName = ChatManager.getNameOfItem(chat.getPlayer(), item, c); ChatItem.debug("NBT tag: " + PacketUtils.getNbtTag(item)); HoverEvent hover; - if(PacketUtils.IS_PAPER) + if(Utils.IS_PAPER) hover = item.asHoverEvent(); else hover = HoverEvent.showItem(Key.key(item.getType().getKey().getKey()), item.getAmount(), BinaryTagHolder.of(PacketUtils.getNbtTag(item))); diff --git a/src/main/java/me/dadus33/chatitem/chatmanager/v1/basecomp/hook/ComponentNMSManager.java b/src/main/java/me/dadus33/chatitem/chatmanager/v1/basecomp/hook/ComponentNMSManager.java index e07a2e54..27ee2152 100644 --- a/src/main/java/me/dadus33/chatitem/chatmanager/v1/basecomp/hook/ComponentNMSManager.java +++ b/src/main/java/me/dadus33/chatitem/chatmanager/v1/basecomp/hook/ComponentNMSManager.java @@ -9,13 +9,12 @@ import me.dadus33.chatitem.chatmanager.v1.basecomp.IComponentManager; import me.dadus33.chatitem.chatmanager.v1.packets.ChatItemPacket; import me.dadus33.chatitem.hook.DiscordSrvSupport; -import me.dadus33.chatitem.utils.PacketUtils; public class ComponentNMSManager implements IComponentManager { @Override public boolean hasConditions() { - return PacketUtils.COMPONENT_CLASS != null; + return ChatItem.getPlatform().hasBaseComponentSerializer(); } @Override @@ -23,7 +22,7 @@ public String getBaseComponentAsJSON(ChatItemPacket packet) { Object chatBaseComp = packet.getContent().getChatComponents().readSafely(0); if (chatBaseComp != null) { try { - Object o = PacketUtils.CHAT_SERIALIZER.getMethod("a", PacketUtils.COMPONENT_CLASS).invoke(null, chatBaseComp); + Object o = ChatItem.getPlatform().baseComponentToJson(chatBaseComp); if(o != null && o instanceof String && JsonParser.parseString((String) o).isJsonObject()) { return (String) o; } diff --git a/src/main/java/me/dadus33/chatitem/chatmanager/v1/basecomp/hook/IChatBaseComponentManager.java b/src/main/java/me/dadus33/chatitem/chatmanager/v1/basecomp/hook/IChatBaseComponentManager.java index 7016c917..8ebeaca7 100644 --- a/src/main/java/me/dadus33/chatitem/chatmanager/v1/basecomp/hook/IChatBaseComponentManager.java +++ b/src/main/java/me/dadus33/chatitem/chatmanager/v1/basecomp/hook/IChatBaseComponentManager.java @@ -1,44 +1,22 @@ package me.dadus33.chatitem.chatmanager.v1.basecomp.hook; -import java.lang.reflect.Method; - import me.dadus33.chatitem.ChatItem; import me.dadus33.chatitem.chatmanager.v1.PacketEditingChatManager; import me.dadus33.chatitem.chatmanager.v1.basecomp.IComponentManager; import me.dadus33.chatitem.chatmanager.v1.packets.ChatItemPacket; -import me.dadus33.chatitem.utils.PacketUtils; public class IChatBaseComponentManager implements IComponentManager { - private Method serializerGetJson; - - public IChatBaseComponentManager() { - try { - Class chatSerializer = PacketUtils.CHAT_SERIALIZER; - //fromJson = chatSerializer.getMethod("a", String.class); - for (Method m : chatSerializer.getDeclaredMethods()) { - if (m.getParameterCount() == 1 && m.getParameterTypes()[0].equals(PacketUtils.COMPONENT_CLASS) && m.getReturnType().equals(String.class)) { - serializerGetJson = m; - break; - } - } - if (serializerGetJson == null) - ChatItem.getInstance().getLogger().warning("Failed to find JSON serializer in class: " + chatSerializer.getCanonicalName()); - } catch (Exception e) { - e.printStackTrace(); - } - } - @Override public boolean hasConditions() { - return serializerGetJson != null; + return ChatItem.getPlatform().hasBaseComponentSerializer(); } @Override public String getBaseComponentAsJSON(ChatItemPacket packet) { try { Object obj = packet.getContent().getChatComponents().readSafely(0); - return obj == null ? null : (String) serializerGetJson.invoke(null, obj); + return obj == null ? null : (String) ChatItem.getPlatform().baseComponentToJson(obj); } catch (Exception exc) { exc.printStackTrace(); } diff --git a/src/main/java/me/dadus33/chatitem/chatmanager/v1/basecomp/hook/PCMComponentManager.java b/src/main/java/me/dadus33/chatitem/chatmanager/v1/basecomp/hook/PCMComponentManager.java index 3bb19112..6a3f8e7d 100644 --- a/src/main/java/me/dadus33/chatitem/chatmanager/v1/basecomp/hook/PCMComponentManager.java +++ b/src/main/java/me/dadus33/chatitem/chatmanager/v1/basecomp/hook/PCMComponentManager.java @@ -33,7 +33,7 @@ public String getBaseComponentAsJSON(ChatItemPacket packet) { Object chatBaseComp = ReflectionUtils.getMethod(pcmClass, PacketUtils.COMPONENT_CLASS).invoke(pcm); if(chatBaseComp != null) ChatItem.debug("[PCMManager] Founded " + chatBaseComp.getClass().getSimpleName()); - return chatBaseComp == null ? null : PacketUtils.CHAT_SERIALIZER.getMethod("a", PacketUtils.COMPONENT_CLASS).invoke(null, chatBaseComp).toString(); + return chatBaseComp == null ? null : ChatItem.getPlatform().baseComponentToJson(chatBaseComp); } catch (Exception exc) { exc.printStackTrace(); } @@ -44,7 +44,7 @@ public String getBaseComponentAsJSON(ChatItemPacket packet) { @Override public void writeJson(ChatItemPacket packet, String json) { try { - Object chatComp = PacketUtils.CHAT_SERIALIZER.getMethod("a", String.class).invoke(null, json); + Object chatComp = ChatItem.getPlatform().jsonToBaseComponent(json); Class pcmClass = PacketUtils.getNmsClass("PlayerChatMessage", "network.chat."); ContentModifier pcmModifier = packet.getContent().getSpecificModifier((Class) pcmClass); Object pcm = ReflectionUtils.getMethod(pcmClass, pcmClass, PacketUtils.COMPONENT_CLASS).invoke(pcmModifier.readSafely(0), chatComp); diff --git a/src/main/java/me/dadus33/chatitem/chatmanager/v1/listeners/ChatPacketManager.java b/src/main/java/me/dadus33/chatitem/chatmanager/v1/listeners/ChatPacketManager.java index 183f59cd..3eb64eda 100644 --- a/src/main/java/me/dadus33/chatitem/chatmanager/v1/listeners/ChatPacketManager.java +++ b/src/main/java/me/dadus33/chatitem/chatmanager/v1/listeners/ChatPacketManager.java @@ -1,6 +1,5 @@ package me.dadus33.chatitem.chatmanager.v1.listeners; -import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -38,24 +37,11 @@ public class ChatPacketManager extends PacketHandler { private Object lastSentPacket = null; - private Method serializerGetJson; private PacketEditingChatManager manager; private final List componentManager = new ArrayList<>(); public ChatPacketManager(PacketEditingChatManager manager) { this.manager = manager; - try { - for (Method m : PacketUtils.CHAT_SERIALIZER.getDeclaredMethods()) { - if (m.getParameterCount() == 1 && m.getParameterTypes()[0].equals(PacketUtils.COMPONENT_CLASS) && m.getReturnType().equals(String.class)) { - serializerGetJson = m; - break; - } - } - if (serializerGetJson == null) - ChatItem.getInstance().getLogger().warning("Failed to find JSON serializer in class: " + PacketUtils.CHAT_SERIALIZER.getCanonicalName()); - } catch (Exception e) { - e.printStackTrace(); - } for (IComponentManager getter : Arrays.asList(new StringComponentManager(), new ComponentNMSManager(), new PCMComponentManager())) { tryRegister(getter); diff --git a/src/main/java/me/dadus33/chatitem/commands/ChatItemCommand.java b/src/main/java/me/dadus33/chatitem/commands/ChatItemCommand.java index d28a352f..0b061a9e 100644 --- a/src/main/java/me/dadus33/chatitem/commands/ChatItemCommand.java +++ b/src/main/java/me/dadus33/chatitem/commands/ChatItemCommand.java @@ -24,7 +24,6 @@ import me.dadus33.chatitem.listeners.InventoryListener; import me.dadus33.chatitem.utils.Colors; import me.dadus33.chatitem.utils.Messages; -import me.dadus33.chatitem.utils.PacketUtils; import me.dadus33.chatitem.utils.Utils; import net.md_5.bungee.api.chat.ClickEvent; import net.md_5.bungee.api.chat.TextComponent; @@ -35,7 +34,7 @@ public class ChatItemCommand implements CommandExecutor, TabExecutor { private static final List ORDERS; static { - if(PacketUtils.IS_PAPER) + if(Utils.IS_PAPER) ORDERS = Arrays.asList("packet", "chat", "paper", "all"); else ORDERS = Arrays.asList("packet", "chat", "all"); diff --git a/src/main/java/me/dadus33/chatitem/listeners/InventoryListener.java b/src/main/java/me/dadus33/chatitem/listeners/InventoryListener.java index 8c202b77..935e02e0 100644 --- a/src/main/java/me/dadus33/chatitem/listeners/InventoryListener.java +++ b/src/main/java/me/dadus33/chatitem/listeners/InventoryListener.java @@ -20,7 +20,7 @@ import me.dadus33.chatitem.listeners.holder.CustomInventoryHolder; import me.dadus33.chatitem.utils.ItemUtils; import me.dadus33.chatitem.utils.Messages; -import me.dadus33.chatitem.utils.PacketUtils; +import me.dadus33.chatitem.utils.Utils; public class InventoryListener implements Listener { @@ -115,7 +115,7 @@ public static void open(Player p) { int slot = 0; for (String manager : Arrays.asList("all", "auto", "packet", "chat", "paper")) { - if (manager == "paper" && !PacketUtils.IS_PAPER) + if (manager == "paper" && !Utils.IS_PAPER) continue; holder.keyBySlot.put(slot, manager); inv.setItem(slot++, getManagerItem(manager)); diff --git a/src/main/java/me/dadus33/chatitem/platform/IPlatform.java b/src/main/java/me/dadus33/chatitem/platform/IPlatform.java index a5a0159f..e36fd4bc 100644 --- a/src/main/java/me/dadus33/chatitem/platform/IPlatform.java +++ b/src/main/java/me/dadus33/chatitem/platform/IPlatform.java @@ -10,6 +10,7 @@ import org.bukkit.plugin.Plugin; import me.dadus33.chatitem.utils.Messages; +import me.dadus33.chatitem.utils.Version; public interface IPlatform { @@ -28,4 +29,14 @@ default ItemStack createItemStack(Material type, String name, String... lore) { ItemStack createItemStack(Material type, String name, List lore); String getPluginVersion(Plugin plugin); + + Version getMinecraftVersion(); + + String getNMSVersion(); + + boolean hasBaseComponentSerializer(); + + String baseComponentToJson(Object obj); + + Object jsonToBaseComponent(String json); } diff --git a/src/main/java/me/dadus33/chatitem/platform/hook/PaperPlatform.java b/src/main/java/me/dadus33/chatitem/platform/hook/PaperPlatform.java index 8bc9f811..7effb2af 100644 --- a/src/main/java/me/dadus33/chatitem/platform/hook/PaperPlatform.java +++ b/src/main/java/me/dadus33/chatitem/platform/hook/PaperPlatform.java @@ -1,5 +1,6 @@ package me.dadus33.chatitem.platform.hook; +import java.lang.reflect.Method; import java.util.List; import java.util.stream.Collectors; @@ -12,6 +13,7 @@ import org.bukkit.plugin.Plugin; import me.dadus33.chatitem.platform.IPlatform; +import me.dadus33.chatitem.utils.Version; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; @@ -45,4 +47,46 @@ public ItemStack createItemStack(Material type, String name, List lore) public String getPluginVersion(Plugin plugin) { return plugin.getPluginMeta().getVersion(); } + + @Override + public Version getMinecraftVersion() { + return Version.getVersionByName("v" + Bukkit.getMinecraftVersion().replace(".", "_")); + } + + @Override + public String getNMSVersion() { + String[] parts = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(","); + return parts.length <= 3 ? "" : parts[3]; + } + + @Override + public boolean hasBaseComponentSerializer() { + return Version.getVersion().isNewerOrEquals(Version.V1_20_6) ? false : SpigotPlatform.getBaseComponentToJsonMethod() != null; + } + + @Override + public String baseComponentToJson(Object obj) { + if(Version.getVersion().isNewerOrEquals(Version.V1_20_6)) + return null; + Method m = SpigotPlatform.getBaseComponentToJsonMethod(); + try { + return (String) m.invoke(null, obj); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + @Override + public Object jsonToBaseComponent(String json) { + if(Version.getVersion().isNewerOrEquals(Version.V1_20_6)) + return null; + Method m = SpigotPlatform.getJsonToBaseComponentMethod(); + try { + return m.invoke(null, json); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } } diff --git a/src/main/java/me/dadus33/chatitem/platform/hook/SpigotPlatform.java b/src/main/java/me/dadus33/chatitem/platform/hook/SpigotPlatform.java index a6e3e2d0..00ee065f 100644 --- a/src/main/java/me/dadus33/chatitem/platform/hook/SpigotPlatform.java +++ b/src/main/java/me/dadus33/chatitem/platform/hook/SpigotPlatform.java @@ -1,5 +1,6 @@ package me.dadus33.chatitem.platform.hook; +import java.lang.reflect.Method; import java.util.List; import org.bukkit.Bukkit; @@ -12,6 +13,8 @@ import me.dadus33.chatitem.platform.IPlatform; import me.dadus33.chatitem.utils.Colors; +import me.dadus33.chatitem.utils.PacketUtils; +import me.dadus33.chatitem.utils.Version; @SuppressWarnings("deprecation") public class SpigotPlatform implements IPlatform { @@ -45,4 +48,78 @@ public String getPluginVersion(Plugin plugin) { return plugin.getDescription().getVersion(); } + @Override + public Version getMinecraftVersion() { + return Version.getVersionByName(getNMSVersion()); + } + + @Override + public String getNMSVersion() { + return Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3]; + } + + @Override + public boolean hasBaseComponentSerializer() { + return getBaseComponentToJsonMethod() != null; + } + + @Override + public String baseComponentToJson(Object obj) { + Method m = getBaseComponentToJsonMethod(); + try { + Object[] args = new Object[m.getParameterCount()]; + args[0] = obj; + return (String) m.invoke(null, args); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + @Override + public Object jsonToBaseComponent(String json) { + Method m = getJsonToBaseComponentMethod(); + try { + Object[] args = new Object[m.getParameterCount()]; + args[0] = json; + return m.invoke(null, args); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + public static Method getBaseComponentToJsonMethod() { + Class chatSerializerClass = PacketUtils.getNmsClass("IChatBaseComponent$ChatSerializer", "network.chat.", "ChatSerializer"); + Class chatBaseComponentClass = PacketUtils.getNmsClass("IChatBaseComponent", "network.chat."); + if(chatSerializerClass == null || chatBaseComponentClass == null) + return null; + try { + for (Method m : chatSerializerClass.getDeclaredMethods()) { + if(m.getParameterTypes()[0].equals(chatBaseComponentClass) && m.getReturnType().equals(String.class)) { + return m; + } + } + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + public static Method getJsonToBaseComponentMethod() { + Class chatSerializerClass = PacketUtils.getNmsClass("IChatBaseComponent$ChatSerializer", "network.chat.", "ChatSerializer"); + Class chatBaseComponentClass = PacketUtils.getNmsClass("IChatBaseComponent", "network.chat."); + if(chatSerializerClass == null || chatBaseComponentClass == null) + return null; + try { + for (Method m : chatSerializerClass.getDeclaredMethods()) { + if(m.getParameterTypes()[0].equals(String.class) && m.getReturnType().equals(chatBaseComponentClass)) { + return m; + } + } + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } } diff --git a/src/main/java/me/dadus33/chatitem/utils/PacketUtils.java b/src/main/java/me/dadus33/chatitem/utils/PacketUtils.java index c665c3c3..8148c2b9 100644 --- a/src/main/java/me/dadus33/chatitem/utils/PacketUtils.java +++ b/src/main/java/me/dadus33/chatitem/utils/PacketUtils.java @@ -15,12 +15,10 @@ public class PacketUtils { - public static final String VERSION = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3]; public static final String NMS_PREFIX; public static final String OBC_PREFIX; - public static final boolean IS_THERMOS = isClassExist("thermos.Thermos"), IS_PAPER = isClassExist("io.papermc.paper.event.player.AsyncChatEvent"); - public static final Class CHAT_SERIALIZER, COMPONENT_CLASS; - public static final Method ICB_FROM_JSON; + public static final boolean IS_THERMOS = Utils.isClassExist("thermos.Thermos"); + public static final Class COMPONENT_CLASS; /** * This Map is to reduce Reflection action which take more resources than just @@ -29,11 +27,10 @@ public class PacketUtils { private static final Map> ALL_CLASS = Collections.synchronizedMap(new HashMap>()); static { - NMS_PREFIX = Version.getVersion().isNewerOrEquals(Version.V1_17) || IS_THERMOS ? "net.minecraft." : "net.minecraft.server." + VERSION + "."; - OBC_PREFIX = "org.bukkit.craftbukkit." + VERSION + "."; - CHAT_SERIALIZER = getNmsClass("IChatBaseComponent$ChatSerializer", "network.chat.", "ChatSerializer"); + String version = ChatItem.getPlatform().getNMSVersion(); + NMS_PREFIX = Version.getVersion().isNewerOrEquals(Version.V1_17) || IS_THERMOS ? "net.minecraft." : "net.minecraft.server." + (version == "" ? "" : version + "."); + OBC_PREFIX = "org.bukkit.craftbukkit." + (version == "" ? "" : version + "."); COMPONENT_CLASS = getNmsClass("IChatBaseComponent", "network.chat."); - ICB_FROM_JSON = getMethod("a", CHAT_SERIALIZER, String.class); } /** @@ -117,15 +114,6 @@ public static Object getEntityPlayer(Player p) { } } - public static boolean isClassExist(String name) { - try { - Class.forName(name); - return true; - } catch (Exception e) { - return false; - } - } - /** * Get NMS player connection of specified player * @@ -205,15 +193,6 @@ else if (v.equals(Version.V1_18)) } return "{}"; } - - private static Method getMethod(String name, Class clazz, Class... params) { - try { - return clazz.getDeclaredMethod(name, params); - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } public static void printPacketToDebug(Object packet) { if(!ChatItem.hasDebug()) diff --git a/src/main/java/me/dadus33/chatitem/utils/Utils.java b/src/main/java/me/dadus33/chatitem/utils/Utils.java index 4549523f..981e6c05 100644 --- a/src/main/java/me/dadus33/chatitem/utils/Utils.java +++ b/src/main/java/me/dadus33/chatitem/utils/Utils.java @@ -39,6 +39,7 @@ public class Utils { + public static final boolean IS_PAPER = isClassExist("io.papermc.paper.event.player.AsyncChatEvent"); private static final Pattern UUID_PATTERN = Pattern.compile("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"); private static final TreeMap ROMAN_KEYS = new TreeMap<>(); private static final HashMap ENCHANTS_NAMES = new HashMap<>(); @@ -259,4 +260,13 @@ public static String arrayToString(Object[] a) { sj.add(o == null ? "null" : o.toString()); return sj.toString(); } + + public static boolean isClassExist(String name) { + try { + Class.forName(name); + return true; + } catch (Exception e) { + return false; + } + } } diff --git a/src/main/java/me/dadus33/chatitem/utils/Version.java b/src/main/java/me/dadus33/chatitem/utils/Version.java index 71336492..83eb2ff6 100644 --- a/src/main/java/me/dadus33/chatitem/utils/Version.java +++ b/src/main/java/me/dadus33/chatitem/utils/Version.java @@ -2,8 +2,6 @@ import java.net.InetSocketAddress; -import org.bukkit.Bukkit; - import me.dadus33.chatitem.ChatItem; public enum Version { @@ -22,7 +20,8 @@ public enum Version { V1_17(755, 756, 17), V1_18(757, 758, 18), V1_19(759, 762, 19), - V1_20(763, 1000, 20), + V1_20(763, 765, 20), + V1_20_6(766, 1000, 20.6), HIGHER(Integer.MAX_VALUE, -1, Integer.MAX_VALUE); // Latest version should always have the upper limit set to Integer.MAX_VALUE so @@ -30,17 +29,11 @@ public enum Version { public final int MIN_VER; public final int MAX_VER; - public final int index; // Represents how new the version is (0 - extremely old) - - private static final Version SERVER_VERSION; - public static final String BUKKIT_VERSION; + public final double index; // Represents how new the version is (0 - extremely old) - static { - BUKKIT_VERSION = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3]; - SERVER_VERSION = getVersionByName(BUKKIT_VERSION); - } + private static final Version SERVER_VERSION = ChatItem.getPlatform().getMinecraftVersion(); - Version(int min, int max, int index) { + Version(int min, int max, double index) { this.MIN_VER = min; this.MAX_VER = max; this.index = index; @@ -55,6 +48,9 @@ public boolean isNewerThan(Version other) { } public static Version getVersionByName(String name) { + try { + return Version.valueOf(name.toUpperCase()); + } catch (Exception e) {} for (Version v : Version.values()) if (name.toLowerCase().startsWith(v.name().toLowerCase())) return v;