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

feat: 优化TellRaw处理

Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
502647092 2016-12-16 15:41:23 +08:00
parent 82f150fd9d
commit 77b65a77da
2 changed files with 39 additions and 39 deletions

View File

@ -1,17 +1,18 @@
package pw.yumc.YumCore.tellraw; package pw.yumc.YumCore.tellraw;
import org.bukkit.Bukkit;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import pw.yumc.YumCore.bukkit.Log;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import pw.yumc.YumCore.bukkit.Log;
/** /**
* 物品序列化类 * 物品序列化类
* *
@ -23,7 +24,7 @@ public abstract class ItemSerialize {
static { static {
try { try {
itemSerialize = new Automatic(); itemSerialize = new Automatic();
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException e) { } catch (IllegalStateException e) {
itemSerialize = new Manual(); itemSerialize = new Manual();
Log.d("初始化自动物品序列化失败!", e); Log.d("初始化自动物品序列化失败!", e);
} }
@ -31,7 +32,7 @@ public abstract class ItemSerialize {
public static String $(ItemStack item) { public static String $(ItemStack item) {
String result = itemSerialize.parse(item); String result = itemSerialize.parse(item);
Log.d("%s物品序列化结果: %s", itemSerialize.getName(), result); Log.d("%s 物品序列化结果: %s", itemSerialize.getName(), result);
return result; return result;
} }
@ -40,36 +41,46 @@ public abstract class ItemSerialize {
public abstract String parse(ItemStack item); public abstract String parse(ItemStack item);
static class Automatic extends ItemSerialize { static class Automatic extends ItemSerialize {
Method asNMSCopyMethod; private static boolean inited = false;
Method nmsSaveNBTMethod; private static Method asNMSCopyMethod;
Class<?> nmsNBTTagCompound; private static Method nmsSaveNBTMethod;
String ver = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3]; private static Class<?> nmsNBTTagCompound;
private static String ver = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];
public Automatic() throws ClassNotFoundException, NoSuchMethodException, SecurityException { static {
Class<?> cis = getOBCClass("inventory.CraftItemStack"); try {
asNMSCopyMethod = cis.getMethod("asNMSCopy", ItemStack.class); Class<?> cis = getOBCClass("inventory.CraftItemStack");
Class<?> nmsItemStack = asNMSCopyMethod.getReturnType(); asNMSCopyMethod = cis.getMethod("asNMSCopy", ItemStack.class);
for (Method method : nmsItemStack.getMethods()) { Class<?> nmsItemStack = asNMSCopyMethod.getReturnType();
Class<?> rt = method.getReturnType(); for (Method method : nmsItemStack.getMethods()) {
if (method.getParameterTypes().length == 0 && "NBTTagCompound".equals(rt.getSimpleName())) { Class<?> rt = method.getReturnType();
nmsNBTTagCompound = rt; if (method.getParameterTypes().length == 0 && "NBTTagCompound".equals(rt.getSimpleName())) {
nmsNBTTagCompound = rt;
}
} }
} for (Method method : nmsItemStack.getMethods()) {
for (Method method : nmsItemStack.getMethods()) { Class<?>[] paras = method.getParameterTypes();
Class<?>[] paras = method.getParameterTypes(); Class<?> rt = method.getReturnType();
Class<?> rt = method.getReturnType(); if (paras.length == 1 && "NBTTagCompound".equals(paras[0].getSimpleName()) && "NBTTagCompound".equals(rt.getSimpleName())) {
if (paras.length == 1 && "NBTTagCompound".equals(paras[0].getSimpleName()) && "NBTTagCompound".equals(rt.getSimpleName())) { nmsSaveNBTMethod = method;
nmsSaveNBTMethod = method; }
} }
inited = true;
} catch (ClassNotFoundException | NoSuchMethodException e) {
e.printStackTrace();
} }
} }
public Automatic() {
if (!inited) { throw new IllegalStateException("无法初始化自动处理类!"); }
}
@Override @Override
public String getName() { public String getName() {
return "Automatic"; return "Automatic";
} }
public Class getOBCClass(String cname) throws ClassNotFoundException { private static Class getOBCClass(String cname) throws ClassNotFoundException {
return Class.forName("org.bukkit.craftbukkit." + ver + "." + cname); return Class.forName("org.bukkit.craftbukkit." + ver + "." + cname);
} }

View File

@ -11,7 +11,6 @@ import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import pw.yumc.YumCore.bukkit.Log; import pw.yumc.YumCore.bukkit.Log;
import pw.yumc.YumCore.bukkit.P;
import pw.yumc.YumCore.bukkit.compatible.C; import pw.yumc.YumCore.bukkit.compatible.C;
/** /**
@ -21,7 +20,6 @@ import pw.yumc.YumCore.bukkit.compatible.C;
* @author * @author
*/ */
public class Tellraw implements Cloneable { public class Tellraw implements Cloneable {
static boolean isPaper = Bukkit.getVersion().contains("Paper") || Bukkit.getVersion().contains("Torch");
private List<MessagePart> messageParts = new ArrayList<>(); private List<MessagePart> messageParts = new ArrayList<>();
public Tellraw(String text) { public Tellraw(String text) {
@ -163,16 +161,7 @@ public class Tellraw implements Cloneable {
public void send(final CommandSender sender) { public void send(final CommandSender sender) {
final String json = toJsonString(); final String json = toJsonString();
if (sender instanceof Player && json.getBytes().length < 32000) { if (sender instanceof Player && json.getBytes().length < 32000) {
if (isPaper && !Bukkit.isPrimaryThread()) { Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + sender.getName() + " " + json);
Bukkit.getScheduler().runTask(P.instance, new Runnable() {
@Override
public void run() {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + sender.getName() + " " + json);
}
});
} else {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + sender.getName() + " " + json);
}
} else { } else {
sender.sendMessage(toOldMessageFormat()); sender.sendMessage(toOldMessageFormat());
} }