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-11-22 23:04:06 +01:00
|
|
|
import net.minestom.server.command.builder.arguments.minecraft.SuggestionType;
|
2020-07-10 16:37:18 +02:00
|
|
|
import net.minestom.server.command.builder.condition.CommandCondition;
|
2020-11-29 21:34:54 +01:00
|
|
|
import net.minestom.server.utils.validate.Check;
|
2020-10-24 16:58:27 +02:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
2020-07-10 16:37:18 +02:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.List;
|
|
|
|
|
2020-07-21 18:04:02 +02:00
|
|
|
/**
|
2020-10-30 18:43:14 +01: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-10-24 16:58:27 +02:00
|
|
|
public Command(@NotNull String name, @Nullable String... aliases) {
|
2020-07-10 16:37:18 +02:00
|
|
|
this.name = name;
|
|
|
|
this.aliases = aliases;
|
|
|
|
|
|
|
|
this.syntaxes = new ArrayList<>();
|
|
|
|
}
|
|
|
|
|
2020-10-11 15:27:23 +02:00
|
|
|
/**
|
2020-11-14 07:06:46 +01:00
|
|
|
* Creates a {@link Command} with a name and no alias.
|
2020-10-11 15:27:23 +02:00
|
|
|
*
|
|
|
|
* @param name the name of the command
|
|
|
|
* @see #Command(String, String...)
|
|
|
|
*/
|
2020-10-24 16:58:27 +02:00
|
|
|
public Command(@NotNull String name) {
|
2020-07-10 16:37:18 +02:00
|
|
|
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>
|
2020-11-14 07:06:46 +01:00
|
|
|
* It is called after the parsing and just before the execution 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-11-14 07:06:46 +01:00
|
|
|
* <p>
|
|
|
|
* Worth mentioning that the condition is also used to know if the command known from a player (at connection).
|
2020-07-21 18:04:02 +02:00
|
|
|
*
|
2020-10-29 19:51:10 +01:00
|
|
|
* @return the command condition, null if not any
|
2020-07-21 18:04:02 +02:00
|
|
|
*/
|
2020-10-29 19:51:10 +01:00
|
|
|
@Nullable
|
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
|
|
|
*
|
2020-10-29 19:51:10 +01:00
|
|
|
* @param commandCondition the new command condition, null to do not call anything
|
2020-11-14 07:06:46 +01:00
|
|
|
* @see #getCondition()
|
2020-07-21 18:04:02 +02:00
|
|
|
*/
|
2020-10-29 19:51:10 +01:00
|
|
|
public void setCondition(@Nullable 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-24 16:58:27 +02:00
|
|
|
public void setArgumentCallback(@NotNull ArgumentCallback callback, @NotNull 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>
|
2020-11-14 07:06:46 +01:00
|
|
|
* A syntax is simply a list of arguments and an executor called when successfully parsed.
|
2020-11-04 04:45:46 +01:00
|
|
|
*
|
|
|
|
* @param commandCondition the condition to use the syntax
|
|
|
|
* @param executor the executor to call when the syntax is successfully received
|
|
|
|
* @param args all the arguments of the syntax
|
|
|
|
* @return the created {@link CommandSyntax}
|
|
|
|
*/
|
|
|
|
public CommandSyntax addSyntax(@Nullable CommandCondition commandCondition,
|
|
|
|
@NotNull CommandExecutor executor,
|
|
|
|
@NotNull Argument<?>... args) {
|
2020-11-29 21:34:54 +01:00
|
|
|
Check.argCondition(args.length == 0,
|
|
|
|
"The syntax argument cannot be empty, consider using Command#setDefaultExecutor");
|
2020-11-04 04:45:46 +01:00
|
|
|
final CommandSyntax syntax = new CommandSyntax(commandCondition, executor, args);
|
|
|
|
this.syntaxes.add(syntax);
|
|
|
|
return syntax;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a new syntax in the command without any condition.
|
2020-07-21 18:04:02 +02:00
|
|
|
*
|
|
|
|
* @param executor the executor to call when the syntax is successfully received
|
|
|
|
* @param args all the arguments of the syntax
|
2020-10-29 22:52:07 +01:00
|
|
|
* @return the created {@link CommandSyntax}
|
2020-11-04 04:45:46 +01:00
|
|
|
* @see #addSyntax(CommandCondition, CommandExecutor, Argument[])
|
2020-07-21 18:04:02 +02:00
|
|
|
*/
|
2020-10-29 22:52:07 +01:00
|
|
|
public CommandSyntax addSyntax(@NotNull CommandExecutor executor, @NotNull Argument<?>... args) {
|
2020-11-04 04:45:46 +01:00
|
|
|
return addSyntax(null, executor, args);
|
2020-07-10 16:37:18 +02:00
|
|
|
}
|
|
|
|
|
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-10-24 16:58:27 +02:00
|
|
|
@NotNull
|
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
|
|
|
*
|
2020-10-29 22:52:07 +01:00
|
|
|
* @return the command aliases, can be null or empty
|
2020-07-21 18:04:02 +02:00
|
|
|
*/
|
2020-10-24 16:58:27 +02:00
|
|
|
@Nullable
|
2020-07-10 16:37:18 +02:00
|
|
|
public String[] getAliases() {
|
|
|
|
return aliases;
|
|
|
|
}
|
|
|
|
|
2020-07-21 18:04:02 +02:00
|
|
|
/**
|
2020-10-29 22:52:07 +01:00
|
|
|
* Gets the default {@link CommandExecutor} which is called when there is no argument
|
2020-10-15 21:16:31 +02:00
|
|
|
* or if no corresponding syntax has been found.
|
2020-07-21 18:04:02 +02:00
|
|
|
*
|
2020-10-29 22:52:07 +01:00
|
|
|
* @return the default executor, null if not any
|
2020-07-21 18:04:02 +02:00
|
|
|
*/
|
2020-10-24 16:58:27 +02:00
|
|
|
@Nullable
|
2020-07-10 16:37:18 +02:00
|
|
|
public CommandExecutor getDefaultExecutor() {
|
|
|
|
return defaultExecutor;
|
|
|
|
}
|
|
|
|
|
2020-07-21 18:04:02 +02:00
|
|
|
/**
|
2020-11-10 08:01:27 +01:00
|
|
|
* Sets the default {@link CommandExecutor}.
|
2020-07-21 18:04:02 +02:00
|
|
|
*
|
2020-10-29 22:52:07 +01:00
|
|
|
* @param executor the new default executor, null to remove it
|
2020-11-10 08:01:27 +01:00
|
|
|
* @see #getDefaultExecutor()
|
2020-07-21 18:04:02 +02:00
|
|
|
*/
|
2020-10-24 16:58:27 +02:00
|
|
|
public void setDefaultExecutor(@Nullable 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-10-24 16:58:27 +02:00
|
|
|
@NotNull
|
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
|
2020-11-22 23:04:06 +01:00
|
|
|
* when in a dynamic argument ({@link ArgumentDynamicWord} (when {@link SuggestionType#ASK_SERVER} is used)
|
|
|
|
* 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-10-17 11:29:05 +02:00
|
|
|
* @return the array containing all the suggestion for the current arg (split " "), can be null
|
2020-08-04 06:14:42 +02:00
|
|
|
*/
|
2020-10-24 16:58:27 +02:00
|
|
|
@Nullable
|
|
|
|
public String[] onDynamicWrite(@NotNull String text) {
|
2020-08-04 06:14:42 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-10-08 01:21:15 +02:00
|
|
|
/**
|
2020-10-17 11:29:05 +02:00
|
|
|
* Called when a {@link CommandSender} executes this command before any syntax callback.
|
2020-10-08 01:21:15 +02:00
|
|
|
* <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
|
|
|
|
*/
|
2020-10-24 16:58:27 +02:00
|
|
|
public void globalListener(@NotNull CommandSender sender, @NotNull Arguments arguments, @NotNull String command) {
|
2020-10-08 01:21:15 +02:00
|
|
|
}
|
|
|
|
|
2020-07-10 16:37:18 +02:00
|
|
|
}
|