-
Notifications
You must be signed in to change notification settings - Fork 4
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", "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();
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 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
There are information that are not necessarily required in order to construct a proper command syntax, such as the following data:
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);
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.
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();
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