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 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
commandAPICommand("mycommand") {
stringArgument("arg0")
stringArgument("arg1")
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
commandAPICommand("mycommand") {
arguments(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
val arguments = listOf(
StringArgument("arg0"),
StringArgument("arg1"),
StringArgument("arg2")
)
commandAPICommand("mycommand") {
arguments(*arguments.toTypedArray())
// 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 CommandArguments args
is the same as the order in which the arguments were declared.
List<Argument<?>> commandArguments = new ArrayList<>();
commandArguments.add(new StringArgument("arg0"));
commandArguments.add(new PotionEffectArgument("arg1"));
commandArguments.add(new LocationArgument("arg2"));
new CommandAPICommand("cmd")
.withArguments(commandArguments)
.executes((sender, args) -> {
String stringArg = (String) args.get("arg0");
PotionEffectType potionArg = (PotionEffectType) args.get("arg1");
Location locationArg = (Location) args.get("arg2");
})
.register();
val commandArguments = listOf(
StringArgument("arg0"),
PotionEffectArgument("arg1"),
LocationArgument("arg2")
)
CommandAPICommand("cmd")
.withArguments(commandArguments)
.executes(CommandExecutor { _, args ->
val stringArg = args["arg0"] as String
val potionArg = args["arg1"] as PotionEffectType
val locationArg = args["arg2"] as Location
})
.register()
val args = listOf(
StringArgument("arg0"),
PotionEffectArgument("arg1"),
LocationArgument("arg2")
)
commandAPICommand("cmd") {
arguments(*args.toTypedArray())
anyExecutor { _, args ->
val stringArg = args["arg0"] as String
val potionArg = args["arg1"] as PotionEffectType
val locationArg = args["arg2"] as Location
}
}
The type to cast each argument (declared in the dev.jorel.commandapi.arguments
package) is listed below:
Argument class | Data type |
---|---|
AngleArgument | float |
AdvancementArgument | org.bukkit.advancement.Advancement |
AdventureChatArgument | net.kyori.adventure.text.Component |
AdventureChatColorArgument | net.kyori.adventure.text.format.NamedTextColor |
AdventureChatComponentArgument | net.kyori.adventure.text.Component |
AxisArgument | java.util.EnumSet<org.bukkit.Axis> |
BiomeArgument | org.bukkit.block.Biome |
BiomeArgument.NamespacedKey | org.bukkit.NamespacedKey |
BlockPredicateArgument | java.util.function.Predicate <org.bukkit.block.Block> |
BlockStateArgument | org.bukkit.block.data.BlockData |
BooleanArgument | boolean |
ChatArgument | net.md_5.bungee.api.chat.BaseComponent[] |
ChatColorArgument | org.bukkit.ChatColor |
ChatComponentArgument | net.md_5.bungee.api.chat.BaseComponent[] |
CommandArgument | dev.jorel.commandapi.wrappers.CommandResult |
CustomArgument<T, B> | T |
DoubleArgument | double |
EnchantmentArgument | org.bukkit.enchantments.Enchantment |
EntitySelectorArgument.ManyEntities | Collection<org.bukkit.entity.Entity> |
EntitySelectorArgument.ManyPlayers | Collection<org.bukkit.entity.Player> |
EntitySelectorArgument.OneEntity | org.bukkit.entity.Entity |
EntitySelectorArgument.OnePlayer | org.bukkit.entity.Player |
EntityTypeArgument | org.bukkit.entity.EntityType |
FloatArgument | float |
FloatRangeArgument | dev.jorel.commandapi.wrappers.FloatRange |
FunctionArgument | dev.jorel.commandapi.wrappers.FunctionWrapper[] |
GreedyStringArgument | String |
IntegerArgument | int |
IntegerRangeArgument | dev.jorel.commandapi.wrappers.IntegerRange |
ItemStackArgument | org.bukkit.inventory.ItemStack |
ItemStackPredicateArgument | java.util.function.Predicate <org.bukkit.inventory.ItemStack> |
ListArgument | java.util.Collection<T> |
LiteralArgument | N/A |
Location2DArgument | dev.jorel.commandapi.wrappers.Location2D |
LocationArgument | org.bukkit.Location |
LongArgument | long |
LootTableArgument | org.bukkit.loot.LootTable |
MapArgument | java.util.LinkedHashMap |
MathOperationArgument | dev.jorel.commandapi.wrappers.MathOperation |
MultiLiteralArgument | String |
NamespacedKeyArgument | org.bukkit.NamespacedKey |
NBTCompoundArgument<T> | The cast type changes depending on whether you're shading the CommandAPI or using the CommandAPI as a plugin:
|
ObjectiveArgument | org.bukkit.scoreboard.Objective |
ObjectiveCriteriaArgument | String |
OfflinePlayerArgument | org.bukkit.OfflinePlayer |
ParticleArgument | dev.jorel.commandapi.wrappers.ParticleData |
PlayerArgument | org.bukkit.entity.Player |
PotionEffectArgument | org.bukkit.potion.PotionEffectType |
PotionEffectArgument.NamespacedKey | org.bukkit.NamespacedKey |
RecipeArgument | The cast type changes depending on your Minecraft version:
|
RotationArgument | dev.jorel.commandapi.wrappers.Rotation |
ScoreboardSlotArgument | dev.jorel.commandapi.wrappers.ScoreboardSlot |
ScoreHolderArgument.Single | String |
ScoreHolderArgument.Multiple | Collection<String> |
SoundArgument | org.bukkit.Sound |
SoundArgument.NamespacedKey | org.bukkit.NamespacedKey |
StringArgument | String |
TeamArgument | org.bukkit.scoreboard.Team |
TextArgument | String |
TimeArgument | int |
UUIDArgument | java.util.UUID |
WorldArgument | org.bukkit.World |