Allow multiple item names in /clearinventory

Allow a comma-separated list of item names in /clearinventory.
This commit is contained in:
md678685 2018-10-15 13:46:12 +01:00
parent 60c90408e7
commit 79bc34047b

View file

@ -14,11 +14,7 @@ import org.bukkit.Server;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import java.util.ArrayList; import java.util.*;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
public class Commandclearinventory extends EssentialsCommand { public class Commandclearinventory extends EssentialsCommand {
@ -89,7 +85,7 @@ public class Commandclearinventory extends EssentialsCommand {
short data = -1; short data = -1;
int type = -1; int type = -1;
int amount = -1; int amount = -1;
Material mat = null; final Set<Material> mats = new HashSet<>();
if (args.length > (offset + 1) && NumberUtil.isInt(args[(offset + 1)])) { if (args.length > (offset + 1) && NumberUtil.isInt(args[(offset + 1)])) {
amount = Integer.parseInt(args[(offset + 1)]); amount = Integer.parseInt(args[(offset + 1)]);
@ -98,16 +94,15 @@ public class Commandclearinventory extends EssentialsCommand {
if (args[offset].equalsIgnoreCase("**")) { if (args[offset].equalsIgnoreCase("**")) {
type = -2; type = -2;
} else if (!args[offset].equalsIgnoreCase("*")) { } else if (!args[offset].equalsIgnoreCase("*")) {
final String[] split = args[offset].split(":"); final String[] split = args[offset].split(",");
final ItemStack item = ess.getItemDb().get(split[0]);
type = ess.getItemDb().getLegacyId(item.getType());
mat = item.getType();
if (split.length > 1 && NumberUtil.isInt(split[1])) { for (String name : split) {
data = Short.parseShort(split[1]); try {
} else { mats.add(ess.getItemDb().get(name).getType());
data = item.getDurability(); } catch (Exception ignored) {}
} }
type = 1;
} }
} }
@ -127,32 +122,27 @@ public class Commandclearinventory extends EssentialsCommand {
InventoryWorkaround.setItemInOffHand(player, null); InventoryWorkaround.setItemInOffHand(player, null);
player.getInventory().setArmorContents(null); player.getInventory().setArmorContents(null);
} else { } else {
if (data == -1) // data -1 means that all subtypes will be cleared for (Material mat : mats) {
{ if (amount == -1) // amount -1 means all items will be cleared
ItemStack stack = new ItemStack(mat); {
if (showExtended) { ItemStack stack = new ItemStack(mat, BASE_AMOUNT, data);
sender.sendMessage(tl("inventoryClearingAllStack", stack.getType().toString().toLowerCase(Locale.ENGLISH), player.getDisplayName())); ItemStack removedStack = player.getInventory().removeItem(stack).get(0);
} final int removedAmount = (BASE_AMOUNT - removedStack.getAmount());
player.getInventory().remove(mat); if (removedAmount > 0 || showExtended) {
} else if (amount == -1) // amount -1 means all items will be cleared sender.sendMessage(tl("inventoryClearingStack", removedAmount, stack.getType().toString().toLowerCase(Locale.ENGLISH), player.getDisplayName()));
{ }
ItemStack stack = new ItemStack(mat, BASE_AMOUNT, data);
ItemStack removedStack = player.getInventory().removeItem(stack).get(0);
final int removedAmount = (BASE_AMOUNT - removedStack.getAmount());
if (removedAmount > 0 || showExtended) {
sender.sendMessage(tl("inventoryClearingStack", removedAmount, stack.getType().toString().toLowerCase(Locale.ENGLISH), player.getDisplayName()));
}
} else {
if (amount < 0) {
amount = 1;
}
ItemStack stack = new ItemStack(mat, amount, data);
if (player.getInventory().containsAtLeast(stack, amount)) {
sender.sendMessage(tl("inventoryClearingStack", amount, stack.getType().toString().toLowerCase(Locale.ENGLISH), player.getDisplayName()));
player.getInventory().removeItem(stack);
} else { } else {
if (showExtended) { if (amount < 0) {
sender.sendMessage(tl("inventoryClearFail", player.getDisplayName(), amount, stack.getType().toString().toLowerCase(Locale.ENGLISH))); amount = 1;
}
ItemStack stack = new ItemStack(mat, amount);
if (player.getInventory().containsAtLeast(stack, amount)) {
sender.sendMessage(tl("inventoryClearingStack", amount, stack.getType().toString().toLowerCase(Locale.ENGLISH), player.getDisplayName()));
player.getInventory().removeItem(stack);
} else {
if (showExtended) {
sender.sendMessage(tl("inventoryClearFail", player.getDisplayName(), amount, stack.getType().toString().toLowerCase(Locale.ENGLISH)));
}
} }
} }
} }