Nuke the custom tag as well

This commit is contained in:
fullwall 2024-09-17 23:52:02 +08:00
parent 00eef79dc9
commit 905dff7eec
4 changed files with 36 additions and 24 deletions

View File

@ -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);

View File

@ -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<Object> createBundlePacket(List<Object> 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;

View File

@ -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<String, Object> 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<String, Object> base = Maps.newHashMap(NMSBridge.super.getComponentMap(item));
CompoundTag ct;
@ -651,11 +648,7 @@ public class NMSImpl implements NMSBridge {
}
if (ct == null)
return base;
Map<String, Object> 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;
}

View File

@ -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<String, Object> 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<String, Object> base = Maps.newHashMap(NMSBridge.super.getComponentMap(item));
CompoundTag ct;
@ -631,11 +628,7 @@ public class NMSImpl implements NMSBridge {
}
if (ct == null)
return base;
Map<String, Object> 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;
}