Skip to main content

Predicate Arguments

A predicate allows for checking for valid values. These arguments are dedicated to checking whether some sort of value is valid according to user input.

Double Range argument

With the double range argument you can define a double, or a range/amount of doubles, which can act as a predicate for numbers. This could be used for an argument which kill all enemies in a specific range or for clearing items in a range of slots. As this argument is rather technical, it is not used very often.

Example usage

public static LiteralCommandNode<CommandSourceStack> doubleRange() {
return Commands.literal("doublerange")
.then(Commands.argument("arg", ArgumentTypes.doubleRange())
.executes(ctx -> {
final DoubleRangeProvider doubleRangeProvider = ctx.getArgument("arg", DoubleRangeProvider.class);
final CommandSender sender = ctx.getSource().getSender();

for (int i = 0; i < 5; i++) {
sender.sendRichMessage("Is <index> in bounds? <result>",
Placeholder.unparsed("index", Integer.toString(i)),
Placeholder.unparsed("result", Boolean.toString(doubleRangeProvider.range().test((double) i)))
);
}
return Command.SINGLE_SUCCESS;
}))
.build();
}

In-game preview

Integer Range Argument

This argument works very similarly to the double range argument, with the only difference being that this argument only accepts integers.

Example usage

MinecraftArguments.java
public static LiteralCommandNode<CommandSourceStack> integerRangeArgument() {
return Commands.literal("integerrange")
.then(Commands.argument("range", ArgumentTypes.integerRange())
.then(Commands.argument("tested_integer", IntegerArgumentType.integer())
.executes(MinecraftArguments::runIntegerRangeCommand)))
.build();
}

private static int runIntegerRangeCommand(final CommandContext<CommandSourceStack> ctx) {
final IntegerRangeProvider integerRangeProvider = ctx.getArgument("range", IntegerRangeProvider.class);
final int integerToTest = ctx.getArgument("tested_integer", int.class);

if (integerRangeProvider.range().contains(integerToTest)) {
ctx.getSource().getSender().sendRichMessage("<aqua><input></aqua> <green>is</green> inside the specified range!",
Placeholder.unparsed("input", Integer.toString(integerToTest))
);
}
else {
ctx.getSource().getSender().sendRichMessage("<aqua><input></aqua> <red>is not</red> inside the specified range!",
Placeholder.unparsed("input", Integer.toString(integerToTest))
);
}

return Command.SINGLE_SUCCESS;
}

In-game preview

Item Predicate Argument

This argument allows for checking whether an item fits some predicate. It is useful for filtering out certain items based on some criteria.

Example usage

public static LiteralCommandNode<CommandSourceStack> itemPredicateArgument() {
return Commands.literal("itempredicate")
.then(Commands.argument("predicate", ArgumentTypes.itemPredicate())
.executes(ctx -> {
final ItemStackPredicate predicate = ctx.getArgument("predicate", ItemStackPredicate.class);
final ItemStack defaultWoodenSword = ItemType.WOODEN_SWORD.createItemStack();

ctx.getSource().getSender().sendRichMessage("Is default wooden sword? <result>.",
Placeholder.parsed("result", predicate.test(defaultWoodenSword) ? "<green>true" : "<red>false")
);
return Command.SINGLE_SUCCESS;
}))
.build();
}

In-game preview