1
0
mirror of https://e.coding.net/circlecloud/YumCore.git synced 2024-11-22 01:48:50 +00:00

feat: 添加内部序列化方法

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-09-08 23:40:26 +08:00
parent e5af3676d3
commit 113701c5bf

View File

@ -1,5 +1,7 @@
package pw.yumc.YumCore.tellraw; package pw.yumc.YumCore.tellraw;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -8,6 +10,7 @@ import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import pw.yumc.YumCore.bukkit.compatible.C; import pw.yumc.YumCore.bukkit.compatible.C;
@ -18,6 +21,14 @@ import pw.yumc.YumCore.bukkit.compatible.C;
* @author * @author
*/ */
public class Tellraw { public class Tellraw {
static ItemSerialize is;
static {
try {
is = new Automatic();
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException e) {
is = new Manual();
}
}
List<MessagePart> messageParts = new ArrayList<>(); List<MessagePart> messageParts = new ArrayList<>();
public Tellraw(final String text) { public Tellraw(final String text) {
@ -97,6 +108,28 @@ public class Tellraw {
return this; 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 * 打开URL
* *
@ -246,7 +279,7 @@ public class Tellraw {
public String toOldMessageFormat() { public String toOldMessageFormat() {
final StringBuilder result = new StringBuilder(); final StringBuilder result = new StringBuilder();
for (final MessagePart part : messageParts) { for (final MessagePart part : messageParts) {
result.append(ChatColor.stripColor(part.text)); result.append(part.text);
} }
return result.toString(); return result.toString();
} }
@ -308,4 +341,48 @@ public class Tellraw {
} }
return this; 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;
}
}
} }