From 113701c5bf53d8ea940346719bda30124d1b54b9 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Thu, 8 Sep 2016 23:40:26 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=86=85=E9=83=A8?= =?UTF-8?q?=E5=BA=8F=E5=88=97=E5=8C=96=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- .../java/pw/yumc/YumCore/tellraw/Tellraw.java | 79 ++++++++++++++++++- 1 file changed, 78 insertions(+), 1 deletion(-) diff --git a/src/main/java/pw/yumc/YumCore/tellraw/Tellraw.java b/src/main/java/pw/yumc/YumCore/tellraw/Tellraw.java index a4948f8..4e2fc3a 100644 --- a/src/main/java/pw/yumc/YumCore/tellraw/Tellraw.java +++ b/src/main/java/pw/yumc/YumCore/tellraw/Tellraw.java @@ -1,5 +1,7 @@ package pw.yumc.YumCore.tellraw; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -8,6 +10,7 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; import pw.yumc.YumCore.bukkit.compatible.C; @@ -18,6 +21,14 @@ import pw.yumc.YumCore.bukkit.compatible.C; * @author 喵♂呜 */ public class Tellraw { + static ItemSerialize is; + static { + try { + is = new Automatic(); + } catch (ClassNotFoundException | NoSuchMethodException | SecurityException e) { + is = new Manual(); + } + } List messageParts = new ArrayList<>(); public Tellraw(final String text) { @@ -97,6 +108,28 @@ public class Tellraw { return this; } + /** + * 悬浮物品 + * + * @param item + * {@link ItemStack} + * @return {@link Tellraw} + */ + public Tellraw item(final ItemStack item) { + return item(is.$(item)); + } + + /** + * 悬浮物品 + * + * @param text + * 文本 + * @return {@link Tellraw} + */ + public Tellraw item(final String json) { + return onHover("show_item", json); + } + /** * 打开URL * @@ -246,7 +279,7 @@ public class Tellraw { public String toOldMessageFormat() { final StringBuilder result = new StringBuilder(); for (final MessagePart part : messageParts) { - result.append(ChatColor.stripColor(part.text)); + result.append(part.text); } return result.toString(); } @@ -308,4 +341,48 @@ public class Tellraw { } return this; } + + static class Automatic implements ItemSerialize { + Method asNMSCopyMethod; + Method nmsSaveNBTMethod; + Class nmsNBTTagCompound; + String ver = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3]; + + 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); + } + + @Override + public String $(final ItemStack item) { + try { + return nmsSaveNBTMethod.invoke(asNMSCopyMethod.invoke(null, item), nmsNBTTagCompound.newInstance()).toString(); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e) { + is = new Manual(); + return is.$(item); + } + } + + public Class getNMSClass(final String cname) throws ClassNotFoundException { + return Class.forName("net.minecraft.server" + ver + "." + cname); + } + + public Class getOBCClass(final String cname) throws ClassNotFoundException { + return Class.forName("org.bukkit.craftbukkit." + ver + "." + cname); + } + } + + static interface ItemSerialize { + public String $(ItemStack item); + } + + static class Manual implements ItemSerialize { + @Override + public String $(final ItemStack item) { + return null; + } + } }