2020-07-10 16:37:18 +02:00
|
|
|
package net.minestom.server.command.builder;
|
|
|
|
|
2020-10-08 01:21:15 +02:00
|
|
|
import net.minestom.server.command.CommandSender;
|
2020-07-10 16:37:18 +02:00
|
|
|
import net.minestom.server.command.builder.arguments.Argument;
|
2020-08-04 06:14:42 +02:00
|
|
|
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;
|
2020-07-10 16:37:18 +02:00
|
|
|
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
|
2020-10-11 17:05:02 +02:00
|
|
|
* "/health set Notch 50" into multiple argument types "/health [set/add/remove] [Username] [Integer]"
|
2020-10-11 15:27:23 +02:00
|
|
|
* <p>
|
|
|
|
* All the default argument types can be found in {@link ArgumentType}
|
|
|
|
* and the syntax be created/registered using {@link #addSyntax(CommandExecutor, Argument[])}.
|
|
|
|
* <p>
|
2020-10-11 15:42:22 +02:00
|
|
|
* If the command is executed with an incorrect syntax or without any argument, the default {@link CommandExecutor} will be called,
|
2020-10-11 15:27:23 +02:00
|
|
|
* you can set it using {@link #setDefaultExecutor(CommandExecutor)}.
|
|
|
|
* <p>
|
2020-10-11 15:42:22 +02:00
|
|
|
* Before any syntax to be successfully executed the {@link CommandSender} needs to validated
|
2020-10-11 15:27:23 +02:00
|
|
|
* 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
|
|
|
*/
|
2020-07-14 13:35:07 +02:00
|
|
|
public class Command {
|
2020-07-10 16:37:18 +02:00
|
|
|
|
2020-10-11 18:35:32 +02:00
|
|
|
private final String name;
|
|
|
|
private final String[] aliases;
|
2020-07-10 16:37:18 +02:00
|
|
|
|
|
|
|
private CommandExecutor defaultExecutor;
|
|
|
|
private CommandCondition condition;
|
2020-10-11 18:35:32 +02:00
|
|
|
|
|
|
|
private final List<CommandSyntax> syntaxes;
|
2020-07-10 16:37:18 +02:00
|
|
|
|
2020-10-11 15:27:23 +02:00
|
|
|
/**
|
2020-10-15 21:16:31 +02:00
|
|
|
* Creates a {@link Command} with a name and one or multiple aliases.
|
2020-10-11 15:27:23 +02:00
|
|
|
*
|
|
|
|
* @param name the name of the command
|
|
|
|
* @param aliases the command aliases
|
|
|
|
* @see #Command(String)
|
|
|
|
*/
|
2020-07-10 16:37:18 +02:00
|
|
|
public Command(String name, String... aliases) {
|
|
|
|
this.name = name;
|
|
|
|
this.aliases = aliases;
|
|
|
|
|
|
|
|
this.syntaxes = new ArrayList<>();
|
|
|
|
}
|
|
|
|
|
2020-10-11 15:27:23 +02:00
|
|
|
/**
|
2020-10-15 21:16:31 +02:00
|
|
|
* Creates a {@link Command} with a name without any aliases.
|
2020-10-11 15:27:23 +02:00
|
|
|
*
|
|
|
|
* @param name the name of the command
|
|
|
|
* @see #Command(String, String...)
|
|
|
|
*/
|
2020-07-10 16:37:18 +02:00
|
|
|
public Command(String name) {
|
|
|
|
this(name, new String[0]);
|
|
|
|
}
|
|
|
|
|
2020-07-21 18:04:02 +02:00
|
|
|
/**
|
2020-10-15 21:16:31 +02:00
|
|
|
* Gets 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-15 21:16:31 +02:00
|
|
|
* the {@link CommandSender} type.
|
2020-07-21 18:04:02 +02:00
|
|
|
*
|
|
|
|
* @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
|
|
|
/**
|
2020-10-15 21:16:31 +02:00
|
|
|
* Sets the {@link CommandCondition}.
|
2020-07-21 18:04:02 +02:00
|
|
|
*
|
|
|
|
* @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
|
|
|
/**
|
2020-10-15 21:16:31 +02:00
|
|
|
* Sets an {@link ArgumentCallback}.
|
2020-07-21 18:04:02 +02:00
|
|
|
* <p>
|
2020-10-15 21:16:31 +02:00
|
|
|
* The argument callback is called when there's an error in the argument.
|
2020-07-21 18:04:02 +02:00
|
|
|
*
|
|
|
|
* @param callback the callback for the argument
|
|
|
|
* @param argument the argument which get the callback
|
|
|
|
*/
|
2020-10-11 18:35:32 +02:00
|
|
|
public void setArgumentCallback(ArgumentCallback callback, Argument<?> argument) {
|
2020-07-10 16:37:18 +02:00
|
|
|
argument.setCallback(callback);
|
|
|
|
}
|
|
|
|
|
2020-07-21 18:04:02 +02:00
|
|
|
/**
|
2020-10-15 21:16:31 +02:00
|
|
|
* Adds a new syntax in the command.
|
2020-07-21 18:04:02 +02:00
|
|
|
* <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-10-11 18:35:32 +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
|
|
|
/**
|
2020-10-15 21:16:31 +02:00
|
|
|
* Gets the main command's name.
|
2020-07-21 18:04:02 +02:00
|
|
|
*
|
|
|
|
* @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
|
|
|
/**
|
2020-10-15 21:16:31 +02:00
|
|
|
* Gets the command's aliases.
|
2020-07-21 18:04:02 +02:00
|
|
|
* <p>
|
2020-10-15 21:16:31 +02:00
|
|
|
* Can be null or empty.
|
2020-07-21 18:04:02 +02:00
|
|
|
*
|
|
|
|
* @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
|
|
|
/**
|
2020-10-15 21:16:31 +02:00
|
|
|
* Gets the default {@link CommandExecutor} (which is called when there is no argument)
|
|
|
|
* or if no corresponding syntax has been found.
|
2020-07-21 18:04:02 +02:00
|
|
|
*
|
|
|
|
* @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
|
|
|
/**
|
2020-10-15 21:16:31 +02:00
|
|
|
* Sets 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
|
|
|
|
*/
|
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
|
|
|
/**
|
2020-10-15 21:16:31 +02:00
|
|
|
* Gets all the syntaxes of this command.
|
2020-07-21 18:04:02 +02:00
|
|
|
*
|
|
|
|
* @return a collection containing all this command syntaxes
|
|
|
|
*/
|
2020-07-10 16:37:18 +02:00
|
|
|
public Collection<CommandSyntax> getSyntaxes() {
|
|
|
|
return syntaxes;
|
|
|
|
}
|
2020-08-04 06:14:42 +02:00
|
|
|
|
|
|
|
/**
|
2020-10-15 21:16:31 +02:00
|
|
|
* Allows for tab auto completion, this is called everytime the player press a key in the chat
|
|
|
|
* when in a dynamic argument ({@link ArgumentDynamicWord} and {@link ArgumentDynamicStringArray}).
|
2020-08-04 06:14:42 +02:00
|
|
|
*
|
2020-10-11 18:35:32 +02:00
|
|
|
* @param text the whole player's text
|
2020-08-04 06:14:42 +02:00
|
|
|
* @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) {
|
|
|
|
}
|
|
|
|
|
2020-07-10 16:37:18 +02:00
|
|
|
}
|