Skip to content
This repository was archived by the owner on Apr 12, 2025. It is now read-only.

Command Creation and Registration

Mazen edited this page Apr 16, 2023 · 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", "t"))
						.requirement(SpigotCommandRequirement.ONLY_PLAYER_EXECUTABLE)
						.syntax((sender, context) -> {

								String name = context.getArgument("name");
								String permission = context.getArgument("perm");
								Boolean value = context.getArgument("value");

								if (context.flags().isPresent("silent"))
									sender.sendMessage(ChatColor.RED + "Silently trying to change permission >:D ?");
								
								if (name != null && permission != null)
									sender.sendMessage("Name => " + name + ", Permission => " + permission + ", Value => " + value);
								
						}, SyntaxFlags.of("silent"),
										Argument.literal("user"), Argument.word("name"), Argument.literal("permission"),
										Argument.literal("set"), Argument.word("perm"), Argument.Boolean("value")
														.asOptional(true).setDefaultValue(true))
						.defaultExecutor((sender, context) -> sender.sendMessage("Default Execution !"))
						.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](https://captions.com)

## 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 defining the execution of that syntax (`(sender, context) -> {//execute here}`)
second step: is defining the arguments of that syntax 

Optional steps:
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)` so that, the 2nd parameter in `Command.Builder#syntax()` parameter is an array of flag names

Check out [Flags](https://flags.com) page for more details about flags.

## 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:
```java
  var cmd = ...
  commandManager.registerCommand(cmd);
Clone this wiki locally