- Improved item handling.
This commit is contained in:
nulli0n committed 2025-04-10 15:19:08 +05:00
1 parent b7495ac492
commit df621123ce
5 files changed
+152 -47

No files matched your search

+1 -1
View File
@@ -6,7 +6,7 @@
<groupId>su.nightexpress.nightcore</groupId> <groupId>su.nightexpress.nightcore</groupId>
<artifactId>nightcore</artifactId> <artifactId>nightcore</artifactId>
<version>2.7.5.1</version> <version>2.7.5.2</version>
<properties> <properties>
<maven.compiler.source>21</maven.compiler.source> <maven.compiler.source>21</maven.compiler.source>
@@ -4,6 +4,7 @@ import su.nightexpress.nightcore.config.ConfigValue;
import su.nightexpress.nightcore.util.Placeholders; import su.nightexpress.nightcore.util.Placeholders;
import su.nightexpress.nightcore.util.StringUtil; import su.nightexpress.nightcore.util.StringUtil;
import su.nightexpress.nightcore.util.TimeUtil; import su.nightexpress.nightcore.util.TimeUtil;
import su.nightexpress.nightcore.util.Version;
import su.nightexpress.nightcore.util.number.NumberShortcut; import su.nightexpress.nightcore.util.number.NumberShortcut;
import su.nightexpress.nightcore.util.wrapper.UniFormatter; import su.nightexpress.nightcore.util.wrapper.UniFormatter;
@@ -15,11 +16,12 @@ public class CoreConfig {
public static final String DIR_UI = "/ui/"; public static final String DIR_UI = "/ui/";
@Deprecated public static final ConfigValue<Integer> DATA_FIXER_MISSING_VERSION = ConfigValue.create("DataFixer.MissingVersion",
public static final ConfigValue<Boolean> DATA_FIXER_ENABLED = ConfigValue.create("DataFixer.Enabled", Version.MC_1_21_4.getDataVersion()
false, );
"When enabled, uses Mojang's DataFixer util to update ItemStacks from <= 1.20.4 NBT format to 1.20.5+ NBT format.",
"IMPORTANT NOTE: You need to use this setting only once! Enable it, reboot the server and re-save all configurations that stores compressed item data: shops, crates, etc." public static final ConfigValue<Integer> DATA_FIXER_UNKNOWN_VERSION = ConfigValue.create("DataFixer.UnknownVersion",
10000
); );
public static final ConfigValue<Long> MENU_CLICK_COOLDOWN = ConfigValue.create("Menu.Click_Cooldown", public static final ConfigValue<Long> MENU_CLICK_COOLDOWN = ConfigValue.create("Menu.Click_Cooldown",
@@ -11,6 +11,7 @@ import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import su.nightexpress.nightcore.NightCore; import su.nightexpress.nightcore.NightCore;
import su.nightexpress.nightcore.core.CoreConfig;
import java.io.*; import java.io.*;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
@@ -40,8 +41,8 @@ public class ItemNbt {
private static Method mNbtIoRead; private static Method mNbtIoRead;
private static Method mTagParserParseTag; private static Method mTagParserParseTag;
private static int DATA_FIXER_SOURCE_VERSION; //private static int DATA_FIXER_SOURCE_VERSION;
private static int DATA_FXIER_TARGET_VERSION; //private static int DATA_FIXER_TARGET_VERSION;
private static DataFixer dataFixer; private static DataFixer dataFixer;
private static Object nbtOps; private static Object nbtOps;
private static Object itemStackReference; private static Object itemStackReference;
@@ -91,8 +92,8 @@ public class ItemNbt {
} }
// Load DataFixer components. // Load DataFixer components.
DATA_FIXER_SOURCE_VERSION = 3700; // 1.20.4 //DATA_FIXER_SOURCE_VERSION = 4189;//3700; // 1.20.4
DATA_FXIER_TARGET_VERSION = 4325; // 1.21.5 //DATA_FIXER_TARGET_VERSION = 4325; // 1.21.5 or 1.21.4
Method mGetDataFixer = Reflex.getMethod(CLS_DATA_FIXERS, "a"); Method mGetDataFixer = Reflex.getMethod(CLS_DATA_FIXERS, "a");
if (mGetDataFixer == null) { if (mGetDataFixer == null) {
@@ -165,16 +166,36 @@ public class ItemNbt {
meta.setDisplayName("Test Item"); meta.setDisplayName("Test Item");
meta.setLore(Lists.newList("Test Lore 1", "Test Lore 2", "Test Lore 3")); meta.setLore(Lists.newList("Test Lore 1", "Test Lore 2", "Test Lore 3"));
meta.addEnchant(Enchantment.FIRE_ASPECT, 10, true); meta.addEnchant(Enchantment.FIRE_ASPECT, 10, true);
meta.setCustomModelData(100500);
}); });
String nbt = ItemNbt.compress(testItem); ItemTag tag = getTag(testItem);
if (nbt == null) return false; if (tag == null) return false;
ItemStack decompressed = ItemNbt.decompress(nbt); ItemStack parsed = fromTag(tag);
return decompressed != null && decompressed.isSimilar(testItem); return parsed != null && parsed.isSimilar(testItem);
// String nbt = ItemNbt.compress(testItem);
// if (nbt == null) return false;
//
// ItemStack decompressed = ItemNbt.decompress(nbt);
// return decompressed != null && decompressed.isSimilar(testItem);
}
@NotNull
@Deprecated
public static List<String> compress(@NotNull ItemStack[] items) {
return compress(Arrays.asList(items));
}
@NotNull
@Deprecated
public static List<String> compress(@NotNull List<ItemStack> items) {
return new ArrayList<>(items.stream().map(ItemNbt::compress).filter(Objects::nonNull).toList());
} }
@Nullable @Nullable
@Deprecated
public static String compress(@NotNull ItemStack item) { public static String compress(@NotNull ItemStack item) {
Object compoundTag = toCompoundTag(item); Object compoundTag = toCompoundTag(item);
if (compoundTag == null) return null; if (compoundTag == null) return null;
@@ -186,6 +207,7 @@ public class ItemNbt {
} }
@Nullable @Nullable
@Deprecated
public static ItemStack decompress(@NotNull String compressed) { public static ItemStack decompress(@NotNull String compressed) {
if (compressed.isBlank()) return null; if (compressed.isBlank()) return null;
@@ -193,10 +215,17 @@ public class ItemNbt {
DataInputStream dataInput = new DataInputStream(inputStream); DataInputStream dataInput = new DataInputStream(inputStream);
Object compoundTag = Reflex.invokeMethod(mNbtIoRead, null, dataInput); Object compoundTag = Reflex.invokeMethod(mNbtIoRead, null, dataInput);
return compoundTag == null ? null : fromCompoundTag(compoundTag); return compoundTag == null ? null : fromCompoundTag(compoundTag, CoreConfig.DATA_FIXER_MISSING_VERSION.get());
}
@Deprecated
public static ItemStack[] decompress(@NotNull List<String> list) {
List<ItemStack> items = list.stream().map(ItemNbt::decompress).filter(Objects::nonNull).toList();
return items.toArray(new ItemStack[list.size()]);
} }
@Nullable @Nullable
@Deprecated
public static String getTagString(@NotNull ItemStack item) { public static String getTagString(@NotNull ItemStack item) {
Object compoundTag = toCompoundTag(item); Object compoundTag = toCompoundTag(item);
if (compoundTag == null) return null; if (compoundTag == null) return null;
@@ -205,27 +234,38 @@ public class ItemNbt {
} }
@Nullable @Nullable
@Deprecated
public static ItemStack fromTagString(@NotNull String tagString) { public static ItemStack fromTagString(@NotNull String tagString) {
if (tagString.isBlank() || tagString.equalsIgnoreCase("{}")) return null; // if (tagString.isBlank() || tagString.equalsIgnoreCase("{}")) return null;
//
// Object compoundTag = Reflex.invokeMethod(mTagParserParseTag, null, tagString);
// if (compoundTag != null) System.out.println("fromCompoundTag(compoundTag) = " + fromCompoundTag(compoundTag));
// return compoundTag == null ? null : fromCompoundTag(compoundTag);
Object compoundTag = Reflex.invokeMethod(mTagParserParseTag, null, tagString); return fromTag(new ItemTag(tagString, CoreConfig.DATA_FIXER_MISSING_VERSION.get()));
return compoundTag == null ? null : fromCompoundTag(compoundTag);
} }
@NotNull @Nullable
public static List<String> compress(@NotNull ItemStack[] items) { public static ItemStack fromTag(@NotNull ItemTag itemTag) {
return compress(Arrays.asList(items)); if (!loaded) return null;
if (itemTag.isEmpty()) return null;
String tagString = itemTag.getTag();
int sourceVersion = itemTag.getDataVersion();
Object tag = Reflex.invokeMethod(mTagParserParseTag, null, tagString);
return tag == null ? null : fromCompoundTag(tag, sourceVersion);
} }
@NotNull @Nullable
public static List<String> compress(@NotNull List<ItemStack> items) { public static ItemTag getTag(@NotNull ItemStack item) {
return new ArrayList<>(items.stream().map(ItemNbt::compress).filter(Objects::nonNull).toList()); Object compoundTag = toCompoundTag(item);
if (compoundTag == null) return null;
return new ItemTag(compoundTag.toString(), Version.getCurrent().getDataVersion());
} }
public static ItemStack[] decompress(@NotNull List<String> list) {
List<ItemStack> items = list.stream().map(ItemNbt::decompress).filter(Objects::nonNull).toList();
return items.toArray(new ItemStack[list.size()]);
}
@NotNull @NotNull
public static ItemStack getHoverEventItem(@NotNull String value) { public static ItemStack getHoverEventItem(@NotNull String value) {
@@ -263,11 +303,11 @@ public class ItemNbt {
} }
@Nullable @Nullable
private static ItemStack fromCompoundTag(@NotNull Object tag) { private static ItemStack fromCompoundTag(@NotNull Object tag, int sourceVersion) {
if (!loaded) return null; if (!loaded) return null;
Object itemStack; Object itemStack;
Object compoundTag = ItemNbt.applyDataFixer(tag); Object compoundTag = ItemNbt.applyDataFixer(tag, sourceVersion);
if (useRegistry) { if (useRegistry) {
Optional<?> optional = (Optional<?>) Reflex.invokeMethod(mItemStackParse, null, registryAccess, compoundTag); Optional<?> optional = (Optional<?>) Reflex.invokeMethod(mItemStackParse, null, registryAccess, compoundTag);
@@ -280,9 +320,19 @@ public class ItemNbt {
return itemStack == null ? null : (ItemStack) Reflex.invokeMethod(mCraftItemStackAsBukkitCopy, null, itemStack); return itemStack == null ? null : (ItemStack) Reflex.invokeMethod(mCraftItemStackAsBukkitCopy, null, itemStack);
} }
// @SuppressWarnings({"rawtypes", "unchecked"})
// private static Object applyDataFixer(@NotNull Object compoundTag) {
// Dynamic<?> dynamic = new Dynamic<>((DynamicOps) nbtOps, compoundTag);
// return dataFixer.update((DSL.TypeReference) itemStackReference, dynamic, DATA_FIXER_SOURCE_VERSION, DATA_FIXER_TARGET_VERSION).getValue();
// }
@SuppressWarnings({"rawtypes", "unchecked"}) @SuppressWarnings({"rawtypes", "unchecked"})
private static Object applyDataFixer(@NotNull Object compoundTag) { private static Object applyDataFixer(@NotNull Object compoundTag, int sourceVersion) {
int targetVersion = Version.getCurrent() == Version.UNKNOWN ? CoreConfig.DATA_FIXER_UNKNOWN_VERSION.get() : Version.getCurrent().getDataVersion();
if (targetVersion <= 0) return compoundTag;
if (sourceVersion > targetVersion || sourceVersion <= 0) return compoundTag;
Dynamic<?> dynamic = new Dynamic<>((DynamicOps) nbtOps, compoundTag); Dynamic<?> dynamic = new Dynamic<>((DynamicOps) nbtOps, compoundTag);
return dataFixer.update((DSL.TypeReference) itemStackReference, dynamic, DATA_FIXER_SOURCE_VERSION, DATA_FXIER_TARGET_VERSION).getValue(); return dataFixer.update((DSL.TypeReference) itemStackReference, dynamic, sourceVersion, targetVersion).getValue();
} }
} }
@@ -0,0 +1,47 @@
package su.nightexpress.nightcore.util;
import org.jetbrains.annotations.NotNull;
import su.nightexpress.nightcore.config.ConfigValue;
import su.nightexpress.nightcore.config.FileConfig;
import su.nightexpress.nightcore.config.Writeable;
import su.nightexpress.nightcore.core.CoreConfig;
public class ItemTag implements Writeable {
private static final String EMPTY = "{}";
private final String tag;
private final int dataVersion;
public ItemTag(@NotNull String tag, int dataVersion) {
this.tag = tag;
this.dataVersion = dataVersion;
}
@NotNull
public static ItemTag read(@NotNull FileConfig config, @NotNull String path) {
String value = ConfigValue.create(path + ".Value", EMPTY).read(config);
int dataVersion = ConfigValue.create(path + ".DataVersion", CoreConfig.DATA_FIXER_MISSING_VERSION.get()).read(config);
return new ItemTag(value, dataVersion);
}
@Override
public void write(@NotNull FileConfig config, @NotNull String path) {
config.set(path + ".Value", this.tag);
config.set(path + ".DataVersion", this.dataVersion);
}
public boolean isEmpty() {
return this.tag.isBlank() || this.tag.equalsIgnoreCase(EMPTY);
}
@NotNull
public String getTag() {
return this.tag;
}
public int getDataVersion() {
return this.dataVersion;
}
}
@@ -13,18 +13,18 @@ import java.util.stream.Stream;
public enum Version { public enum Version {
V1_19_R3("1.19.4", Status.OUTDATED), V1_19_R3("1.19.4", 3337, Status.OUTDATED),
V1_20_R1("1.20.1", Status.OUTDATED), V1_20_R1("1.20.1", 3465, Status.OUTDATED),
V1_20_R2("1.20.2", Status.OUTDATED), V1_20_R2("1.20.2", 3578, Status.OUTDATED),
V1_20_R3("1.20.4", Status.OUTDATED), V1_20_R3("1.20.4", 3700, Status.OUTDATED),
MC_1_20_6("1.20.6", Status.OUTDATED), MC_1_20_6("1.20.6", 3839, Status.OUTDATED),
MC_1_21_0("1.21", Status.OUTDATED), MC_1_21_0("1.21", 3953, Status.OUTDATED),
MC_1_21("1.21.1"), MC_1_21("1.21.1", 3955),
MC_1_21_2("1.21.2", Status.OUTDATED), MC_1_21_2("1.21.2", 4080, Status.OUTDATED),
MC_1_21_3("1.21.3"), MC_1_21_3("1.21.3", 4082),
MC_1_21_4("1.21.4"), MC_1_21_4("1.21.4", 4189),
MC_1_21_5("1.21.5"), MC_1_21_5("1.21.5", 4325),
UNKNOWN("Unknown"), UNKNOWN("Unknown", 0),
; ;
public static final String CRAFTBUKKIT_PACKAGE = Bukkit.getServer().getClass().getPackage().getName(); public static final String CRAFTBUKKIT_PACKAGE = Bukkit.getServer().getClass().getPackage().getName();
@@ -33,14 +33,16 @@ public enum Version {
private static Software software; private static Software software;
private final Status status; private final Status status;
private final int dataVersion;
private final String localized; private final String localized;
Version(@NotNull String localized) { Version(@NotNull String localized, int dataVersion) {
this(localized, Status.SUPPORTED); this(localized, dataVersion, Status.SUPPORTED);
} }
Version(@NotNull String localized, @NotNull Status status) { Version(@NotNull String localized, int dataVersion, @NotNull Status status) {
this.localized = localized; this.localized = localized;
this.dataVersion = dataVersion;
this.status = status; this.status = status;
} }
@@ -123,6 +125,10 @@ public enum Version {
return this.status; return this.status;
} }
public int getDataVersion() {
return this.dataVersion;
}
@NotNull @NotNull
public String getLocalized() { public String getLocalized() {
return this.localized; return this.localized;