From b3c0a31cbf259935e9e25d2f937a8173f707e45c Mon Sep 17 00:00:00 2001 From: 502647092 Date: Mon, 12 Sep 2016 16:11:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0MOD=E6=9C=8D=E5=85=BC?= =?UTF-8?q?=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- .../yumc/YumCore/tellraw/ItemSerialize.java | 36 ++++++++++++++----- .../java/pw/yumc/YumCore/tellraw/Tellraw.java | 6 ---- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/main/java/pw/yumc/YumCore/tellraw/ItemSerialize.java b/src/main/java/pw/yumc/YumCore/tellraw/ItemSerialize.java index cb90dee..3e0467f 100644 --- a/src/main/java/pw/yumc/YumCore/tellraw/ItemSerialize.java +++ b/src/main/java/pw/yumc/YumCore/tellraw/ItemSerialize.java @@ -30,9 +30,13 @@ public abstract class ItemSerialize { } public static String $(final ItemStack item) { - return itemSerialize.parse(item); + final String result = itemSerialize.parse(item); + Log.debug(String.format("%s物品序列化结果: %s", itemSerialize.getName(), result)); + return result; } + public abstract String getName(); + public abstract String parse(final ItemStack item); static class Automatic extends ItemSerialize { @@ -44,13 +48,25 @@ public abstract class ItemSerialize { public Automatic() throws ClassNotFoundException, NoSuchMethodException, SecurityException { final Class cis = getOBCClass("inventory.CraftItemStack"); asNMSCopyMethod = cis.getMethod("asNMSCopy", ItemStack.class); - final Class nmsItemStack = getNMSClass("ItemStack"); - nmsNBTTagCompound = getNMSClass("NBTTagCompound"); - nmsSaveNBTMethod = nmsItemStack.getMethod("save", nmsNBTTagCompound); + final Class nmsItemStack = asNMSCopyMethod.getReturnType(); + for (final Method method : nmsItemStack.getMethods()) { + final Class rt = method.getReturnType(); + if (method.getParameterTypes().length == 0 && rt.getSimpleName().equals("NBTTagCompound")) { + nmsNBTTagCompound = rt; + } + } + for (final Method method : nmsItemStack.getMethods()) { + final Class[] paras = method.getParameterTypes(); + final Class rt = method.getReturnType(); + if (paras.length == 1 && paras[0].getSimpleName().equals("NBTTagCompound") && rt.getSimpleName().equals("NBTTagCompound")) { + nmsSaveNBTMethod = method; + } + } } - public Class getNMSClass(final String cname) throws ClassNotFoundException { - return Class.forName("net.minecraft.server." + ver + "." + cname); + @Override + public String getName() { + return "Automatic"; } public Class getOBCClass(final String cname) throws ClassNotFoundException { @@ -70,6 +86,11 @@ public abstract class ItemSerialize { static class Manual extends ItemSerialize { + @Override + public String getName() { + return "Manual"; + } + @Override public String parse(final ItemStack item) { return serialize(item); @@ -92,7 +113,7 @@ public abstract class ItemSerialize { display.append("Lore:["); int i = 0; for (final String line : im.getLore()) { - display.append(String.format("%s:%s,", i, new JsonBuilder(line).toString())); + display.append(String.format("%s:\"%s\",", i, new JsonBuilder(line).toString())); i++; } display.deleteCharAt(display.length() - 1); @@ -131,7 +152,6 @@ public abstract class ItemSerialize { if (im.hasEnchants()) { meta.append(String.format("ench:[%s],", getEnch(im.getEnchants().entrySet()))); } - im.getItemFlags(); if (im.hasDisplayName() || im.hasLore()) { meta.append(String.format("display:%s,", getDisplay(im))); } diff --git a/src/main/java/pw/yumc/YumCore/tellraw/Tellraw.java b/src/main/java/pw/yumc/YumCore/tellraw/Tellraw.java index 34ecb38..0e8b4dc 100644 --- a/src/main/java/pw/yumc/YumCore/tellraw/Tellraw.java +++ b/src/main/java/pw/yumc/YumCore/tellraw/Tellraw.java @@ -6,7 +6,6 @@ import java.util.List; import org.bukkit.Bukkit; import org.bukkit.ChatColor; -import org.bukkit.Material; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; @@ -48,11 +47,6 @@ public class Tellraw { return new Tellraw(text); } - public static void main(final String[] args) { - final ItemStack item = new ItemStack(Material.DIAMOND_SWORD); - System.out.println(Tellraw.create("命令").command("yum list").item(item).toJsonString()); - } - /** * 发送Tellraw公告 */