perf: 优化 NBT API 调用
This commit is contained in:
@@ -5,6 +5,9 @@ import cn.hamster3.mc.plugin.core.bukkit.command.ParentCommand;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.constant.CoreMessage;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.util.CoreBukkitUtils;
|
||||
import de.tr7zw.changeme.nbtapi.*;
|
||||
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT;
|
||||
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBTCompoundList;
|
||||
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBTList;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -62,13 +65,12 @@ public class NBTCommand extends ParentCommand {
|
||||
sender.sendMessage("§c你的手持物品为空");
|
||||
return true;
|
||||
}
|
||||
NBTItem nbtItem = new NBTItem(stack);
|
||||
sendNBTCompound(nbtItem, sender, 0);
|
||||
ReadWriteNBT readWriteNBT = NBT.itemStackToNBT(stack);
|
||||
sendNBTCompound(readWriteNBT, sender, 0);
|
||||
return true;
|
||||
}
|
||||
|
||||
@SuppressWarnings("ForLoopReplaceableByForEach")
|
||||
private void sendNBTCompound(NBTCompound compound, CommandSender sender, int level) {
|
||||
private void sendNBTCompound(ReadWriteNBT compound, CommandSender sender, int level) {
|
||||
StringBuilder prefixBuilder = new StringBuilder();
|
||||
for (int i = 0; i < level; i++) {
|
||||
prefixBuilder.append(" ");
|
||||
@@ -137,7 +139,7 @@ public class NBTCommand extends ParentCommand {
|
||||
String listSpace = space + " ";
|
||||
switch (listType) {
|
||||
case NBTTagString: {
|
||||
NBTList<String> list = compound.getStringList(key);
|
||||
ReadWriteNBTList<String> list = compound.getStringList(key);
|
||||
for (String string : list) {
|
||||
sender.sendMessage(String.format("%s- %s", listSpace, string));
|
||||
}
|
||||
@@ -145,7 +147,7 @@ public class NBTCommand extends ParentCommand {
|
||||
}
|
||||
case NBTTagIntArray: {
|
||||
boolean all4Length = true;
|
||||
NBTList<int[]> list = compound.getIntArrayList(key);
|
||||
ReadWriteNBTList<int[]> list = compound.getIntArrayList(key);
|
||||
for (int[] intArray : list) {
|
||||
if (intArray.length != 4) {
|
||||
all4Length = false;
|
||||
@@ -153,7 +155,7 @@ public class NBTCommand extends ParentCommand {
|
||||
}
|
||||
}
|
||||
if (all4Length) {
|
||||
NBTList<UUID> uuidList = compound.getUUIDList(key);
|
||||
ReadWriteNBTList<UUID> uuidList = compound.getUUIDList(key);
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
int[] intArray = list.get(i);
|
||||
UUID uuid = uuidList.get(i);
|
||||
@@ -169,37 +171,37 @@ public class NBTCommand extends ParentCommand {
|
||||
break;
|
||||
}
|
||||
case NBTTagInt: {
|
||||
NBTList<Integer> list = compound.getIntegerList(key);
|
||||
ReadWriteNBTList<Integer> list = compound.getIntegerList(key);
|
||||
for (Integer value : list) {
|
||||
sender.sendMessage(String.format("%s- %d", listSpace, value));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case NBTTagLong: {
|
||||
NBTList<Long> list = compound.getLongList(key);
|
||||
ReadWriteNBTList<Long> list = compound.getLongList(key);
|
||||
for (Long value : list) {
|
||||
sender.sendMessage(String.format("%s- %d", listSpace, value));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case NBTTagFloat: {
|
||||
NBTList<Float> list = compound.getFloatList(key);
|
||||
ReadWriteNBTList<Float> list = compound.getFloatList(key);
|
||||
for (Float value : list) {
|
||||
sender.sendMessage(String.format("%s- %f", listSpace, value));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case NBTTagDouble: {
|
||||
NBTList<Double> list = compound.getDoubleList(key);
|
||||
ReadWriteNBTList<Double> list = compound.getDoubleList(key);
|
||||
for (Double value : list) {
|
||||
sender.sendMessage(String.format("%s- %f", listSpace, value));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case NBTTagCompound: {
|
||||
NBTCompoundList compoundList = compound.getCompoundList(key);
|
||||
ReadWriteNBTCompoundList compoundList = compound.getCompoundList(key);
|
||||
for (int i = 0; i < compoundList.size(); i++) {
|
||||
NBTListCompound listCompound = compoundList.get(i);
|
||||
ReadWriteNBT listCompound = compoundList.get(i);
|
||||
sendNBTCompound(listCompound, sender, level + 1);
|
||||
}
|
||||
}
|
||||
|
@@ -3,10 +3,10 @@ package cn.hamster3.mc.plugin.core.bukkit.util;
|
||||
import cn.hamster3.mc.plugin.core.bukkit.listener.CallbackListener;
|
||||
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
|
||||
import com.google.gson.JsonObject;
|
||||
import de.tr7zw.changeme.nbtapi.NBTContainer;
|
||||
import de.tr7zw.changeme.nbtapi.NBTItem;
|
||||
import de.tr7zw.changeme.nbtapi.NBT;
|
||||
import me.clip.placeholderapi.PlaceholderAPI;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import net.kyori.adventure.title.Title;
|
||||
import org.bukkit.Bukkit;
|
||||
@@ -241,23 +241,45 @@ public final class CoreBukkitUtils {
|
||||
@NotNull
|
||||
public static DisplayMessage loadDisplayMessage(@NotNull ConfigurationSection config) {
|
||||
DisplayMessage displayMessage = new DisplayMessage();
|
||||
String message = config.getString("message");
|
||||
if (message != null) {
|
||||
displayMessage.setMessage(LegacyComponentSerializer.legacySection().deserialize(message));
|
||||
String miniMessage = config.getString("mini-message");
|
||||
if (miniMessage != null) {
|
||||
displayMessage.setMessage(MiniMessage.miniMessage().deserialize(miniMessage));
|
||||
} else {
|
||||
String message = config.getString("message");
|
||||
if (message != null) {
|
||||
displayMessage.setMessage(LegacyComponentSerializer.legacySection().deserialize(message));
|
||||
}
|
||||
}
|
||||
String actionbar = config.getString("actionbar");
|
||||
if (actionbar != null) {
|
||||
displayMessage.setActionbar(LegacyComponentSerializer.legacySection().deserialize(actionbar));
|
||||
String miniActionbar = config.getString("mini-actionbar");
|
||||
if (miniActionbar != null) {
|
||||
displayMessage.setActionbar(MiniMessage.miniMessage().deserialize(miniActionbar));
|
||||
} else {
|
||||
String actionbar = config.getString("actionbar");
|
||||
if (actionbar != null) {
|
||||
displayMessage.setActionbar(LegacyComponentSerializer.legacySection().deserialize(actionbar));
|
||||
}
|
||||
}
|
||||
String title = config.getString("title");
|
||||
String subtitle = config.getString("subtitle");
|
||||
if (title != null || subtitle != null) {
|
||||
String miniTitle = config.getString("mini-title");
|
||||
String miniSubtitle = config.getString("mini-subtitle");
|
||||
if (miniTitle != null || miniSubtitle != null) {
|
||||
displayMessage.setTitle(
|
||||
title, subtitle,
|
||||
MiniMessage.miniMessage().deserialize(miniTitle == null ? "" : miniTitle),
|
||||
MiniMessage.miniMessage().deserialize(miniSubtitle == null ? "" : miniSubtitle),
|
||||
config.getInt("fade-in", 10),
|
||||
config.getInt("stay", 70),
|
||||
config.getInt("fade-out", 20)
|
||||
);
|
||||
} else {
|
||||
String title = config.getString("title");
|
||||
String subtitle = config.getString("subtitle");
|
||||
if (title != null || subtitle != null) {
|
||||
displayMessage.setTitle(
|
||||
title, subtitle,
|
||||
config.getInt("fade-in", 10),
|
||||
config.getInt("stay", 70),
|
||||
config.getInt("fade-out", 20)
|
||||
);
|
||||
}
|
||||
}
|
||||
String sound = config.getString("sound");
|
||||
if (sound != null) {
|
||||
@@ -330,7 +352,7 @@ public final class CoreBukkitUtils {
|
||||
*/
|
||||
@NotNull
|
||||
public static String serializeItemStack(@NotNull ItemStack stack) {
|
||||
return NBTItem.convertItemtoNBT(stack).toString();
|
||||
return NBT.itemStackToNBT(stack).toString();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -341,7 +363,7 @@ public final class CoreBukkitUtils {
|
||||
*/
|
||||
@Nullable
|
||||
public static ItemStack deserializeItemStack(@NotNull String string) {
|
||||
return NBTItem.convertNBTtoItem(new NBTContainer(string));
|
||||
return NBT.itemStackFromNBT(NBT.parseNBT(string));
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user