From b763fd386861bd97352126916642f3a3b5c73df4 Mon Sep 17 00:00:00 2001 From: Charles DeLancey <46200123+Evidentsinger14@users.noreply.github.com> Date: Sun, 11 Aug 2024 10:43:00 -0400 Subject: [PATCH] Fix removing negative items in /clearinventory (#5899) Co-authored-by: JRoy <10731363+JRoy@users.noreply.github.com> --- .../essentials/commands/Commandclearinventory.java | 9 +++++++-- .../com/earth2me/essentials/craftbukkit/Inventories.java | 4 ++++ Essentials/src/main/resources/messages.properties | 1 + 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandclearinventory.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandclearinventory.java index f0d1ff55f5c..3a284ff0c6a 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandclearinventory.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandclearinventory.java @@ -6,6 +6,7 @@ import com.earth2me.essentials.utils.NumberUtil; import com.earth2me.essentials.utils.StringUtil; import com.earth2me.essentials.utils.VersionUtil; +import net.ess3.api.TranslatableException; import org.bukkit.Material; import org.bukkit.Server; import org.bukkit.entity.Player; @@ -80,7 +81,7 @@ private void parseCommand(final Server server, final CommandSource sender, final } } - protected void clearHandler(final CommandSource sender, final Player player, final String[] args, final int offset, final boolean showExtended) { + protected void clearHandler(final CommandSource sender, final Player player, final String[] args, final int offset, final boolean showExtended) throws TranslatableException { ClearHandlerType type = ClearHandlerType.ALL_EXCEPT_ARMOR; final Set items = new HashSet<>(); int amount = -1; @@ -124,6 +125,11 @@ protected void clearHandler(final CommandSource sender, final Player player, fin stack.setDurability(item.getData()); } + // can't remove a negative amount of items. (it adds them) + if (amount < -1) { + throw new TranslatableException("cannotRemoveNegativeItems"); + } + // amount -1 means all items will be cleared if (amount == -1) { final int removedAmount = Inventories.removeItemSimilar(player, stack, true); @@ -131,7 +137,6 @@ protected void clearHandler(final CommandSource sender, final Player player, fin sender.sendTl("inventoryClearingStack", removedAmount, stack.getType().toString().toLowerCase(Locale.ENGLISH), player.getDisplayName()); } } else { - stack.setAmount(amount < 0 ? 1 : amount); if (Inventories.removeItemAmount(player, stack, amount)) { sender.sendTl("inventoryClearingStack", amount, stack.getType().toString().toLowerCase(Locale.ENGLISH), player.getDisplayName()); } else { diff --git a/Essentials/src/main/java/com/earth2me/essentials/craftbukkit/Inventories.java b/Essentials/src/main/java/com/earth2me/essentials/craftbukkit/Inventories.java index eb5c395bff8..fffccf9ca94 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/craftbukkit/Inventories.java +++ b/Essentials/src/main/java/com/earth2me/essentials/craftbukkit/Inventories.java @@ -243,6 +243,10 @@ public static int removeItems(final Player player, final Predicate re } public static boolean removeItemAmount(final Player player, final ItemStack toRemove, int amount) { + if (amount < 0) { + throw new IllegalArgumentException("Amount cannot be negative."); + } + final List clearSlots = new ArrayList<>(); final ItemStack[] items = player.getInventory().getContents(); diff --git a/Essentials/src/main/resources/messages.properties b/Essentials/src/main/resources/messages.properties index 19125df7a54..632a3e382b5 100644 --- a/Essentials/src/main/resources/messages.properties +++ b/Essentials/src/main/resources/messages.properties @@ -118,6 +118,7 @@ burnMsg=You set {0} on fire for {1} seco cannotSellNamedItem=You are not allowed to sell named items. cannotSellTheseNamedItems=You are not allowed to sell these named items\: {0} cannotStackMob=You do not have permission to stack multiple mobs. +cannotRemoveNegativeItems=You can not remove negative items. canTalkAgain=You can now talk again. cantFindGeoIpDB=Can''t find GeoIP database\! cantGamemode=You do not have permission to change to gamemode {0}