Minestom/src/main/java/net/minestom/server/command/builder/Command.java

190 lines
6.2 KiB
Java
Raw Normal View History

package net.minestom.server.command.builder;
2020-10-08 01:21:15 +02:00
import net.minestom.server.command.CommandSender;
import net.minestom.server.command.builder.arguments.Argument;
import net.minestom.server.command.builder.arguments.ArgumentDynamicStringArray;
import net.minestom.server.command.builder.arguments.ArgumentDynamicWord;
2020-10-11 15:27:23 +02:00
import net.minestom.server.command.builder.arguments.ArgumentType;
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
/**
2020-10-06 21:03:00 +02:00
* Represents a command which have suggestion/auto-completion
2020-10-11 15:27:23 +02:00
* <p>
* The command works using a list of valid syntaxes.
* For instance we could build the command
* "/health set Notch 50" into multiple argument types -> "/health [set/add/remove] [Username] [Integer]"
* <p>
* All the default argument types can be found in {@link ArgumentType}
* and the syntax be created/registered using {@link #addSyntax(CommandExecutor, Argument[])}.
* <p>
* If the command is executed with an incorrect syntax or without any argument, the default {@link CommandExecutor} will be executed,
* you can set it using {@link #setDefaultExecutor(CommandExecutor)}.
* <p>
* Before any syntax to be successfully executed the {@link CommandSender} needs to validate
* the {@link CommandCondition} sets with {@link #setCondition(CommandCondition)} (ignored if null).
* <p>
* Some {@link Argument} could also require additional condition (eg: a number which need to be between 2 values),
* in this case, if the whole syntax is correct but not the argument condition,
* you can listen to its error code using {@link #setArgumentCallback(ArgumentCallback, Argument)} or {@link Argument#setCallback(ArgumentCallback)}.
2020-07-21 18:04:02 +02:00
*/
public class Command {
private String name;
private String[] aliases;
private CommandExecutor defaultExecutor;
private CommandCondition condition;
private List<CommandSyntax> syntaxes;
2020-10-11 15:27:23 +02:00
/**
* Create a {@link Command} with a name and one or multiple aliases
*
* @param name the name of the command
* @param aliases the command aliases
* @see #Command(String)
*/
public Command(String name, String... aliases) {
this.name = name;
this.aliases = aliases;
this.syntaxes = new ArrayList<>();
}
2020-10-11 15:27:23 +02:00
/**
* Create a {@link Command} with a name without any aliases
*
* @param name the name of the command
* @see #Command(String, String...)
*/
public Command(String name) {
this(name, new String[0]);
}
2020-07-21 18:04:02 +02:00
/**
2020-10-11 15:27:23 +02:00
* Get the {@link CommandCondition}
2020-07-21 18:04:02 +02:00
* <p>
* It is called no matter the syntax used and can be used to check permissions or
2020-10-08 01:21:15 +02:00
* the {@link CommandSender} type
2020-07-21 18:04:02 +02:00
*
* @return the command condition
*/
public CommandCondition getCondition() {
return condition;
}
2020-07-21 18:04:02 +02:00
/**
2020-10-11 15:27:23 +02:00
* Set the {@link CommandCondition}
2020-07-21 18:04:02 +02:00
*
* @param commandCondition the new command condition
*/
public void setCondition(CommandCondition commandCondition) {
this.condition = commandCondition;
}
2020-07-21 18:04:02 +02:00
/**
2020-10-11 15:27:23 +02:00
* Set an {@link ArgumentCallback}
2020-07-21 18:04:02 +02:00
* <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-10-11 15:27:23 +02:00
public void setArgumentCallback(ArgumentCallback callback, Argument argument) {
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
*/
public void addSyntax(CommandExecutor executor, Argument... args) {
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
*/
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
*/
public String[] getAliases() {
return aliases;
}
2020-07-21 18:04:02 +02:00
/**
2020-10-11 15:27:23 +02:00
* Get the default {@link CommandExecutor} (which is called when there is no argument)
2020-10-08 01:28:40 +02:00
* or if no corresponding syntax has been found
2020-07-21 18:04:02 +02:00
*
* @return the default executor
*/
public CommandExecutor getDefaultExecutor() {
return defaultExecutor;
}
2020-07-21 18:04:02 +02:00
/**
2020-10-11 15:27:23 +02:00
* Set the default {@link CommandExecutor} (which is called when there is no argument)
2020-07-21 18:04:02 +02:00
*
* @param executor the new default executor
*/
public void setDefaultExecutor(CommandExecutor executor) {
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
*/
public Collection<CommandSyntax> getSyntaxes() {
return syntaxes;
}
/**
* Allow for tab auto completion, this is called everytime the player press a key in the chat
2020-09-20 15:04:07 +02:00
* when in a dynamic argument ({@link ArgumentDynamicWord} and {@link ArgumentDynamicStringArray})
*
* @param text the whole player text
* @return the array containing all the suggestion for the current arg (split " ")
*/
public String[] onDynamicWrite(String text) {
return null;
}
2020-10-08 01:21:15 +02:00
/**
* Called when a {@link CommandSender} executes this command.
* Executed before any syntax callback.
* <p>
* WARNING: the {@link CommandCondition} is not executed, and all the {@link CommandSyntax} are not checked,
* this is called every time a {@link CommandSender} send a command which start by {@link #getName()} or {@link #getAliases()}.
2020-10-08 01:28:40 +02:00
* <p>
* Can be used if you wish to still suggest the player syntaxes but want to parse things mostly by yourself.
2020-10-08 01:21:15 +02:00
*
* @param sender the {@link CommandSender}
* @param arguments the UNCHECKED arguments of the command, some can be null even when unexpected
* @param command the raw UNCHECKED received command
*/
public void globalListener(CommandSender sender, Arguments arguments, String command) {
}
}