ItemStack predicate arguments

Similar to the BlockPredicateArgument, the ItemStackPredicateArgument is a way of performing predicate checks on ItemStack objects. These can represent tags, such as the ones declared here on the MinecraftWiki, or individual items. The cast type for this argument is Predicate<ItemStack>.

Example - Removing items in inventories based on predicates

Say we wanted to remove items in your inventory (I know, the /clear command does this, but this is the only example I could come up with). To do this, we'll use the following command syntax:

/rem <item>

We implement this with a simple for loop over the player's inventory and remove items that satisfy the predicate.

// Register our command
new CommandAPICommand("rem")
    .withArguments(new ItemStackPredicateArgument("items"))
    .executesPlayer((player, args) -> {
        
        // Get our predicate
        @SuppressWarnings("unchecked")
        Predicate<ItemStack> predicate = (Predicate<ItemStack>) args[0];
        
        for (ItemStack item : player.getInventory()) {
            if (predicate.test(item)) {
                player.getInventory().remove(item);
            }
        }
    })
    .register();
// Register our command
CommandAPICommand("rem")
    .withArguments(ItemStackPredicateArgument("items"))
    .executesPlayer(PlayerCommandExecutor { player, args ->

        // Get our predicate
        val predicate = args[0] as Predicate<ItemStack>

        for (item in player.getInventory()) {
            if (predicate.test(item)) {
                player.inventory.remove(item)
            }
        }
    })
    .register()