修改初始化流程...

Signed-off-by: 502647092 <jtb1@163.com>
master
502647092 2016-01-13 16:31:43 +08:00
parent c6f9bdcae4
commit d65ba8f5f3
2 changed files with 42 additions and 65 deletions

View File

@ -76,7 +76,7 @@ public abstract class FancyMessage implements JsonRepresentedObject, Cloneable,
protected static Method getNMSEntityStatisticMethod = null; protected static Method getNMSEntityStatisticMethod = null;
protected static Method getNMSMaterialStatisticMethod = null; protected static Method getNMSMaterialStatisticMethod = null;
protected static Method getNMSsaveNBTMethod = null; protected static Method getNMSSaveNBTMethod = null;
protected static Method getNMSStatisticMethod = null; protected static Method getNMSStatisticMethod = null;
protected static Method getOBCasNMSCopyMethod = null; protected static Method getOBCasNMSCopyMethod = null;
@ -121,69 +121,12 @@ public abstract class FancyMessage implements JsonRepresentedObject, Cloneable,
} }
try { try {
_stringParser = GsonAgent.newJsonParser(); _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 { try {
nmsPacketPlayOutChatConstructor = com.comphenix.protocol.PacketType.Play.Server.CHAT.getPacketClass().getDeclaredConstructor(MinecraftReflection.getIChatBaseComponentClass()); 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"));
}
nmsPacketPlayOutChatConstructor.setAccessible(true); 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;
}
}
internalClass = FancyMessageInternal.class;
if (!useProtocolLib) {
try {
obcCraftStatisticClass = Reflection.getOBCClass("CraftStatistic");
getNMSStatisticMethod = Reflection.getMethod(obcCraftStatisticClass, "getNMSStatistic", Statistic.class);
nmsStatisticClass = Reflection.getNMSClass("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 = Reflection.getNMSClass("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 = Reflection.getOBCClass("inventory.CraftItemStack");
getOBCasNMSCopyMethod = Reflection.getMethod(obcCraftItemStack, "asNMSCopy", ItemStack.class);
nmsItemStack = Reflection.getNMSClass("ItemStack");
nmsNBTTagCompound = Reflection.getNMSClass("NBTTagCompound");
try {
getNMSsaveNBTMethod = Reflection.getMethod(nmsItemStack, "save", nmsNBTTagCompound);
} catch (final Exception e) {
getNMSsaveNBTMethod = Reflection.getMethodByParamsAndType(nmsItemStack, nmsNBTTagCompound, nmsNBTTagCompound).get(0);
}
nmsPacketClass = Reflection.getNMSClass("Packet");
try {
chatSerializerClazz = Reflection.getNMSClass("IChatBaseComponent$ChatSerializer");
} catch (final Exception e) {
chatSerializerClazz = Reflection.getNMSClass("ChatSerializer");
}
nmsIChatBaseComponentClass = Reflection.getNMSClass("IChatBaseComponent");
} catch (final Exception e) {
throw e;
}
} else {
try { try {
obcCraftStatisticClass = MinecraftReflection.getCraftBukkitClass("CraftStatistic"); obcCraftStatisticClass = MinecraftReflection.getCraftBukkitClass("CraftStatistic");
getNMSStatisticMethod = Reflection.getMethod(obcCraftStatisticClass, "getNMSStatistic", Statistic.class); getNMSStatisticMethod = Reflection.getMethod(obcCraftStatisticClass, "getNMSStatistic", Statistic.class);
@ -207,15 +150,48 @@ public abstract class FancyMessage implements JsonRepresentedObject, Cloneable,
nmsItemStack = MinecraftReflection.getItemStackClass(); nmsItemStack = MinecraftReflection.getItemStackClass();
nmsNBTTagCompound = MinecraftReflection.getNBTCompoundClass(); nmsNBTTagCompound = MinecraftReflection.getNBTCompoundClass();
try { try {
getNMSsaveNBTMethod = Reflection.getMethod(nmsItemStack, "save", nmsNBTTagCompound); getNMSSaveNBTMethod = Reflection.getMethod(nmsItemStack, "save", nmsNBTTagCompound);
} catch (final Exception e) { } catch (final Exception e) {
getNMSsaveNBTMethod = Reflection.getMethodByParamsAndType(nmsItemStack, nmsNBTTagCompound, nmsNBTTagCompound).get(0); getNMSSaveNBTMethod = Reflection.getMethodByParamsAndType(nmsItemStack, nmsNBTTagCompound, nmsNBTTagCompound).get(0);
} }
nmsPacketClass = MinecraftReflection.getPacketClass(); nmsPacketClass = MinecraftReflection.getPacketClass();
chatSerializerClazz = MinecraftReflection.getChatSerializerClass(); 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(); nmsIChatBaseComponentClass = MinecraftReflection.getIChatBaseComponentClass();
} catch (final Exception | Error ex) {
try {
obcCraftStatisticClass = Reflection.getOBCClass("CraftStatistic");
getNMSStatisticMethod = Reflection.getMethod(obcCraftStatisticClass, "getNMSStatistic", Statistic.class);
nmsStatisticClass = Reflection.getNMSClass("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 = Reflection.getNMSClass("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 = Reflection.getOBCClass("inventory.CraftItemStack");
getOBCasNMSCopyMethod = Reflection.getMethod(obcCraftItemStack, "asNMSCopy", ItemStack.class);
nmsItemStack = Reflection.getNMSClass("ItemStack");
nmsNBTTagCompound = Reflection.getNMSClass("NBTTagCompound");
try {
getNMSSaveNBTMethod = Reflection.getMethod(nmsItemStack, "save", nmsNBTTagCompound);
} catch (final Exception e) {
getNMSSaveNBTMethod = Reflection.getMethodByParamsAndType(nmsItemStack, nmsNBTTagCompound, nmsNBTTagCompound).get(0);
}
nmsPacketClass = Reflection.getNMSClass("Packet");
try {
chatSerializerClazz = Reflection.getNMSClass("IChatBaseComponent$ChatSerializer");
} catch (final Exception e) {
chatSerializerClazz = Reflection.getNMSClass("ChatSerializer");
}
nmsIChatBaseComponentClass = Reflection.getNMSClass("IChatBaseComponent");
} catch (final Exception e) { } catch (final Exception e) {
throw e; throw e;
} }
@ -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"); throw new ClassNotFoundException("Can't find the NBTMethod or NBTTagCompound class");
} }
internalClass = FancyMessageInternal.class;
if (!useProtocolLib) { if (!useProtocolLib) {
Bukkit.getLogger().info("[TellRawLib] 使用ChatSerializer序列化类处理..."); Bukkit.getLogger().info("[TellRawLib] 使用ChatSerializer序列化类处理...");
} else { } else {

View File

@ -77,7 +77,7 @@ public class FancyMessageInternal extends FancyMessage {
public FancyMessage itemTooltip(final ItemStack itemStack) { public FancyMessage itemTooltip(final ItemStack itemStack) {
try { try {
final Object nmsItem = getOBCasNMSCopyMethod.invoke(null, itemStack); 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) { } catch (final Exception e) {
e.printStackTrace(); e.printStackTrace();
return this; return this;