Skip to content

Using command arguments

Luca edited this page Jan 30, 2022 · 1 revision

In many cases, we want to create commands to which we can pass arguments. To add one or more arguments to our command, we simply add them as additional parameters to our execute method. Bedrock will than handle all the nescessary type checking and transforms the text input of the user into the needed data type. By default, we can use the following data types:

  • String
  • int
  • double
  • float
  • UUID an UUID, either with dashes or without
  • BedrockPlayer an Player that is currently online (name or UUID)
  • BedrockOfflinePlayer an Player that has played on the server before (last known name or UUID)

A command handler that has a player and a string as arguments would have the following execute method:

public void execute(BedrockChatSender sender, BedrockPlayer player, String text) {}

Custom argument types

Enum as argument type

Optional Arguments

Marking an argument as beeing optional is done by wrapping the data type in the generic Optional class. All optional arguments have to be last and can not be followed by any non optional parameters. In case you are not familliar with the varargs syntax yet, make sure to read up on it: oracle docs.

public void execute(BedrockChatSender sender, Optional<int> page) {}

Using multiple arguments as an array

If we wish to process a sequence of multiple arguments of the same data type and of unspecified length, we can do so by using the varargs syntax. Each method is only allowed to have one varargs argument and it has to be the last argument. In case you are not familliar with the varargs syntax yet, make sure to read up on it: oracle docs.

public void execute(BedrockChatSender sender, BedrockPlayer player, String... text) {}