Skip to content

SubCommands

Mazen edited this page May 18, 2023 · 3 revisions

SubCommands Creation

In order to create a subcommand, you need to understand what is a subcommand (if you don't). A subcommand is basically a command with args for execution, in which that command can be assigned to a root command to be executed under it's name.

Creating SubCommands using builders:

It's simple, all what you got to do is to initialize the builder of your platform by using <Platform>SubCommandBuilder#builder() Here's a simple example using the mCommands-spigot platform:

               var sub1 = SpigotSubCommandBuilder.builder("test","sub1")
						.argument(Argument.word("username"))
						.children("sub2")
						.execute((sender, context)-> {
							System.out.println("Executing context for sub1 !");
						})
						.build();


		var sub2 = SpigotSubCommandBuilder.builder("test","sub2")
						.argument(Argument.integer("num"))
						.parent("sub1")
						.children("sub3")
						.execute((sender, context)-> {
							System.out.println("Executing context for sub2 !");
						})
						.build();

		var sub3 = SpigotSubCommandBuilder.builder("test", "sub3")
						.argument(Argument.word("address"))
						.parent("sub2")
						.execute((sender, context)-> {
							System.out.println("Executing context for sub3 !");
						})
						.build();

		var cmd = Command.builder(commandManager, "test")
						.syntax(sub1, sub2, sub3)
						.build();

		commandManager.registerCommand(cmd);

In the example above, You have created a chain of subcommands and put them into 1 syntax, so the usage will be like /test sub1 <username> sub2 <num> sub3 <address>

Creating subcommands with Annotations

The same way above can be done using annotations in your annotated command class

Here's a simple example of the aannotatted Command class:

@Command(name = "example")
@SubCommand(value = TestSub1.class)
@SubCommand(value = TestSub2.class)
@SubCommand(value = TestSub3.class)
public class TestAnnotatedCommand {


}

To create a sub command class using annotations, the class must be annotated with BOTH @ExecutionMeta and @SubCommandInfo or else an exception will be thrown.

Moreover, you must create a method called execute and annotate it with @SubCommandExecution. Furthermore, you can specify a parent subcommand if there's and also you can specify children for this sub command in the @SubCommandInfo and you can also specify aliases for this subcommand.

Finally, in the @ExecutionMeta annotation, you must provide the syntax (The CHILD subcommand arguments ONLY) and you can also provide other optional details about the subcommand such as description and permission.

Here are few examples to help you out understand:

Creating subcommand sub1

@SubCommandInfo(name = "sub1", children = TestSub2.class, aliases = "s1")
@ExecutionMeta(syntax = "<username>")
public final class TestSub1 {

	@SubCommandExecution
	public void execute(YourSenderType sender, /*can be specified in the @ExecutionMeta*/
	                    @Arg(id = "username") String username) {
		System.out.println("Executing sub 1 for user '" + username + "'");
	}

}

Creating subcommand sub2

@SubCommandInfo(name = "sub2", aliases = "s2", parent = TestSub1.class, children = TestSub3.class)
@ExecutionMeta(syntax = "<num>")
public final class TestSub2 {

	@SubCommandExecution
	public void execute(YourSenderType sender, /*can be specified in the @ExecutionMeta*/
	                    @Arg(id = "username") String userName,
	                    @Arg(id = "num") int num) {
		System.out.println("Executing sub 2 using user = " + userName + ", number= '" + num + "'");
	}

}

Creating subcommand sub3

@SubCommandInfo(name = "sub3", parent = TestSub2.class)
@ExecutionMeta(syntax = "<address>")
public class TestSub3 {

	@SubCommandExecution
	public void execute(YourSenderType sender, /*can be specified in the @ExecutionMeta*/
	                    @Arg(id = "username") String username,
			    @Arg(id = "num") int num,
	                    @Arg(id = "address") String address) {
		System.out.printf("Executing, username = %s, num=%s, address=%s \n", username, num, address);
	}

}

Finally

As usual you will then have to register the annotated command class using AnnotationParser#parse() like this parser.parse(new TestAnnotatedCommand());

After the subcommands you've made, the syntax will be ALSO like this /test sub1 <username> sub2 <num> sub3 <address>