Skip to content

Commit

Permalink
Fix removing negative items in /clearinventory (#5899)
Browse files Browse the repository at this point in the history
Co-authored-by: JRoy <[email protected]>
  • Loading branch information
Evidentsinger14 and JRoy authored Aug 11, 2024
1 parent 7b02d22 commit b763fd3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Item> items = new HashSet<>();
int amount = -1;
Expand Down Expand Up @@ -124,14 +125,18 @@ 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);
if (removedAmount > 0 || showExtended) {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ public static int removeItems(final Player player, final Predicate<ItemStack> 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<Integer> clearSlots = new ArrayList<>();
final ItemStack[] items = player.getInventory().getContents();

Expand Down
1 change: 1 addition & 0 deletions Essentials/src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ burnMsg=<primary>You set<secondary> {0} <primary>on fire for<secondary> {1} seco
cannotSellNamedItem=<primary>You are not allowed to sell named items.
cannotSellTheseNamedItems=<primary>You are not allowed to sell these named items\: <dark_red>{0}
cannotStackMob=<dark_red>You do not have permission to stack multiple mobs.
cannotRemoveNegativeItems=<dark_red>You can not remove negative items.
canTalkAgain=<primary>You can now talk again.
cantFindGeoIpDB=Can''t find GeoIP database\!
cantGamemode=<dark_red>You do not have permission to change to gamemode {0}
Expand Down

0 comments on commit b763fd3

Please sign in to comment.