perf: 优化 NBT API 调用

This commit is contained in:
2024-08-09 04:15:05 +08:00
parent 6beabf4ec4
commit 00d582a5ea
2 changed files with 51 additions and 27 deletions

View File

@@ -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.constant.CoreMessage;
import cn.hamster3.mc.plugin.core.bukkit.util.CoreBukkitUtils; import cn.hamster3.mc.plugin.core.bukkit.util.CoreBukkitUtils;
import de.tr7zw.changeme.nbtapi.*; 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.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@@ -62,13 +65,12 @@ public class NBTCommand extends ParentCommand {
sender.sendMessage("§c你的手持物品为空"); sender.sendMessage("§c你的手持物品为空");
return true; return true;
} }
NBTItem nbtItem = new NBTItem(stack); ReadWriteNBT readWriteNBT = NBT.itemStackToNBT(stack);
sendNBTCompound(nbtItem, sender, 0); sendNBTCompound(readWriteNBT, sender, 0);
return true; return true;
} }
@SuppressWarnings("ForLoopReplaceableByForEach") private void sendNBTCompound(ReadWriteNBT compound, CommandSender sender, int level) {
private void sendNBTCompound(NBTCompound compound, CommandSender sender, int level) {
StringBuilder prefixBuilder = new StringBuilder(); StringBuilder prefixBuilder = new StringBuilder();
for (int i = 0; i < level; i++) { for (int i = 0; i < level; i++) {
prefixBuilder.append(" "); prefixBuilder.append(" ");
@@ -137,7 +139,7 @@ public class NBTCommand extends ParentCommand {
String listSpace = space + " "; String listSpace = space + " ";
switch (listType) { switch (listType) {
case NBTTagString: { case NBTTagString: {
NBTList<String> list = compound.getStringList(key); ReadWriteNBTList<String> list = compound.getStringList(key);
for (String string : list) { for (String string : list) {
sender.sendMessage(String.format("%s- %s", listSpace, string)); sender.sendMessage(String.format("%s- %s", listSpace, string));
} }
@@ -145,7 +147,7 @@ public class NBTCommand extends ParentCommand {
} }
case NBTTagIntArray: { case NBTTagIntArray: {
boolean all4Length = true; boolean all4Length = true;
NBTList<int[]> list = compound.getIntArrayList(key); ReadWriteNBTList<int[]> list = compound.getIntArrayList(key);
for (int[] intArray : list) { for (int[] intArray : list) {
if (intArray.length != 4) { if (intArray.length != 4) {
all4Length = false; all4Length = false;
@@ -153,7 +155,7 @@ public class NBTCommand extends ParentCommand {
} }
} }
if (all4Length) { if (all4Length) {
NBTList<UUID> uuidList = compound.getUUIDList(key); ReadWriteNBTList<UUID> uuidList = compound.getUUIDList(key);
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {
int[] intArray = list.get(i); int[] intArray = list.get(i);
UUID uuid = uuidList.get(i); UUID uuid = uuidList.get(i);
@@ -169,37 +171,37 @@ public class NBTCommand extends ParentCommand {
break; break;
} }
case NBTTagInt: { case NBTTagInt: {
NBTList<Integer> list = compound.getIntegerList(key); ReadWriteNBTList<Integer> list = compound.getIntegerList(key);
for (Integer value : list) { for (Integer value : list) {
sender.sendMessage(String.format("%s- %d", listSpace, value)); sender.sendMessage(String.format("%s- %d", listSpace, value));
} }
break; break;
} }
case NBTTagLong: { case NBTTagLong: {
NBTList<Long> list = compound.getLongList(key); ReadWriteNBTList<Long> list = compound.getLongList(key);
for (Long value : list) { for (Long value : list) {
sender.sendMessage(String.format("%s- %d", listSpace, value)); sender.sendMessage(String.format("%s- %d", listSpace, value));
} }
break; break;
} }
case NBTTagFloat: { case NBTTagFloat: {
NBTList<Float> list = compound.getFloatList(key); ReadWriteNBTList<Float> list = compound.getFloatList(key);
for (Float value : list) { for (Float value : list) {
sender.sendMessage(String.format("%s- %f", listSpace, value)); sender.sendMessage(String.format("%s- %f", listSpace, value));
} }
break; break;
} }
case NBTTagDouble: { case NBTTagDouble: {
NBTList<Double> list = compound.getDoubleList(key); ReadWriteNBTList<Double> list = compound.getDoubleList(key);
for (Double value : list) { for (Double value : list) {
sender.sendMessage(String.format("%s- %f", listSpace, value)); sender.sendMessage(String.format("%s- %f", listSpace, value));
} }
break; break;
} }
case NBTTagCompound: { case NBTTagCompound: {
NBTCompoundList compoundList = compound.getCompoundList(key); ReadWriteNBTCompoundList compoundList = compound.getCompoundList(key);
for (int i = 0; i < compoundList.size(); i++) { for (int i = 0; i < compoundList.size(); i++) {
NBTListCompound listCompound = compoundList.get(i); ReadWriteNBT listCompound = compoundList.get(i);
sendNBTCompound(listCompound, sender, level + 1); sendNBTCompound(listCompound, sender, level + 1);
} }
} }

View File

@@ -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.bukkit.listener.CallbackListener;
import cn.hamster3.mc.plugin.core.common.data.DisplayMessage; import cn.hamster3.mc.plugin.core.common.data.DisplayMessage;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import de.tr7zw.changeme.nbtapi.NBTContainer; import de.tr7zw.changeme.nbtapi.NBT;
import de.tr7zw.changeme.nbtapi.NBTItem;
import me.clip.placeholderapi.PlaceholderAPI; import me.clip.placeholderapi.PlaceholderAPI;
import net.kyori.adventure.text.Component; 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.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.title.Title; import net.kyori.adventure.title.Title;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@@ -241,14 +241,35 @@ public final class CoreBukkitUtils {
@NotNull @NotNull
public static DisplayMessage loadDisplayMessage(@NotNull ConfigurationSection config) { public static DisplayMessage loadDisplayMessage(@NotNull ConfigurationSection config) {
DisplayMessage displayMessage = new DisplayMessage(); DisplayMessage displayMessage = new DisplayMessage();
String miniMessage = config.getString("mini-message");
if (miniMessage != null) {
displayMessage.setMessage(MiniMessage.miniMessage().deserialize(miniMessage));
} else {
String message = config.getString("message"); String message = config.getString("message");
if (message != null) { if (message != null) {
displayMessage.setMessage(LegacyComponentSerializer.legacySection().deserialize(message)); displayMessage.setMessage(LegacyComponentSerializer.legacySection().deserialize(message));
} }
}
String miniActionbar = config.getString("mini-actionbar");
if (miniActionbar != null) {
displayMessage.setActionbar(MiniMessage.miniMessage().deserialize(miniActionbar));
} else {
String actionbar = config.getString("actionbar"); String actionbar = config.getString("actionbar");
if (actionbar != null) { if (actionbar != null) {
displayMessage.setActionbar(LegacyComponentSerializer.legacySection().deserialize(actionbar)); displayMessage.setActionbar(LegacyComponentSerializer.legacySection().deserialize(actionbar));
} }
}
String miniTitle = config.getString("mini-title");
String miniSubtitle = config.getString("mini-subtitle");
if (miniTitle != null || miniSubtitle != null) {
displayMessage.setTitle(
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 title = config.getString("title");
String subtitle = config.getString("subtitle"); String subtitle = config.getString("subtitle");
if (title != null || subtitle != null) { if (title != null || subtitle != null) {
@@ -259,6 +280,7 @@ public final class CoreBukkitUtils {
config.getInt("fade-out", 20) config.getInt("fade-out", 20)
); );
} }
}
String sound = config.getString("sound"); String sound = config.getString("sound");
if (sound != null) { if (sound != null) {
displayMessage.setSound(sound, displayMessage.setSound(sound,
@@ -330,7 +352,7 @@ public final class CoreBukkitUtils {
*/ */
@NotNull @NotNull
public static String serializeItemStack(@NotNull ItemStack stack) { 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 @Nullable
public static ItemStack deserializeItemStack(@NotNull String string) { public static ItemStack deserializeItemStack(@NotNull String string) {
return NBTItem.convertNBTtoItem(new NBTContainer(string)); return NBT.itemStackFromNBT(NBT.parseNBT(string));
} }
/** /**