From 7eb31f713573c67aad2e531d0a2763550b0d894d Mon Sep 17 00:00:00 2001 From: Evidentsinger14 Date: Sat, 10 Aug 2024 20:59:31 -0400 Subject: [PATCH 1/2] ensure amount of items is positive. --- .../essentials/commands/Commandclearinventory.java | 8 +++++++- Essentials/src/main/resources/messages.properties | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) 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..ef9cefeba6c 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); 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} From 202f55686917e00e2eac53cbd3b905257022c353 Mon Sep 17 00:00:00 2001 From: JRoy <10731363+JRoy@users.noreply.github.com> Date: Sun, 11 Aug 2024 10:34:34 -0400 Subject: [PATCH 2/2] Throw IAE in Inventories#removeItemAmount when amount < 0 --- .../earth2me/essentials/commands/Commandclearinventory.java | 3 +-- .../java/com/earth2me/essentials/craftbukkit/Inventories.java | 4 ++++ 2 files changed, 5 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 ef9cefeba6c..3a284ff0c6a 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandclearinventory.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandclearinventory.java @@ -126,7 +126,7 @@ protected void clearHandler(final CommandSource sender, final Player player, fin } // can't remove a negative amount of items. (it adds them) - if(amount < -1){ + if (amount < -1) { throw new TranslatableException("cannotRemoveNegativeItems"); } @@ -137,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();