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.
This commit is contained in:
rasmus123d 2024-08-14 16:42:38 +02:00
parent 8143bd6124
commit 728755d13a

View File

@ -130,13 +130,15 @@ public final class ItemUtils {
*/ */
public static boolean hasItemIncludingOffHand(Player player, Material material) { public static boolean hasItemIncludingOffHand(Player player, Material material) {
// Checks main inventory / item bar // Checks main inventory / item bar
boolean containsInMain = player.getInventory().contains(material); for (ItemStack itemStack : player.getInventory().getContents()) {
if (itemStack != null && itemStack.getType() == material) {
if (containsInMain) { return true;
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 * @param amount Amount of the material to remove
*/ */
public static void removeItemIncludingOffHand(@NotNull Player player, @NotNull Material material, int amount) { public static void removeItemIncludingOffHand(@NotNull Player player, @NotNull Material material, int amount) {
// Checks main inventory / item bar // Remove from main inventory
if (player.getInventory().contains(material)) { for (ItemStack itemStack : player.getInventory().getContents()) {
player.getInventory().removeItem(new ItemStack(material, amount)); if (itemStack != null && itemStack.getType() == material) {
return; 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 // Remove from off-hand
final ItemStack offHandItem = player.getInventory().getItemInOffHand(); ItemStack offHandItem = player.getInventory().getItemInOffHand();
if (offHandItem.getType() == material) { if (offHandItem != null && offHandItem.getType() == material) {
int newAmount = offHandItem.getAmount() - amount; int stackAmount = offHandItem.getAmount();
if (newAmount > 0) { if (stackAmount > amount) {
offHandItem.setAmount(newAmount); offHandItem.setAmount(stackAmount - amount);
} else { } else {
player.getInventory().setItemInOffHand(new ItemStack(Material.AIR)); player.getInventory().setItemInOffHand(new ItemStack(Material.AIR));
} }