Arguments

Arguments in the CommandAPI are registered by using an Argument[] or List<Argument> object. There are two things you need to keep in mind when creating arguments:

  • The order which they will be used
  • The type of each argument

By definition of a List, the order of the elements inserted into it are preserved, meaning the order you add arguments to the List will be the resulting order of which arguments are presented to the user when they run that command.

Adding arguments for registration is simple:

// Create a List
List<Argument> arguments = new ArrayList<>();

// Add an argument with the node "target", which is a PlayerArgument
arguments.add(new PlayerArgument("target"));

The String value is the node that is registered into Minecraft's internal command graph. This is name is also used as a prompt that is shown to a player when they are entering the command.


The CommandAPI is very flexible when it comes to registering arguments, and lets you use a number of different methods to suit your preference:

new CommandAPICommand("mycommand")
    .withArguments(new StringArgument("arg0"))
    .withArguments(new StringArgument("arg1"))
    .withArguments(new StringArgument("arg2"))
    // And so on
CommandAPICommand("mycommand")
    .withArguments(StringArgument("arg0"))
    .withArguments(StringArgument("arg1"))
    .withArguments(StringArgument("arg2"))
    // And so on
new CommandAPICommand("mycommand")
    .withArguments(new StringArgument("arg0"), new StringArgument("arg1"), new StringArgument("arg2"))
    // And so on
CommandAPICommand("mycommand")
    .withArguments(StringArgument("arg0"), StringArgument("arg1"), StringArgument("arg2"))
    // And so on
List<Argument<?>> arguments = new ArrayList<>();
arguments.add(new StringArgument("arg0"));
arguments.add(new StringArgument("arg1"));
arguments.add(new StringArgument("arg2"));

new CommandAPICommand("mycommand")
    .withArguments(arguments)
    // And so on
val arguments = listOf(
    StringArgument("arg0"),
    StringArgument("arg1"),
    StringArgument("arg2")
)

CommandAPICommand("mycommand")
    .withArguments(arguments)
    // And so on

Argument Casting

To access arguments, they have to be casted to the type that the argument represents. The order of the arguments in the args[] is the same as the order in which the arguments were declared.

List<Argument<?>> arguments = new ArrayList<>();
arguments.add(new StringArgument("arg0"));
arguments.add(new PotionEffectArgument("arg1"));
arguments.add(new LocationArgument("arg2"));

new CommandAPICommand("cmd")
    .withArguments(arguments)
    .executes((sender, args) -> {
        String stringArg = (String) args[0];
        PotionEffectType potionArg = (PotionEffectType) args[1];
        Location locationArg = (Location) args[2];
    })
    .register();
val arguments = listOf(
    StringArgument("arg0"),
    PotionEffectArgument("arg1"),
    LocationArgument("arg2")
)

CommandAPICommand("cmd")
    .withArguments(arguments)
    .executes(CommandExecutor { _, args ->
        val stringArg = args[0] as String
        val potionArg = args[1] as PotionEffectType
        val locationArg = args[2] as Location
    })
    .register()

The type to cast each argument (declared in the dev.jorel.commandapi.arguments package) is listed below:

Argument classData type
AngleArgumentfloat
AdvancementArgumentorg.bukkit.advancement.Advancement
AdventureChatArgumentnet.kyori.adventure.text.Component
AdventureChatComponentArgumentnet.kyori.adventure.text.Component
AxisArgumentjava.util.EnumSet<org.bukkit.Axis>
BiomeArgumentorg.bukkit.block.Biome
BiomeArgument.NamespacedKeyorg.bukkit.NamespacedKey
BlockPredicateArgumentjava.util.function.Predicate
<org.bukkit.block.Block>
BlockStateArgumentorg.bukkit.block.data.BlockData
BooleanArgumentboolean
ChatArgumentnet.md_5.bungee.api.chat.BaseComponent[]
ChatColorArgumentorg.bukkit.ChatColor
ChatComponentArgumentnet.md_5.bungee.api.chat.BaseComponent[]
CommandArgumentdev.jorel.commandapi.wrappers.CommandResult
CustomArgument<T, B>T
DoubleArgumentdouble
EnchantmentArgumentorg.bukkit.enchantments.Enchantment
EntitySelectorArgument.ManyEntitiesCollection<org.bukkit.entity.Entity>
EntitySelectorArgument.ManyPlayersCollection<org.bukkit.entity.Player>
EntitySelectorArgument.OneEntityorg.bukkit.entity.Entity
EntitySelectorArgument.OnePlayerorg.bukkit.entity.Player
EntityTypeArgumentorg.bukkit.entity.EntityType
EnvironmentArgumentorg.bukkit.World.Environment
FloatArgumentfloat
FloatRangeArgumentdev.jorel.commandapi.wrappers.FloatRange
FunctionArgumentdev.jorel.commandapi.wrappers.FunctionWrapper[]
GreedyStringArgumentString
IntegerArgumentint
IntegerRangeArgumentdev.jorel.commandapi.wrappers.IntegerRange
ItemStackArgumentorg.bukkit.inventory.ItemStack
ItemStackPredicateArgumentjava.util.function.Predicate
<org.bukkit.inventory.ItemStack>
ListArgumentjava.util.Collection<T>
LiteralArgumentN/A
Location2DArgumentdev.jorel.commandapi.wrappers.Location2D
LocationArgumentorg.bukkit.Location
LongArgumentlong
LootTableArgumentorg.bukkit.loot.LootTable
MathOperationArgumentdev.jorel.commandapi.wrappers.MathOperation
MultiLiteralArgumentString
NamespacedKeyArgumentorg.bukkit.NamespacedKey
NBTCompoundArgument<T>The cast type changes depending on whether you're shading the CommandAPI or using the CommandAPI as a plugin:
  • Shading:
    T (implemented yourself)

  • Plugin:
    dev.jorel.commandapi.nbtapi.NBTContainer
ObjectiveArgumentString
ObjectiveCriteriaArgumentString
OfflinePlayerArgumentorg.bukkit.OfflinePlayer
ParticleArgumentdev.jorel.commandapi.wrappers.ParticleData
PlayerArgumentorg.bukkit.entity.Player
PotionEffectArgumentorg.bukkit.potion.PotionEffectType
RecipeArgumentThe cast type changes depending on your Minecraft version:
  • Version 1.14.4 and below:
    org.bukkit.inventory.Recipe

  • 1.15 and above:
    org.bukkit.inventory.ComplexRecipe
RotationArgumentdev.jorel.commandapi.wrappers.Rotation
ScoreboardSlotArgumentdev.jorel.commandapi.wrappers.ScoreboardSlot
ScoreHolderArgument.SingleString
ScoreHolderArgument.MultipleCollection<String>
SoundArgumentorg.bukkit.Sound
SoundArgument.NamespacedKeyorg.bukkit.NamespacedKey
StringArgumentString
TeamArgumentString
TextArgumentString
TimeArgumentint
UUIDArgumentjava.util.UUID
WorldArgumentorg.bukkit.World

Optional/Different Arguments

Sometimes, you want to register a command that has a different effect whether arguments are included or not. For example, take the /kill command. If you run /kill on its own, it will kill the command sender. If however you run /kill <target>, it will kill the target. In other words, we have the following command command syntax:

/kill          - Kills yourself
/kill <target> - Kills a target player

As shown by the command syntax, we need to register two commands.

Example - /kill command with two separate arguments

For example, say we're registering a command /kill:

/kill          - Kills yourself
/kill <target> - Kills a target player

We first register the first /kill command as normal:

new CommandAPICommand("kill")
    .executesPlayer((player, args) -> {
        player.setHealth(0);
    })
    .register();
CommandAPICommand("kill")
    .executesPlayer(PlayerCommandExecutor { player, _ ->
        player.setHealth(0.0)
    })
    .register()

Now we declare our command with arguments for our second command. Then, we can register our second command /kill <target> as usual:

// Register our second /kill <target> command
new CommandAPICommand("kill")
    .withArguments(new PlayerArgument("target"))
    .executesPlayer((player, args) -> {
        ((Player) args[0]).setHealth(0);
    })
    .register();
// Register our second /kill <target> command
CommandAPICommand("kill")
    .withArguments(PlayerArgument("target"))
    .executesPlayer(PlayerCommandExecutor { _, args ->
        (args[0] as Player).setHealth(0.0)
    })
    .register()

This gives us the ability to run both /kill and /kill <target> with the same command name "kill", but have different results based on the arguments used.

In this example, we use the simpler, inline .withArguments(Argument... arguments) method to register our argument. There is no difference to using this method as opposed to explicitly declaring a list and using .withArguments(List<Argument> arguments), so feel free to use whichever method you want!