Skip to content

Command Creation and Registration

Mazen edited this page Apr 17, 2024 · 8 revisions

Command Creation

All commands in mCommands are created using 1 way which is the builder way !

Command Creation Example:

var cmd = Command.builder(commandManager, "test")
	.info(new CommandInfo("test.perm", "Test cmd", "testis"))
	.requirement(SpigotCommandRequirement.ONLY_PLAYER_EXECUTABLE)
	.cooldown(new CommandCooldown(5, TimeUnit.MINUTES))
        .coordination(CommandExecutionCoordinator.Type.ASYNC)
	.syntax(
          SpigotCommandSyntaxBuilder.builder(commandManager, CustomSender.class, "test")
	  .argument(Argument.literal("testsub"))
	  .execute((sender, context) -> {
		sender.sendWoo();
	   }).build()
        )
	.defaultExecutor((s, context) -> s.sendMessage("OMG NO ARGS !"))
	.build();

Command Info

The Command.Builder#info() builder method of the command is some data about the command such as it's permission, description and it's aliases

Command Requirements

Command requirements are basically requirements or conditions for the command to be executed successfully you can add your own requirements by implementing <Platform>CommandRequirement interface, in spigot platform case, you should implement SpigotCommandRequirement

Here's an example:

public class CustomCommandRequirement implements SpigotCommandRequirement {
	
	@Override
	public boolean accepts(CommandSender sender, Context<CommandSender> commandContext) {
		return sender.getName().equalsIgnoreCase("Mqzen"); // your condition;
	}

	/**
	 * @return the key of the caption message to send if the condition/requirement is not true !
	 * setting this to null will cause no message to be sent !
	 */
	@Override
	public @Nullable CaptionKey caption() {
		return null;
	}
	
}

for more information about captions page Captions

Command Syntax Declaration

A command syntax is the syntax for execution of the command a command can have multiple syntaxes which can act as subcommands

Creating a syntax requires several steps. First step: is initiating the builder of that syntax (could be SpigotCommandSyntaxBuilder#builder or BungeeCommandSyntaxBuilder#builder) Second step: is declaring the information of this syntax such as the arguments required and the execution

Optional steps:

There are information that are not necessarily required in order to construct a proper command syntax, such as the following data:

Custom Command Senders

You can choose to define custom senders in syntax.however, if you don't choose to define a custom sender, then don't worry , the command sender type is the original sender type of the platform by default

Let's use this example of a custom sender class:

public class CustomSender {


	@NotNull
	private final CommandSender sender;

	public CustomSender(@NotNull CommandSender sender) {
		this.sender = sender;
	}

	public void sendWoo() {
		sender.sendMessage("WOOOOOOO");
	}

}

if you choose to define a custom sender, that custom sender type MUST BE REGISTERED in your command manager's sender provider registry to do that you should do this before creating the command like this:

	commandManager.senderProviderRegistry()
		.registerSenderProvider(CustomSender.class, CustomSender::new);

Command flags

You can define flags that can be used in that syntax, in which these flags ARE REGISTERED in the command manager's flag registry, using SyntaxFlags.of(//your flags here).

Check out Flags page for more details about flags.

Command Cooldowns

You can give the command a cooldown to prevent abuse of that command by calling Command.Builder#cooldown method here's a simple example:

var cmd = Command.builder(commandManager, "hello")
	 .cooldown(new CommandCooldown(5, TimeUnit.MINUTES))
	 .defaultExecutor((sender, context)-> sender.sendMessage("Hello " + sender.getName()))
	 .build();

Command Default Execution

It's the execution that is executed when there's no arguments provided in the context provided by the command sender example: "/test" here there were no args input.

Command Registration

To register a command, call YourCommandManager#registerCommand example:

  var cmd = ...
  commandManager.registerCommand(cmd);