From 6d65c09e6fe374aa58d1026ed9f5159fcadf80c8 Mon Sep 17 00:00:00 2001 From: acrylic-style Date: Sat, 5 Oct 2024 15:38:36 +0900 Subject: [PATCH] exclude Damage tag from backup (restore) --- build.gradle.kts | 2 +- .../mori01231/lifecore/util/ItemUtil.java | 32 ++++++++++++++++--- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index cb38bec..29ac5b1 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -9,7 +9,7 @@ plugins { } group = "net.azisaba" -version = "1.15.2+6.17.2" +version = "1.15.2+6.17.3" java { toolchain.languageVersion.set(JavaLanguageVersion.of(8)) diff --git a/src/main/java/com/github/mori01231/lifecore/util/ItemUtil.java b/src/main/java/com/github/mori01231/lifecore/util/ItemUtil.java index 8e6ecc4..2836fb1 100644 --- a/src/main/java/com/github/mori01231/lifecore/util/ItemUtil.java +++ b/src/main/java/com/github/mori01231/lifecore/util/ItemUtil.java @@ -15,10 +15,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; -import java.util.UUID; +import java.util.*; import java.util.function.Consumer; public class ItemUtil { @@ -44,6 +41,14 @@ public static boolean isProbablyAdminSword(@Nullable ItemStack stack) { return type; } + @Contract("null, _ -> null") + public static @Nullable NBTBase getTag(@Nullable ItemStack stack, @NotNull String key) { + if (stack == null || stack.getType().isAir()) return null; + NBTTagCompound tag = CraftItemStack.asNMSCopy(stack).getTag(); + if (tag == null) return null; + return tag.get(key); + } + @Contract("null, _ -> null") public static @Nullable String getStringTag(@Nullable ItemStack stack, @NotNull String key) { if (stack == null || stack.getType().isAir()) return null; @@ -52,6 +57,13 @@ public static boolean isProbablyAdminSword(@Nullable ItemStack stack) { return tag.getString(key); } + public static int getIntTag(@Nullable ItemStack stack, @NotNull String key) { + if (stack == null || stack.getType().isAir()) return 0; + NBTTagCompound tag = CraftItemStack.asNMSCopy(stack).getTag(); + if (tag == null) return 0; + return tag.getInt(key); + } + @Contract("null, _ -> null") public static @Nullable byte[] getByteArrayTag(@Nullable ItemStack stack, @NotNull String key) { if (stack == null || stack.getType().isAir()) return null; @@ -155,6 +167,8 @@ public static ItemStack backupTag(@Nullable ItemStack stack) { return setTag(stack, "backup", tag); } + private static final Set RESTORE_BYPASS_SET = new HashSet<>(Collections.singletonList("Damage")); + @Contract("null -> null") public static ItemStack restoreTag(@Nullable ItemStack stack) { if (stack == null || stack.getType().isAir()) return null; @@ -162,7 +176,15 @@ public static ItemStack restoreTag(@Nullable ItemStack stack) { if (tag == null) return stack; NBTTagCompound backup = tag.getCompound("backup"); if (backup == null || backup.isEmpty()) return stack; - return setTag(stack, null, backup); + ItemStack newStack = setTag(stack, null, backup); + for (String bypassTag : RESTORE_BYPASS_SET) { + if (containsTag(stack, bypassTag)) { + NBTBase value = getTag(stack, bypassTag); + assert value != null; + newStack = setTag(newStack, bypassTag, value); + } + } + return newStack; } public static boolean containsTag(@Nullable ItemStack stack, @NotNull String key) {