-
Notifications
You must be signed in to change notification settings - Fork 3
Command Creation and Registration
All commands in mCommands are created using 1 way which is the builder way !
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();
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 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
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 page for more details about flags.
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.
To register a command, call YourCommandManager#registerCommand
example:
var cmd = ...
commandManager.registerCommand(cmd);
I hope this wiki helped you with learning how to fully utilize mCommands