From aad9b036bc271e854687e4ca1fa6eaa304a24aae Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Sun, 3 Jan 2021 23:18:34 -0800
Subject: [PATCH] consider enchants for destroy speed

---
 Spigot-API-Patches/Add-Destroy-Speed-API.patch   | 16 +++++++++++++++-
 .../Add-Destroy-Speed-API.patch                  | 12 ++++++++++--
 2 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/Spigot-API-Patches/Add-Destroy-Speed-API.patch b/Spigot-API-Patches/Add-Destroy-Speed-API.patch
index ae3ad8d224..228bf01d9f 100644
--- a/Spigot-API-Patches/Add-Destroy-Speed-API.patch
+++ b/Spigot-API-Patches/Add-Destroy-Speed-API.patch
@@ -3,6 +3,7 @@ From: Ineusia <ineusia@yahoo.com>
 Date: Mon, 26 Oct 2020 11:37:48 -0500
 Subject: [PATCH] Add Destroy Speed API
 
+Co-authored-by: Jake Potrebic <jake.m.potrebic@gmail.com>
 
 diff --git a/src/main/java/org/bukkit/block/Block.java b/src/main/java/org/bukkit/block/Block.java
 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
@@ -22,6 +23,19 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
 +     * @return the speed that this Block will be mined by the given {@link ItemStack}
 +     */
 +    @NotNull
-+    float getDestroySpeed(@NotNull ItemStack itemStack);
++    public default float getDestroySpeed(@NotNull ItemStack itemStack) {
++        return getDestroySpeed(itemStack, false);
++    }
++
++    /**
++     * Gets the speed at which this blook will be destroyed by a given {@link org.bukkit.inventory.ItemStack}
++     * <p>
++     * Default value is 1.0
++     * @param itemStack {@link org.bukkit.inventory.ItemStack} used to mine this Block
++     * @param considerEnchants true to look at enchants on the itemstack
++     * @return the speed that this Block will be mined by the given {@link org.bukkit.inventory.ItemStack}
++     */
++    @NotNull
++    float getDestroySpeed(@NotNull ItemStack itemStack, boolean considerEnchants);
      // Paper end
  }
diff --git a/Spigot-Server-Patches/Add-Destroy-Speed-API.patch b/Spigot-Server-Patches/Add-Destroy-Speed-API.patch
index 5e4f91f2f1..502ba1f97b 100644
--- a/Spigot-Server-Patches/Add-Destroy-Speed-API.patch
+++ b/Spigot-Server-Patches/Add-Destroy-Speed-API.patch
@@ -3,6 +3,7 @@ From: Ineusia <ineusia@yahoo.com>
 Date: Mon, 26 Oct 2020 11:48:06 -0500
 Subject: [PATCH] Add Destroy Speed API
 
+Co-authored-by: Jake Potrebic <jake.m.potrebic@gmail.com>
 
 diff --git a/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java b/src/main/java/org/bukkit/craftbukkit/block/CraftBlock.java
 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644
@@ -14,14 +15,21 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000
      }
 +
 +    @Override
-+    public float getDestroySpeed(ItemStack itemStack) {
++    public float getDestroySpeed(ItemStack itemStack, boolean considerEnchants) {
 +        net.minecraft.server.ItemStack nmsItemStack;
 +        if (itemStack instanceof CraftItemStack) {
 +            nmsItemStack = ((CraftItemStack) itemStack).getHandle();
 +        } else {
 +            nmsItemStack = CraftItemStack.asNMSCopy(itemStack);
 +        }
-+        return nmsItemStack.getItem().getDestroySpeed(nmsItemStack, this.getNMSBlock().getBlockData());
++        float speed = nmsItemStack.getItem().getDestroySpeed(nmsItemStack, this.getNMSBlock().getBlockData());
++        if (speed > 1.0F && considerEnchants) {
++            int enchantLevel = net.minecraft.server.EnchantmentManager.getEnchantmentLevel(net.minecraft.server.Enchantments.DIG_SPEED, nmsItemStack);
++            if (enchantLevel > 0) {
++                speed += enchantLevel * enchantLevel + 1;
++            }
++        }
++        return speed;
 +    }
      // Paper end
  }