1
0
mirror of https://e.coding.net/circlecloud/TellRaw.git synced 2024-12-27 07:28:51 +00:00

修改初始化流程...

Signed-off-by: 502647092 <jtb1@163.com>
This commit is contained in:
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,31 +121,43 @@ public abstract class FancyMessage implements JsonRepresentedObject, Cloneable,
} }
try { try {
_stringParser = GsonAgent.newJsonParser(); _stringParser = GsonAgent.newJsonParser();
if (!useProtocolLib) { try {
try { nmsPacketPlayOutChatConstructor = com.comphenix.protocol.PacketType.Play.Server.CHAT.getPacketClass().getDeclaredConstructor(MinecraftReflection.getIChatBaseComponentClass());
nmsPacketPlayOutChatConstructor = Reflection.getNMSClass("PacketPlayOutChat").getDeclaredConstructor(Reflection.getNMSClass("IChatBaseComponent")); } catch (final Exception | Error e) {
nmsPacketPlayOutChatConstructor.setAccessible(true); nmsPacketPlayOutChatConstructor = Reflection.getNMSClass("PacketPlayOutChat").getDeclaredConstructor(Reflection.getNMSClass("IChatBaseComponent"));
} 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;
}
} }
internalClass = FancyMessageInternal.class; nmsPacketPlayOutChatConstructor.setAccessible(true);
if (!useProtocolLib) { 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 { try {
obcCraftStatisticClass = Reflection.getOBCClass("CraftStatistic"); obcCraftStatisticClass = Reflection.getOBCClass("CraftStatistic");
getNMSStatisticMethod = Reflection.getMethod(obcCraftStatisticClass, "getNMSStatistic", Statistic.class); getNMSStatisticMethod = Reflection.getMethod(obcCraftStatisticClass, "getNMSStatistic", Statistic.class);
@ -169,9 +181,9 @@ public abstract class FancyMessage implements JsonRepresentedObject, Cloneable,
nmsItemStack = Reflection.getNMSClass("ItemStack"); nmsItemStack = Reflection.getNMSClass("ItemStack");
nmsNBTTagCompound = Reflection.getNMSClass("NBTTagCompound"); nmsNBTTagCompound = Reflection.getNMSClass("NBTTagCompound");
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 = Reflection.getNMSClass("Packet"); nmsPacketClass = Reflection.getNMSClass("Packet");
try { try {
@ -183,42 +195,6 @@ public abstract class FancyMessage implements JsonRepresentedObject, Cloneable,
} catch (final Exception e) { } catch (final Exception e) {
throw 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) { if (nmsChatSerializerGsonInstance == null) {
// Find the field and its value, completely bypassing obfuscation // 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"); 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;