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:
parent
82f150fd9d
commit
77b65a77da
@ -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,12 +41,14 @@ 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 {
|
||||||
|
try {
|
||||||
Class<?> cis = getOBCClass("inventory.CraftItemStack");
|
Class<?> cis = getOBCClass("inventory.CraftItemStack");
|
||||||
asNMSCopyMethod = cis.getMethod("asNMSCopy", ItemStack.class);
|
asNMSCopyMethod = cis.getMethod("asNMSCopy", ItemStack.class);
|
||||||
Class<?> nmsItemStack = asNMSCopyMethod.getReturnType();
|
Class<?> nmsItemStack = asNMSCopyMethod.getReturnType();
|
||||||
@ -62,6 +65,14 @@ public abstract class ItemSerialize {
|
|||||||
nmsSaveNBTMethod = method;
|
nmsSaveNBTMethod = method;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
inited = true;
|
||||||
|
} catch (ClassNotFoundException | NoSuchMethodException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Automatic() {
|
||||||
|
if (!inited) { throw new IllegalStateException("无法初始化自动处理类!"); }
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -69,7 +80,7 @@ public abstract class ItemSerialize {
|
|||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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.getScheduler().runTask(P.instance, new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), "tellraw " + sender.getName() + " " + json);
|
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());
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user