Fixed CraftItemStack/CraftItemMeta enchantment level inconsistency (#8792)

Co-authored-by: Nassim Jahnke <nassim@njahnke.dev>
This commit is contained in:
Will FP 2023-08-21 08:31:52 +01:00
parent d8b5390f81
commit bd8dd4f684

View file

@ -108,6 +108,25 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
static boolean makeTag(net.minecraft.world.item.ItemStack item) {
@@ -0,0 +0,0 @@ public final class CraftItemStack extends ItemStack {
if (this.handle == null) {
return 0;
}
- return EnchantmentHelper.getItemEnchantmentLevel(CraftEnchantment.getRaw(ench), handle);
+ // Paper start - replace to allow custom enchantments
+ final ListTag enchantments = this.handle.getEnchantmentTags();
+ for (int i = 0; i < enchantments.size(); i++) {
+ final CompoundTag tag = enchantments.getCompound(i);
+ final String id = tag.getString(CraftMetaItem.ENCHANTMENTS_ID.NBT);
+ final Enchantment enchant = Enchantment.getByKey(CraftNamespacedKey.fromStringOrNull(id));
+ if (ench.equals(enchant)) {
+ return EnchantmentHelper.getEnchantmentLevel(tag);
+ }
+ }
+ return 0;
+ // Paper end - replace to allow custom enchantments
}
@Override
public int removeEnchantment(Enchantment ench) {
Preconditions.checkArgument(ench != null, "Enchantment cannot be null");