mirror of
https://e.coding.net/circlecloud/YumCore.git
synced 2024-12-24 06:58:51 +00:00
feat: 添加内部序列化方法
Signed-off-by: 502647092 <admin@yumc.pw>
This commit is contained in:
parent
e5af3676d3
commit
113701c5bf
@ -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<MessagePart> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user