From bd8dd4f6846ac60ff89eaed2f06bd14dc70dea5d Mon Sep 17 00:00:00 2001
From: Will FP <38837418+WillFP@users.noreply.github.com>
Date: Mon, 21 Aug 2023 08:31:52 +0100
Subject: [PATCH] Fixed CraftItemStack/CraftItemMeta enchantment level
 inconsistency (#8792)

Co-authored-by: Nassim Jahnke <nassim@njahnke.dev>
---
 .../Handle-Item-Meta-Inconsistencies.patch    | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/patches/server/Handle-Item-Meta-Inconsistencies.patch b/patches/server/Handle-Item-Meta-Inconsistencies.patch
index 65c6d4fd97..45bd2fd2e6 100644
--- a/patches/server/Handle-Item-Meta-Inconsistencies.patch
+++ b/patches/server/Handle-Item-Meta-Inconsistencies.patch
@@ -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");