From 728755d13aceef7908b7be22ddd4a30e17f1caad Mon Sep 17 00:00:00 2001 From: rasmus123d <59487370+RasmusKD@users.noreply.github.com> Date: Wed, 14 Aug 2024 16:42:38 +0200 Subject: [PATCH] Herbalism replant bug fix Fix Herbalism replanting to remove renamed crops from player inventory Previously, players could replant crops infinitely if the replant material was renamed, as it wasn't removed from the inventory. --- .../com/gmail/nossr50/util/ItemUtils.java | 43 ++++++++++++------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/util/ItemUtils.java b/src/main/java/com/gmail/nossr50/util/ItemUtils.java index 6dc9b2785..32f831aed 100644 --- a/src/main/java/com/gmail/nossr50/util/ItemUtils.java +++ b/src/main/java/com/gmail/nossr50/util/ItemUtils.java @@ -130,13 +130,15 @@ public final class ItemUtils { */ public static boolean hasItemIncludingOffHand(Player player, Material material) { // Checks main inventory / item bar - boolean containsInMain = player.getInventory().contains(material); - - if (containsInMain) { - return true; + for (ItemStack itemStack : player.getInventory().getContents()) { + if (itemStack != null && itemStack.getType() == material) { + return true; + } } - return player.getInventory().getItemInOffHand().getType() == material; + // Check off-hand + ItemStack offHandItem = player.getInventory().getItemInOffHand(); + return offHandItem != null && offHandItem.getType() == material; } /** @@ -147,18 +149,29 @@ public final class ItemUtils { * @param amount Amount of the material to remove */ public static void removeItemIncludingOffHand(@NotNull Player player, @NotNull Material material, int amount) { - // Checks main inventory / item bar - if (player.getInventory().contains(material)) { - player.getInventory().removeItem(new ItemStack(material, amount)); - return; + // Remove from main inventory + for (ItemStack itemStack : player.getInventory().getContents()) { + if (itemStack != null && itemStack.getType() == material) { + int stackAmount = itemStack.getAmount(); + if (stackAmount > amount) { + itemStack.setAmount(stackAmount - amount); + return; + } else { + player.getInventory().removeItem(itemStack); + amount -= stackAmount; + if (amount <= 0) { + return; + } + } + } } - // Check off-hand - final ItemStack offHandItem = player.getInventory().getItemInOffHand(); - if (offHandItem.getType() == material) { - int newAmount = offHandItem.getAmount() - amount; - if (newAmount > 0) { - offHandItem.setAmount(newAmount); + // Remove from off-hand + ItemStack offHandItem = player.getInventory().getItemInOffHand(); + if (offHandItem != null && offHandItem.getType() == material) { + int stackAmount = offHandItem.getAmount(); + if (stackAmount > amount) { + offHandItem.setAmount(stackAmount - amount); } else { player.getInventory().setItemInOffHand(new ItemStack(Material.AIR)); }