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

212 lines
7.4 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;
2020-10-24 16:58:27 +02:00
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
2020-07-21 18:04:02 +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
*/
public class Command {
2020-10-11 18:35:32 +02:00
private final String name;
private final String[] aliases;
private CommandExecutor defaultExecutor;
private CommandCondition condition;
2020-10-11 18:35:32 +02:00
private final List<CommandSyntax> syntaxes;
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) {
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-10-24 16:58:27 +02:00
public Command(@NotNull 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, null if not any
2020-07-21 18:04:02 +02:00
*/
@Nullable
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, null to do not call anything
2020-07-21 18:04:02 +02:00
*/
public void setCondition(@Nullable CommandCondition commandCondition) {
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) {
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 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) {
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}
* @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) {
return addSyntax(null, executor, args);
}
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
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
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
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
*
2020-10-29 22:52:07 +01:00
* @param executor the new default executor, null to remove it
2020-07-21 18:04:02 +02:00
*/
2020-10-24 16:58:27 +02:00
public void setDefaultExecutor(@Nullable CommandExecutor executor) {
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
public Collection<CommandSyntax> getSyntaxes() {
return syntaxes;
}
/**
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-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-10-24 16:58:27 +02:00
@Nullable
public String[] onDynamicWrite(@NotNull String text) {
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
}
}