From 905dff7eec29ca0b1df2ba203ed8cc8e8c3adfb4 Mon Sep 17 00:00:00 2001 From: fullwall Date: Tue, 17 Sep 2024 23:52:02 +0800 Subject: [PATCH] Nuke the custom tag as well --- .../net/citizensnpcs/trait/ShopTrait.java | 2 ++ .../main/java/net/citizensnpcs/util/NMS.java | 24 +++++++++++++++++++ .../nms/v1_20_R4/util/NMSImpl.java | 17 ++++--------- .../nms/v1_21_R1/util/NMSImpl.java | 17 ++++--------- 4 files changed, 36 insertions(+), 24 deletions(-) diff --git a/main/src/main/java/net/citizensnpcs/trait/ShopTrait.java b/main/src/main/java/net/citizensnpcs/trait/ShopTrait.java index dccf15c03..a92d28e1d 100644 --- a/main/src/main/java/net/citizensnpcs/trait/ShopTrait.java +++ b/main/src/main/java/net/citizensnpcs/trait/ShopTrait.java @@ -75,6 +75,7 @@ import net.citizensnpcs.trait.shop.PermissionAction; import net.citizensnpcs.trait.shop.PermissionAction.PermissionActionGUI; import net.citizensnpcs.trait.shop.StoredShops; import net.citizensnpcs.util.InventoryMultiplexer; +import net.citizensnpcs.util.NMS; import net.citizensnpcs.util.Util; /** @@ -980,6 +981,7 @@ public class ShopTrait extends Trait { for (NamespacedKey nk : Lists.newArrayList(im.getPersistentDataContainer().getKeys())) { im.getPersistentDataContainer().remove(nk); } + NMS.clearCustomNBT(im); stack.setItemMeta(im); } recipe.addIngredient(stack); diff --git a/main/src/main/java/net/citizensnpcs/util/NMS.java b/main/src/main/java/net/citizensnpcs/util/NMS.java index 50f65813d..98f13e4e3 100644 --- a/main/src/main/java/net/citizensnpcs/util/NMS.java +++ b/main/src/main/java/net/citizensnpcs/util/NMS.java @@ -36,6 +36,7 @@ import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryView; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.SkullMeta; import org.bukkit.scoreboard.Team; import org.bukkit.util.Vector; @@ -153,6 +154,27 @@ public class NMS { BRIDGE.cancelMoveDestination(entity); } + public static void clearCustomNBT(ItemMeta meta) { + if (CUSTOM_NBT_TAG_MISSING) + return; + if (CUSTOM_NBT_TAG == null) { + Class clazz = meta.getClass(); + while (!clazz.getName().contains("CraftMetaItem")) { + clazz = clazz.getSuperclass(); + } + CUSTOM_NBT_TAG = getSetter(clazz, "customTag"); + if (CUSTOM_NBT_TAG == null) { + CUSTOM_NBT_TAG_MISSING = true; + return; + } + } + try { + CUSTOM_NBT_TAG.invoke(meta, null); + } catch (Throwable e) { + e.printStackTrace(); + } + } + public static Iterable createBundlePacket(List packets) { return BRIDGE.createBundlePacket(packets); } @@ -991,6 +1013,8 @@ public class NMS { } private static NMSBridge BRIDGE; + private static MethodHandle CUSTOM_NBT_TAG; + private static boolean CUSTOM_NBT_TAG_MISSING; private static MethodHandle FIND_PROFILES_BY_NAMES; private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup(); private static Field MODIFIERS_FIELD; diff --git a/v1_20_R4/src/main/java/net/citizensnpcs/nms/v1_20_R4/util/NMSImpl.java b/v1_20_R4/src/main/java/net/citizensnpcs/nms/v1_20_R4/util/NMSImpl.java index f70c053ce..ba1e54b29 100644 --- a/v1_20_R4/src/main/java/net/citizensnpcs/nms/v1_20_R4/util/NMSImpl.java +++ b/v1_20_R4/src/main/java/net/citizensnpcs/nms/v1_20_R4/util/NMSImpl.java @@ -84,7 +84,6 @@ import net.citizensnpcs.api.trait.TraitInfo; import net.citizensnpcs.api.util.BoundingBox; import net.citizensnpcs.api.util.EntityDim; import net.citizensnpcs.api.util.Messaging; -import net.citizensnpcs.api.util.SpigotUtil; import net.citizensnpcs.api.util.SpigotUtil.InventoryViewAPI; import net.citizensnpcs.nms.v1_20_R4.entity.AllayController; import net.citizensnpcs.nms.v1_20_R4.entity.ArmadilloController; @@ -633,13 +632,11 @@ public class NMSImpl implements NMSBridge { @Override public Map getComponentMap(org.bukkit.inventory.ItemStack item) { if (META_COMPOUND_TAG == null) { - try { - META_COMPOUND_TAG = NMS.getGetter(Class.forName( - "org.bukkit.craftbukkit." + SpigotUtil.getMinecraftPackage() + ".inventory.CraftMetaItem"), - "customTag"); - } catch (ClassNotFoundException e) { - e.printStackTrace(); + Class base = item.getItemMeta().getClass(); + while (!base.getName().contains("CraftMetaItem")) { + base = base.getSuperclass(); } + META_COMPOUND_TAG = NMS.getGetter(base, "customTag"); } Map base = Maps.newHashMap(NMSBridge.super.getComponentMap(item)); CompoundTag ct; @@ -651,11 +648,7 @@ public class NMSImpl implements NMSBridge { } if (ct == null) return base; - Map custom = Maps.newHashMap(); - for (String key : ct.getAllKeys()) { - custom.put(key, deserialiseNBT(ct.get(key))); - } - base.put("custom_data", custom); + base.put("custom_data", deserialiseNBT(ct)); return base; } diff --git a/v1_21_R1/src/main/java/net/citizensnpcs/nms/v1_21_R1/util/NMSImpl.java b/v1_21_R1/src/main/java/net/citizensnpcs/nms/v1_21_R1/util/NMSImpl.java index 5fcbaff3c..e2b3140eb 100644 --- a/v1_21_R1/src/main/java/net/citizensnpcs/nms/v1_21_R1/util/NMSImpl.java +++ b/v1_21_R1/src/main/java/net/citizensnpcs/nms/v1_21_R1/util/NMSImpl.java @@ -84,7 +84,6 @@ import net.citizensnpcs.api.trait.TraitInfo; import net.citizensnpcs.api.util.BoundingBox; import net.citizensnpcs.api.util.EntityDim; import net.citizensnpcs.api.util.Messaging; -import net.citizensnpcs.api.util.SpigotUtil; import net.citizensnpcs.api.util.SpigotUtil.InventoryViewAPI; import net.citizensnpcs.nms.v1_21_R1.entity.AllayController; import net.citizensnpcs.nms.v1_21_R1.entity.ArmadilloController; @@ -613,13 +612,11 @@ public class NMSImpl implements NMSBridge { @Override public Map getComponentMap(org.bukkit.inventory.ItemStack item) { if (META_COMPOUND_TAG == null) { - try { - META_COMPOUND_TAG = NMS.getGetter(Class.forName( - "org.bukkit.craftbukkit." + SpigotUtil.getMinecraftPackage() + ".inventory.CraftMetaItem"), - "customTag"); - } catch (ClassNotFoundException e) { - e.printStackTrace(); + Class base = item.getItemMeta().getClass(); + while (!base.getName().contains("CraftMetaItem")) { + base = base.getSuperclass(); } + META_COMPOUND_TAG = NMS.getGetter(base, "customTag"); } Map base = Maps.newHashMap(NMSBridge.super.getComponentMap(item)); CompoundTag ct; @@ -631,11 +628,7 @@ public class NMSImpl implements NMSBridge { } if (ct == null) return base; - Map custom = Maps.newHashMap(); - for (String key : ct.getAllKeys()) { - custom.put(key, deserialiseNBT(ct.get(key))); - } - base.put("custom_data", custom); + base.put("custom_data", deserialiseNBT(ct)); return base; }