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
1 changed files with 29 additions and 39 deletions

View File

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