Skip to content

Instantly share code, notes, and snippets.

@zml2008
Last active December 17, 2015 15:29
Show Gist options
  • Select an option

  • Save zml2008/5631908 to your computer and use it in GitHub Desktop.

Select an option

Save zml2008/5631908 to your computer and use it in GitHub Desktop.
Example of new argument parsing for /clear!
@CommandDescription(aliases = "clear", usage = "[player] [item] [data]", desc = "Clears the target's inventory", min = 0, max = 3)
@Permissible("vanilla.command.clear")
public void clear(CommandSource source, CommandArguments args) throws CommandException {
Player player = args.popPlayerOrMe("player", source);
Material filter = VanillaArgumentTypes.popMaterial("filter", args);
Integer data = args.popInteger("data");
PlayerInventory inv = player.get(PlayerInventory.class);
if (inv == null) {
throw new CommandException(player.getName() + " doesn't have a inventory!");
} else {
// Count the items and clear the inventory
Inventory[] inventories = new Inventory[]{inv.getMain(), inv.getQuickbar(), inv.getArmor()};
int cleared = 0;
for (int k = 0; k < inventories.length; k++) {
for (int i = 0; i < inventories[k].size(); i++) {
if (inventories[k].get(i) != null && (filter == null || inventories[k].get(i).isMaterial(filter)) && (data == null || inventories[k].get(i).getData() == data)) {
cleared += inventories[k].get(i).getAmount();
inventories[k].set(i, null);
}
}
}
if (cleared == 0) {
throw new CommandException("Inventory is already empty");
}
source.sendMessage(plugin.getPrefix() + ChatStyle.GREEN + "Cleared the inventory of " + player.getName() + ", removing " + cleared + " items.");
}
}
@Command(aliases = "clear", usage = "[player] [item] [data]", desc = "Clears the target's inventory", min = 0, max = 3)
@CommandPermissions("vanilla.command.clear")
public void clear(CommandSource source, CommandArguments args) throws CommandException {
Player player;
Material filter = null;
Integer data = null;
if (args.length() == 0) {
if (!(source instanceof Player)) {
throw new CommandException("You must be a player to clear your own inventory.");
}
player = (Player) source;
} else if (args.length() == 3) {
if (getEngine() instanceof Client) {
throw new CommandException("You cannot search for players unless you are in server mode.");
}
player = getEngine().getPlayer(args.getString(0), false);
if (player == null) {
throw new CommandException(args.getString(0) + " is not online.");
}
if (args.isInteger(1)) {
filter = VanillaMaterials.getMaterial((short) args.getInteger(1));
} else {
filter = Material.get(args.getString(1));
}
data = args.getInteger(2);
} else {
if (args.isInteger(0)) {
if (!(source instanceof Player)) {
throw new CommandException("You must be a player to clear your own inventory.");
}
player = (Player) source;
filter = VanillaMaterials.getMaterial((short) args.getInteger(0));
} else {
if (getEngine() instanceof Client) {
throw new CommandException("You cannot search for players unless you are in server mode.");
}
player = getEngine().getPlayer(args.getString(0), false);
if (player == null) {
filter = Material.get(args.getString(0));
if (filter == null) {
throw new CommandException(args.getString(0) + " is not online.");
}
if (!(source instanceof Player)) {
throw new CommandException("You must be a player to clear your own inventory.");
}
player = (Player) source;
}
}
if (args.length() == 2) {
if (args.isInteger(1)) {
if (filter == null) {
filter = VanillaMaterials.getMaterial((short) args.getInteger(1));
} else {
data = args.getInteger(1);
}
} else {
if (filter == null) {
filter = Material.get(args.getString(1));
} else {
throw new NumberFormatException();
}
}
}
}
PlayerInventory inv = player.get(PlayerInventory.class);
if (inv == null) {
throw new CommandException(player.getName() + " doesn't have a inventory!");
} else {
// Count the items and clear the inventory
Inventory[] inventories = new Inventory[] { inv.getMain(), inv.getQuickbar(), inv.getArmor() };
int cleared = 0;
for (int k = 0; k < inventories.length; k++) {
for (int i = 0; i < inventories[k].size(); i++) {
if (inventories[k].get(i) != null && (filter == null || inventories[k].get(i).isMaterial(filter)) && (data == null || inventories[k].get(i).getData() == data)) {
cleared += inventories[k].get(i).getAmount();
inventories[k].set(i, null);
}
}
}
if (cleared == 0) {
throw new CommandException("Inventory is already empty");
}
source.sendMessage(plugin.getPrefix() + ChatStyle.GREEN + "Cleared the inventory of " + player.getName() + ", removing " + cleared + " items.");
}
}
@kitskub

kitskub commented Jul 13, 2013

Copy link
Copy Markdown

In New, Line 12, the else isn't needed because a CommandException would be thrown and the method wouldn't continue.
EDIT: see my fork

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment