Creating a Command

Creating the command

The first parameter of a command should always be the executor's class, it can be either CommandSender, ConsoleCommandSender, or Player, based on who's allowed to execute the command.

Here's an example for a command with no arguments and no permission:

@Command(name = "helloworld", aliases = {"hello", "hw"})
public void helloWorld(CommandSender sender) {
    sender.sendMessage("Hello world!");
}

If you wish to include a permission node, you may add permission = "your.permission" to the annotation.

Commands with arguments

If you want to add arguments within your command, you need to use the @Arg annotation to your parameter(s). The annotation requires the name of the argument and you may insert a default value if you don't want to require someone to write an argument.

Your parameters should be a class, and it has to be a registered parameter type. You may register your own parameters as I will show you later. I will list the parameters right beyond this paragraph, but you may also view the current registered parameters here. You can also override existing parameter types.

Registered parameter type classes (by default, may be overridden):

  • Boolean and boolean

  • Integer and int

  • Float and float

  • Double and double

  • Player

  • OfflinePlayer

  • String

  • World

Your argument can also include wildcard strings (adding wildcard = true to your annotation), allowing you to write a whole sentence in your command and it counts as one argument. Wildcards should always be at the end of your parameters, or else it could have conflicts with the other arguments. If one of your parameters is a string, the command executors may use quotes to create a whole sentence, even if wildcard is disabled.

Here's an example with two arguments, one with a default value, one without. Note: Arguments with a default value should never come before one that does not include a default value.

Using flags without values

With this library, you can add flags to your command that will return either true or false based on the player's input. A flag works like this; if you type anywhere in the command (must be after the parameter) the flag (e.g. "-e"), it will output true. Example: /commandname -e arguments_here

The annotation is @Flag, and it only has one value, which is the name. Remember to not include the "-" unless you want to use double dashes. You do not need to include value = "", all you need to do is @Flag("e"). And remember to not use the @Arg annotation.

Here's an example command with flags:

And if you want to use the flags with arguments, this is how it would look like.

Using flags with values

This is practically the same as flags without values, just it doesn't return a boolean. This argument will return an input from the player. The annotation is @FlagValue.

This is how the command will look like with the flag: /flagtest -e "string value". This will make the string parameter have the value "string value".

This is how the command will look like without the flag: /flagtest. This will make the string parameter just return null. Of course, you can use normal arguments and normal flags on the same command.

Here is an example of how you can construct a command with @FlagValue:


Registering the command

Using the CommandHandler class, you can register the command by using the CommandHandler#register method. It is a static method thus giving you direct access to the method. This method will require a Class<?>, which is the class of where the command is currently held.

Here's an example:

Last updated