Skip to content

4. Command System

Fllip edited this page Mar 11, 2021 · 7 revisions

SimpleCloud provides an own command system that can be used in modules.

Creating a command

Fist, you need to create a class. The class must implement ICommandHandler and must have the annotation @Command

@Command(name = "test", /*command name */
		commandType = CommandType.INGAME /*command type: explanation below */,
		permission = "command.test.use")
public class YourCommand implements ICommandHandler {

The command type can be INGAME, CONSOLE and CONSOLE_AND_INGAME. If the type is CONSOLE or CONSOLE_AND_INGAME the command will have the /cloud prefix. If the type is INAGME the command will only be executable by players.

The following example is without any parameters.

@CommandSubPath() //creates a command with no arguments (/test)
public void handle(ICommandSender sender /*the sender of the command*/) {
    //a sender can be a ICloudPlayer or ConsoleSender
}

The next example has infinite arguments as you have in Bukkit and BungeeCord.

@CommandSubPath() //creates a command for all arguments (/test [...])
public void handle(ICommandSender sender, String[] args) {

}

The next example creates a command with one fixed argument.

@CommandSubPath(path = "execute") //creates a command with "execute" as a fixed argument (/test execute)
public void handleExecute(ICommandSender sender) {

}

The annotation @CommandSubPath can have a path as an argument. In this path, you write fixed and not fixed arguments.

Fixed arguments are arguments without the <>. They are fixed and you have to type them as they are when executing the command. Non-fixed arguments are arguments with the <>. They are like variables. Example:

@CommandSubPath(path = "execute <service>")

The argument "execute" is fixed. The argument "service" can be anything the user types here. To get the argument the user typed there you can use the @CommandArgument annotation. The annotation has a name-value which shall be exactly the one from the @CommandSubPath without the <>. The full command will look like this:

@CommandSubPath(path = "execute <service>") //creates a command with one fixed and one parameter (/test execute <name>)
public void handleExecute(ICommandSender sender, @CommandArgument(name = "service") String argument) {
    //the parameter <name> specified in the path will be invoked
    //in the "argument" parameter of this method, because the
    //annotation @CommandArgument has also the value name="service"
}

Another example:

@CommandSubPath(path = "message <service> <message>")
public void handleExecute(ICommandSender sender,
                          @CommandArgument(name = "service") String service,
                          @CommandArgument(name = "message") String delay) {

}

You don't have to use a String as argument type every time. It is also possible to use other supported types. Supported types are Integer, Boolean, Double, Float, ITemplate, ICloudServiceGroup and every enum. So a command can look like this:

@CommandSubPath(path = "execute <name>") //creates a command with one fixed and one parameter (/test execute <name>)
public void handleExecute(ICommandSender sender, @CommandArgument(name = "name") ICloudService service) {
    //In this example the specified name is parsed to a service.
}

Registering a command

To register a command you need to have the launcher dependency in your pom.xml file. You can find the dependency in the API tab. If you have it you can register the command:

    Launcher.getInstance().getCommandManager().registerCommand(yourCloudModule, new YourCommand());

Clone this wiki locally