From c9f4876f29f435317075d5d74032a59ccc1c9700 Mon Sep 17 00:00:00 2001 From: Brianna Date: Thu, 29 Oct 2020 13:31:12 -0500 Subject: [PATCH] Added the ability to check if a tool has the required durability. --- .../java/com/songoda/core/utils/ItemUtils.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Core/src/main/java/com/songoda/core/utils/ItemUtils.java b/Core/src/main/java/com/songoda/core/utils/ItemUtils.java index aa3aff9b..4c1a3cb8 100644 --- a/Core/src/main/java/com/songoda/core/utils/ItemUtils.java +++ b/Core/src/main/java/com/songoda/core/utils/ItemUtils.java @@ -134,6 +134,21 @@ public class ItemUtils { return item; } + public static boolean hasEnoughDurability(ItemStack tool, int requiredAmount) { + if (tool.getType().getMaxDurability() <= 1) + return true; + if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13)) { + if (!tool.hasItemMeta() || !(tool.getItemMeta() instanceof Damageable)) + return true; + + Damageable damageable = (Damageable) tool.getItemMeta(); + int durabilityRemaining = tool.getType().getMaxDurability() - damageable.getDamage(); + return durabilityRemaining > requiredAmount; + } else { + return tool.getDurability() + requiredAmount <= tool.getType().getMaxDurability(); + } + } + static Class cb_ItemStack = NMSUtils.getCraftClass("inventory.CraftItemStack"); static Class mc_ItemStack = NMSUtils.getNMSClass("ItemStack"); static Class mc_NBTTagCompound = NMSUtils.getNMSClass("NBTTagCompound");