diff --git a/src/main/java/cn/citycraft/TellRaw/FancyMessage.java b/src/main/java/cn/citycraft/TellRaw/FancyMessage.java index 54721f8..940f671 100644 --- a/src/main/java/cn/citycraft/TellRaw/FancyMessage.java +++ b/src/main/java/cn/citycraft/TellRaw/FancyMessage.java @@ -76,7 +76,7 @@ public abstract class FancyMessage implements JsonRepresentedObject, Cloneable, protected static Method getNMSEntityStatisticMethod = null; protected static Method getNMSMaterialStatisticMethod = null; - protected static Method getNMSsaveNBTMethod = null; + protected static Method getNMSSaveNBTMethod = null; protected static Method getNMSStatisticMethod = null; protected static Method getOBCasNMSCopyMethod = null; @@ -121,31 +121,43 @@ public abstract class FancyMessage implements JsonRepresentedObject, Cloneable, } try { _stringParser = GsonAgent.newJsonParser(); - if (!useProtocolLib) { - try { - nmsPacketPlayOutChatConstructor = Reflection.getNMSClass("PacketPlayOutChat").getDeclaredConstructor(Reflection.getNMSClass("IChatBaseComponent")); - nmsPacketPlayOutChatConstructor.setAccessible(true); - } catch (final NoSuchMethodException e) { - Bukkit.getLogger().log(Level.SEVERE, "Could not find Minecraft method or constructor.", e); - throw e; - } catch (final SecurityException e) { - Bukkit.getLogger().log(Level.WARNING, "Could not access constructor.", e); - throw e; - } - } else { - try { - nmsPacketPlayOutChatConstructor = com.comphenix.protocol.PacketType.Play.Server.CHAT.getPacketClass().getDeclaredConstructor(MinecraftReflection.getIChatBaseComponentClass()); - nmsPacketPlayOutChatConstructor.setAccessible(true); - } catch (final NoSuchMethodException ex) { - Bukkit.getLogger().log(Level.SEVERE, "Could not find Minecraft method or constructor.", ex); - throw ex; - } catch (final SecurityException e) { - Bukkit.getLogger().log(Level.WARNING, "Could not access constructor.", e); - throw e; - } + try { + nmsPacketPlayOutChatConstructor = com.comphenix.protocol.PacketType.Play.Server.CHAT.getPacketClass().getDeclaredConstructor(MinecraftReflection.getIChatBaseComponentClass()); + } catch (final Exception | Error e) { + nmsPacketPlayOutChatConstructor = Reflection.getNMSClass("PacketPlayOutChat").getDeclaredConstructor(Reflection.getNMSClass("IChatBaseComponent")); } - internalClass = FancyMessageInternal.class; - if (!useProtocolLib) { + nmsPacketPlayOutChatConstructor.setAccessible(true); + try { + obcCraftStatisticClass = MinecraftReflection.getCraftBukkitClass("CraftStatistic"); + getNMSStatisticMethod = Reflection.getMethod(obcCraftStatisticClass, "getNMSStatistic", Statistic.class); + nmsStatisticClass = MinecraftReflection.getMinecraftClass("Statistic"); + try { + nmsStatistic_NameField = Reflection.getField(nmsStatisticClass, "name"); + } catch (final Exception e) { + nmsStatistic_NameField = Reflection.getDeclaredFieldByType(nmsStatisticClass, String.class).get(0); + } + getNMSMaterialStatisticMethod = Reflection.getMethod(obcCraftStatisticClass, "getMaterialStatistic", Statistic.class, Material.class); + getNMSEntityStatisticMethod = Reflection.getMethod(obcCraftStatisticClass, "getEntityStatistic", Statistic.class, EntityType.class); + nmsAchievementClass = MinecraftReflection.getMinecraftClass("Achievement"); + getNMSAchievementMethod = Reflection.getMethod(obcCraftStatisticClass, "getNMSAchievement", Achievement.class); + try { + nmsAchievement_NameField = Reflection.getField(nmsAchievementClass, "name"); + } catch (final Exception e) { + nmsAchievement_NameField = Reflection.getDeclaredFieldByType(nmsAchievementClass, String.class).get(0); + } + obcCraftItemStack = MinecraftReflection.getCraftItemStackClass(); + getOBCasNMSCopyMethod = Reflection.getMethod(obcCraftItemStack, "asNMSCopy", ItemStack.class); + nmsItemStack = MinecraftReflection.getItemStackClass(); + nmsNBTTagCompound = MinecraftReflection.getNBTCompoundClass(); + try { + getNMSSaveNBTMethod = Reflection.getMethod(nmsItemStack, "save", nmsNBTTagCompound); + } catch (final Exception e) { + getNMSSaveNBTMethod = Reflection.getMethodByParamsAndType(nmsItemStack, nmsNBTTagCompound, nmsNBTTagCompound).get(0); + } + nmsPacketClass = MinecraftReflection.getPacketClass(); + chatSerializerClazz = MinecraftReflection.getChatSerializerClass(); + nmsIChatBaseComponentClass = MinecraftReflection.getIChatBaseComponentClass(); + } catch (final Exception | Error ex) { try { obcCraftStatisticClass = Reflection.getOBCClass("CraftStatistic"); getNMSStatisticMethod = Reflection.getMethod(obcCraftStatisticClass, "getNMSStatistic", Statistic.class); @@ -169,9 +181,9 @@ public abstract class FancyMessage implements JsonRepresentedObject, Cloneable, nmsItemStack = Reflection.getNMSClass("ItemStack"); nmsNBTTagCompound = Reflection.getNMSClass("NBTTagCompound"); try { - getNMSsaveNBTMethod = Reflection.getMethod(nmsItemStack, "save", nmsNBTTagCompound); + getNMSSaveNBTMethod = Reflection.getMethod(nmsItemStack, "save", nmsNBTTagCompound); } catch (final Exception e) { - getNMSsaveNBTMethod = Reflection.getMethodByParamsAndType(nmsItemStack, nmsNBTTagCompound, nmsNBTTagCompound).get(0); + getNMSSaveNBTMethod = Reflection.getMethodByParamsAndType(nmsItemStack, nmsNBTTagCompound, nmsNBTTagCompound).get(0); } nmsPacketClass = Reflection.getNMSClass("Packet"); try { @@ -183,42 +195,6 @@ public abstract class FancyMessage implements JsonRepresentedObject, Cloneable, } catch (final Exception e) { throw e; } - } else { - try { - obcCraftStatisticClass = MinecraftReflection.getCraftBukkitClass("CraftStatistic"); - getNMSStatisticMethod = Reflection.getMethod(obcCraftStatisticClass, "getNMSStatistic", Statistic.class); - nmsStatisticClass = MinecraftReflection.getMinecraftClass("Statistic"); - try { - nmsStatistic_NameField = Reflection.getField(nmsStatisticClass, "name"); - } catch (final Exception e) { - nmsStatistic_NameField = Reflection.getDeclaredFieldByType(nmsStatisticClass, String.class).get(0); - } - getNMSMaterialStatisticMethod = Reflection.getMethod(obcCraftStatisticClass, "getMaterialStatistic", Statistic.class, Material.class); - getNMSEntityStatisticMethod = Reflection.getMethod(obcCraftStatisticClass, "getEntityStatistic", Statistic.class, EntityType.class); - nmsAchievementClass = MinecraftReflection.getMinecraftClass("Achievement"); - getNMSAchievementMethod = Reflection.getMethod(obcCraftStatisticClass, "getNMSAchievement", Achievement.class); - try { - nmsAchievement_NameField = Reflection.getField(nmsAchievementClass, "name"); - } catch (final Exception e) { - nmsAchievement_NameField = Reflection.getDeclaredFieldByType(nmsAchievementClass, String.class).get(0); - } - obcCraftItemStack = MinecraftReflection.getCraftItemStackClass(); - getOBCasNMSCopyMethod = Reflection.getMethod(obcCraftItemStack, "asNMSCopy", ItemStack.class); - nmsItemStack = MinecraftReflection.getItemStackClass(); - nmsNBTTagCompound = MinecraftReflection.getNBTCompoundClass(); - try { - getNMSsaveNBTMethod = Reflection.getMethod(nmsItemStack, "save", nmsNBTTagCompound); - } catch (final Exception e) { - getNMSsaveNBTMethod = Reflection.getMethodByParamsAndType(nmsItemStack, nmsNBTTagCompound, nmsNBTTagCompound).get(0); - } - nmsPacketClass = MinecraftReflection.getPacketClass(); - chatSerializerClazz = MinecraftReflection.getChatSerializerClass(); - // Since the method is so simple, and all the obfuscated methods have the same name, it's easier to reimplement 'IChatBaseComponent a(String)' than to reflectively call it - // Of course, the implementation may change, but fuzzy matches might break with signature changes - nmsIChatBaseComponentClass = MinecraftReflection.getIChatBaseComponentClass(); - } catch (final Exception e) { - throw e; - } } if (nmsChatSerializerGsonInstance == null) { // Find the field and its value, completely bypassing obfuscation @@ -235,9 +211,10 @@ public abstract class FancyMessage implements JsonRepresentedObject, Cloneable, } } } - if (getNMSsaveNBTMethod == null || nmsNBTTagCompound == null) { + if (getNMSSaveNBTMethod == null || nmsNBTTagCompound == null) { throw new ClassNotFoundException("Can't find the NBTMethod or NBTTagCompound class"); } + internalClass = FancyMessageInternal.class; if (!useProtocolLib) { Bukkit.getLogger().info("[TellRawLib] 使用ChatSerializer序列化类处理..."); } else { diff --git a/src/main/java/cn/citycraft/TellRaw/internal/FancyMessageInternal.java b/src/main/java/cn/citycraft/TellRaw/internal/FancyMessageInternal.java index 3dbff44..07edee2 100644 --- a/src/main/java/cn/citycraft/TellRaw/internal/FancyMessageInternal.java +++ b/src/main/java/cn/citycraft/TellRaw/internal/FancyMessageInternal.java @@ -77,7 +77,7 @@ public class FancyMessageInternal extends FancyMessage { public FancyMessage itemTooltip(final ItemStack itemStack) { try { final Object nmsItem = getOBCasNMSCopyMethod.invoke(null, itemStack); - return itemTooltip(getNMSsaveNBTMethod.invoke(nmsItem, nmsNBTTagCompound.newInstance()).toString()); + return itemTooltip(getNMSSaveNBTMethod.invoke(nmsItem, nmsNBTTagCompound.newInstance()).toString()); } catch (final Exception e) { e.printStackTrace(); return this;