2020-07-10 16:37:18 +02:00
|
|
|
package net.minestom.server.command.builder;
|
|
|
|
|
|
|
|
import net.minestom.server.command.builder.arguments.Argument;
|
|
|
|
import net.minestom.server.command.builder.condition.CommandCondition;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.List;
|
|
|
|
|
2020-07-21 18:04:02 +02:00
|
|
|
/**
|
|
|
|
* Represent a command which have suggestion/auto-completion
|
|
|
|
*/
|
2020-07-14 13:35:07 +02:00
|
|
|
public class Command {
|
2020-07-10 16:37:18 +02:00
|
|
|
|
|
|
|
private String name;
|
|
|
|
private String[] aliases;
|
|
|
|
|
|
|
|
private CommandExecutor defaultExecutor;
|
|
|
|
private CommandCondition condition;
|
|
|
|
private List<CommandSyntax> syntaxes;
|
|
|
|
|
|
|
|
public Command(String name, String... aliases) {
|
|
|
|
this.name = name;
|
|
|
|
this.aliases = aliases;
|
|
|
|
|
|
|
|
this.syntaxes = new ArrayList<>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Command(String name) {
|
|
|
|
this(name, new String[0]);
|
|
|
|
}
|
|
|
|
|
2020-07-21 18:04:02 +02:00
|
|
|
/**
|
|
|
|
* Get the command condition
|
|
|
|
* <p>
|
|
|
|
* It is called no matter the syntax used and can be used to check permissions or
|
|
|
|
* the {@link net.minestom.server.command.CommandSender} type
|
|
|
|
*
|
|
|
|
* @return the command condition
|
|
|
|
*/
|
2020-07-10 16:37:18 +02:00
|
|
|
public CommandCondition getCondition() {
|
|
|
|
return condition;
|
|
|
|
}
|
|
|
|
|
2020-07-21 18:04:02 +02:00
|
|
|
/**
|
|
|
|
* Set the command condition
|
|
|
|
*
|
|
|
|
* @param commandCondition the new command condition
|
|
|
|
*/
|
2020-07-14 13:35:07 +02:00
|
|
|
public void setCondition(CommandCondition commandCondition) {
|
2020-07-10 16:37:18 +02:00
|
|
|
this.condition = commandCondition;
|
|
|
|
}
|
|
|
|
|
2020-07-21 18:04:02 +02:00
|
|
|
/**
|
|
|
|
* Add an argument callback
|
|
|
|
* <p>
|
|
|
|
* The argument callback is called when there's an error in the argument
|
|
|
|
*
|
|
|
|
* @param callback the callback for the argument
|
|
|
|
* @param argument the argument which get the callback
|
|
|
|
*/
|
2020-07-14 13:35:07 +02:00
|
|
|
public void addCallback(ArgumentCallback callback, Argument argument) {
|
2020-07-10 16:37:18 +02:00
|
|
|
argument.setCallback(callback);
|
|
|
|
}
|
|
|
|
|
2020-07-21 18:04:02 +02:00
|
|
|
/**
|
|
|
|
* Add a new syntax in the command
|
|
|
|
* <p>
|
|
|
|
* A syntax is simply a list of arguments
|
|
|
|
*
|
|
|
|
* @param executor the executor to call when the syntax is successfully received
|
|
|
|
* @param args all the arguments of the syntax
|
|
|
|
*/
|
2020-07-14 13:35:07 +02:00
|
|
|
public void addSyntax(CommandExecutor executor, Argument... args) {
|
2020-07-10 16:37:18 +02:00
|
|
|
CommandSyntax syntax = new CommandSyntax(args);
|
|
|
|
syntax.setExecutor(executor);
|
|
|
|
this.syntaxes.add(syntax);
|
|
|
|
}
|
|
|
|
|
2020-07-21 18:04:02 +02:00
|
|
|
/**
|
|
|
|
* Get the main command's name
|
|
|
|
*
|
|
|
|
* @return the main command's name
|
|
|
|
*/
|
2020-07-10 16:37:18 +02:00
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2020-07-21 18:04:02 +02:00
|
|
|
/**
|
|
|
|
* Get the command's aliases
|
|
|
|
* <p>
|
|
|
|
* Can be null or empty
|
|
|
|
*
|
|
|
|
* @return the command aliases
|
|
|
|
*/
|
2020-07-10 16:37:18 +02:00
|
|
|
public String[] getAliases() {
|
|
|
|
return aliases;
|
|
|
|
}
|
|
|
|
|
2020-07-21 18:04:02 +02:00
|
|
|
/**
|
|
|
|
* Get the default executor (which is called when there is no argument)
|
|
|
|
*
|
|
|
|
* @return the default executor
|
|
|
|
*/
|
2020-07-10 16:37:18 +02:00
|
|
|
public CommandExecutor getDefaultExecutor() {
|
|
|
|
return defaultExecutor;
|
|
|
|
}
|
|
|
|
|
2020-07-21 18:04:02 +02:00
|
|
|
/**
|
|
|
|
* Set the default executor (which is called when there is no argument)
|
|
|
|
*
|
|
|
|
* @param executor the new default executor
|
|
|
|
*/
|
2020-07-14 13:35:07 +02:00
|
|
|
public void setDefaultExecutor(CommandExecutor executor) {
|
2020-07-10 16:37:18 +02:00
|
|
|
this.defaultExecutor = executor;
|
|
|
|
}
|
|
|
|
|
2020-07-21 18:04:02 +02:00
|
|
|
/**
|
|
|
|
* Get all the syntaxes of this command
|
|
|
|
*
|
|
|
|
* @return a collection containing all this command syntaxes
|
|
|
|
*/
|
2020-07-10 16:37:18 +02:00
|
|
|
public Collection<CommandSyntax> getSyntaxes() {
|
|
|
|
return syntaxes;
|
|
|
|
}
|
|
|
|
}
|