2020-07-10 16:37:18 +02:00
|
|
|
package net.minestom.server.command.builder;
|
|
|
|
|
2020-07-14 13:35:07 +02:00
|
|
|
import net.minestom.server.command.CommandSender;
|
2020-07-10 16:37:18 +02:00
|
|
|
import net.minestom.server.command.builder.arguments.Argument;
|
|
|
|
import net.minestom.server.command.builder.condition.CommandCondition;
|
2020-10-29 19:51:10 +01:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
2020-10-28 01:29:05 +01:00
|
|
|
import org.jetbrains.annotations.Nullable;
|
2020-07-10 16:37:18 +02:00
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
2020-10-15 14:36:21 +02:00
|
|
|
/**
|
|
|
|
* Class responsible for parsing {@link Command}.
|
|
|
|
*/
|
2020-07-14 13:35:07 +02:00
|
|
|
public class CommandDispatcher {
|
2020-07-10 16:37:18 +02:00
|
|
|
|
2020-10-15 14:36:21 +02:00
|
|
|
private final Map<String, Command> commandMap = new HashMap<>();
|
|
|
|
private final Set<Command> commands = new HashSet<>();
|
2020-07-10 16:37:18 +02:00
|
|
|
|
2020-11-14 01:39:51 +01:00
|
|
|
/**
|
|
|
|
* Registers a command,
|
|
|
|
* be aware that registering a command name or alias will override the previous entry.
|
|
|
|
*
|
|
|
|
* @param command the command to register
|
|
|
|
*/
|
2020-10-29 19:51:10 +01:00
|
|
|
public void register(@NotNull Command command) {
|
2020-08-04 06:14:42 +02:00
|
|
|
this.commandMap.put(command.getName().toLowerCase(), command);
|
2020-10-29 19:51:10 +01:00
|
|
|
|
|
|
|
// Register aliases
|
|
|
|
final String[] aliases = command.getAliases();
|
|
|
|
if (aliases != null) {
|
|
|
|
for (String alias : command.getAliases()) {
|
|
|
|
this.commandMap.put(alias.toLowerCase(), command);
|
|
|
|
}
|
2020-07-10 16:37:18 +02:00
|
|
|
}
|
2020-10-29 19:51:10 +01:00
|
|
|
|
2020-07-10 16:37:18 +02:00
|
|
|
this.commands.add(command);
|
|
|
|
}
|
|
|
|
|
2020-11-24 22:56:12 +01:00
|
|
|
public void unregister(@NotNull Command command) {
|
2020-11-06 16:03:08 +01:00
|
|
|
commandMap.remove(command.getName().toLowerCase());
|
2020-11-24 22:56:12 +01:00
|
|
|
for (String alias : command.getAliases()) {
|
2020-11-06 16:03:08 +01:00
|
|
|
this.commandMap.remove(alias.toLowerCase());
|
|
|
|
}
|
|
|
|
commands.remove(command);
|
|
|
|
}
|
|
|
|
|
2020-08-03 06:36:42 +02:00
|
|
|
/**
|
2020-10-29 19:51:10 +01:00
|
|
|
* Parses the given command.
|
2020-08-03 06:36:42 +02:00
|
|
|
*
|
|
|
|
* @param commandString the command (containing the command name and the args if any)
|
|
|
|
* @return the result of the parsing, null if the command doesn't exist
|
|
|
|
*/
|
2020-10-29 19:51:10 +01:00
|
|
|
public CommandResult parse(@NotNull String commandString) {
|
2020-07-10 16:37:18 +02:00
|
|
|
commandString = commandString.trim();
|
|
|
|
|
|
|
|
// Split space
|
2020-08-03 06:36:42 +02:00
|
|
|
final String spaceRegex = " ";
|
2020-10-29 19:51:10 +01:00
|
|
|
final String[] parts = commandString.split(spaceRegex);
|
|
|
|
final String commandName = parts[0];
|
2020-07-10 16:37:18 +02:00
|
|
|
|
2020-08-03 06:36:42 +02:00
|
|
|
final String[] args = commandString.replaceFirst(Pattern.quote(commandName), "").trim().split(spaceRegex);
|
2020-07-10 16:37:18 +02:00
|
|
|
|
2020-08-03 06:36:42 +02:00
|
|
|
final Command command = findCommand(commandName);
|
|
|
|
// Check if the command exists
|
2020-07-10 16:37:18 +02:00
|
|
|
if (command == null)
|
|
|
|
return null;
|
|
|
|
|
2020-08-03 06:36:42 +02:00
|
|
|
// Find the used syntax, or check which argument is wrong
|
2020-07-10 16:37:18 +02:00
|
|
|
return findCommandResult(command, args);
|
|
|
|
}
|
|
|
|
|
2020-10-29 19:51:10 +01:00
|
|
|
public void execute(@NotNull CommandSender source, @NotNull String commandString) {
|
2020-07-10 16:37:18 +02:00
|
|
|
CommandResult result = parse(commandString);
|
2020-10-08 01:21:15 +02:00
|
|
|
result.execute(source, commandString);
|
2020-07-10 16:37:18 +02:00
|
|
|
}
|
|
|
|
|
2020-10-29 19:51:10 +01:00
|
|
|
@NotNull
|
2020-07-14 13:35:07 +02:00
|
|
|
public Set<Command> getCommands() {
|
2020-07-10 16:37:18 +02:00
|
|
|
return Collections.unmodifiableSet(commands);
|
|
|
|
}
|
|
|
|
|
2020-08-04 06:14:42 +02:00
|
|
|
/**
|
2020-10-15 21:16:31 +02:00
|
|
|
* GetS the command class associated with the name;
|
2020-08-04 06:14:42 +02:00
|
|
|
*
|
|
|
|
* @param commandName the command name
|
|
|
|
* @return the {@link Command} associated with the name, null if not any
|
|
|
|
*/
|
2020-10-29 19:51:10 +01:00
|
|
|
@Nullable
|
|
|
|
public Command findCommand(@NotNull String commandName) {
|
2020-08-04 06:14:42 +02:00
|
|
|
commandName = commandName.toLowerCase();
|
2020-10-15 14:36:21 +02:00
|
|
|
return commandMap.getOrDefault(commandName, null);
|
2020-07-10 16:37:18 +02:00
|
|
|
}
|
|
|
|
|
2020-10-29 19:51:10 +01:00
|
|
|
private CommandResult findCommandResult(@NotNull Command command, @NotNull String[] args) {
|
2020-07-10 16:37:18 +02:00
|
|
|
CommandResult result = new CommandResult();
|
|
|
|
result.command = command;
|
|
|
|
|
|
|
|
Arguments executorArgs = new Arguments();
|
|
|
|
|
2020-10-29 19:51:10 +01:00
|
|
|
// The default executor should be used if no argument is provided
|
2020-07-10 16:37:18 +02:00
|
|
|
if (args[0].length() == 0) {
|
|
|
|
result.executor = command.getDefaultExecutor();
|
|
|
|
result.arguments = executorArgs;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-10-29 19:51:10 +01:00
|
|
|
// SYNTAXES PARSING
|
2020-07-10 16:37:18 +02:00
|
|
|
|
2020-10-29 19:51:10 +01:00
|
|
|
// All the registered syntaxes of the command
|
2020-08-03 06:36:42 +02:00
|
|
|
final Collection<CommandSyntax> syntaxes = command.getSyntaxes();
|
2020-10-29 19:51:10 +01:00
|
|
|
// Contains all the fully validated syntaxes (we later find the one with the most amount of arguments)
|
2020-07-10 16:37:18 +02:00
|
|
|
List<CommandSyntax> validSyntaxes = new ArrayList<>();
|
2020-10-29 19:51:10 +01:00
|
|
|
// Contains the raw string value of each argument that has been validated
|
|
|
|
// CommandSyntax - (Argument index/Raw string value)
|
2020-07-10 16:37:18 +02:00
|
|
|
Map<CommandSyntax, String[]> syntaxesValues = new HashMap<>();
|
|
|
|
|
2020-10-29 19:51:10 +01:00
|
|
|
// Contains all the syntaxes that are not fully correct, used to later, retrieve the "most correct syntax"
|
|
|
|
// Number of correct argument - The data about the failing argument
|
2020-07-10 16:37:18 +02:00
|
|
|
TreeMap<Integer, CommandSuggestionHolder> syntaxesSuggestions = new TreeMap<>(Collections.reverseOrder());
|
|
|
|
|
|
|
|
for (CommandSyntax syntax : syntaxes) {
|
2020-10-29 22:52:07 +01:00
|
|
|
final Argument<?>[] arguments = syntax.getArguments();
|
2020-08-03 06:36:42 +02:00
|
|
|
final String[] argsValues = new String[arguments.length];
|
2020-07-10 16:37:18 +02:00
|
|
|
|
|
|
|
boolean syntaxCorrect = true;
|
2020-11-03 23:31:26 +01:00
|
|
|
// The current index in the raw command string arguments
|
2020-07-10 16:37:18 +02:00
|
|
|
int argIndex = 0;
|
|
|
|
|
|
|
|
boolean useRemaining = false;
|
2020-10-29 19:51:10 +01:00
|
|
|
// Check the validity of the arguments...
|
2020-07-10 16:37:18 +02:00
|
|
|
for (int argCount = 0; argCount < syntax.getArguments().length; argCount++) {
|
2020-10-29 22:52:07 +01:00
|
|
|
final Argument<?> argument = syntax.getArguments()[argCount];
|
2020-07-10 16:37:18 +02:00
|
|
|
useRemaining = argument.useRemaining();
|
|
|
|
|
2020-08-03 10:35:46 +02:00
|
|
|
// the correction result of the argument
|
2020-07-10 16:37:18 +02:00
|
|
|
int correctionResult = Argument.SUCCESS;
|
2020-08-03 10:35:46 +02:00
|
|
|
// true if the arg is valid, false otherwise
|
2020-07-10 16:37:18 +02:00
|
|
|
boolean correct = false;
|
2020-08-03 10:35:46 +02:00
|
|
|
// the raw string representing the correct argument syntax
|
2020-08-19 01:24:51 +02:00
|
|
|
StringBuilder argValue = new StringBuilder();
|
2020-07-10 16:37:18 +02:00
|
|
|
|
|
|
|
if (useRemaining) {
|
2020-11-03 23:31:26 +01:00
|
|
|
final boolean hasArgs = args.length > argIndex;
|
|
|
|
// Verify if there is any string part available
|
|
|
|
if (hasArgs) {
|
|
|
|
// Argument is supposed to take the rest of the command input
|
|
|
|
for (int i = argIndex; i < args.length; i++) {
|
|
|
|
final String arg = args[i];
|
|
|
|
if (argValue.length() > 0)
|
|
|
|
argValue.append(" ");
|
|
|
|
argValue.append(arg);
|
|
|
|
}
|
2020-07-10 16:37:18 +02:00
|
|
|
|
2020-11-03 23:31:26 +01:00
|
|
|
final String argValueString = argValue.toString();
|
2020-08-19 01:24:51 +02:00
|
|
|
|
2020-11-03 23:31:26 +01:00
|
|
|
correctionResult = argument.getCorrectionResult(argValueString);
|
|
|
|
if (correctionResult == Argument.SUCCESS) {
|
|
|
|
correct = true;
|
|
|
|
argsValues[argIndex] = argValueString;
|
|
|
|
}
|
2020-07-10 16:37:18 +02:00
|
|
|
}
|
|
|
|
} else {
|
2020-10-29 19:51:10 +01:00
|
|
|
// Argument is either single-word or can accept optional delimited space(s)
|
2020-07-10 16:37:18 +02:00
|
|
|
for (int i = argIndex; i < args.length; i++) {
|
2020-08-03 06:36:42 +02:00
|
|
|
final String arg = args[i];
|
2020-07-10 16:37:18 +02:00
|
|
|
|
2020-08-19 01:24:51 +02:00
|
|
|
argValue.append(arg);
|
|
|
|
|
|
|
|
final String argValueString = argValue.toString();
|
2020-07-10 16:37:18 +02:00
|
|
|
|
2020-08-19 01:24:51 +02:00
|
|
|
correctionResult = argument.getCorrectionResult(argValueString);
|
2020-07-10 16:37:18 +02:00
|
|
|
if (correctionResult == Argument.SUCCESS) {
|
|
|
|
correct = true;
|
2020-08-19 01:24:51 +02:00
|
|
|
argsValues[argIndex] = argValueString;
|
2020-07-10 16:37:18 +02:00
|
|
|
argIndex = i + 1;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if (!argument.allowSpace())
|
|
|
|
break;
|
2020-08-19 01:24:51 +02:00
|
|
|
argValue.append(" ");
|
2020-07-10 16:37:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-29 19:51:10 +01:00
|
|
|
if (!correct) {
|
2020-10-15 14:36:21 +02:00
|
|
|
// Argument is not correct, add it to the syntax suggestion with the number
|
|
|
|
// of correct argument(s) and do not check the next syntax argument
|
2020-07-10 16:37:18 +02:00
|
|
|
syntaxCorrect = false;
|
|
|
|
CommandSuggestionHolder suggestionHolder = new CommandSuggestionHolder();
|
|
|
|
suggestionHolder.syntax = syntax;
|
2020-08-19 01:24:51 +02:00
|
|
|
suggestionHolder.argValue = argValue.toString();
|
2020-07-10 16:37:18 +02:00
|
|
|
suggestionHolder.correctionResult = correctionResult;
|
|
|
|
suggestionHolder.argIndex = argCount;
|
|
|
|
syntaxesSuggestions.put(argCount, suggestionHolder);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-10-15 14:36:21 +02:00
|
|
|
|
|
|
|
// Add the syntax to the list of valid syntaxes if correct
|
2020-07-10 16:37:18 +02:00
|
|
|
if (syntaxCorrect) {
|
|
|
|
if (args.length == argIndex || useRemaining) {
|
|
|
|
validSyntaxes.add(syntax);
|
|
|
|
syntaxesValues.put(syntax, argsValues);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-29 19:51:10 +01:00
|
|
|
// Check if there is at least one correct syntax
|
2020-10-28 01:02:12 +01:00
|
|
|
if (!validSyntaxes.isEmpty()) {
|
|
|
|
// Search the syntax with all perfect args
|
2020-10-29 19:51:10 +01:00
|
|
|
final CommandSyntax finalSyntax = findMostCorrectSyntax(validSyntaxes, syntaxesValues, executorArgs);
|
2020-10-28 01:29:05 +01:00
|
|
|
if (finalSyntax != null) {
|
2020-10-29 19:51:10 +01:00
|
|
|
// A fully correct syntax has been found, use it
|
2020-11-04 04:45:46 +01:00
|
|
|
result.syntax = finalSyntax;
|
2020-10-28 01:29:05 +01:00
|
|
|
result.executor = finalSyntax.getExecutor();
|
|
|
|
result.arguments = executorArgs;
|
|
|
|
return result;
|
2020-10-28 01:02:12 +01:00
|
|
|
}
|
2020-07-10 16:37:18 +02:00
|
|
|
|
2020-10-29 19:51:10 +01:00
|
|
|
// Otherwise, search for the first syntax with an incorrect argument
|
2020-10-28 01:02:12 +01:00
|
|
|
for (CommandSyntax syntax : validSyntaxes) {
|
2020-10-29 22:52:07 +01:00
|
|
|
final Argument[] arguments = syntax.getArguments();
|
2020-10-28 01:02:12 +01:00
|
|
|
final String[] argsValues = syntaxesValues.get(syntax);
|
|
|
|
for (int i = 0; i < arguments.length; i++) {
|
2020-10-29 22:52:07 +01:00
|
|
|
final Argument argument = arguments[i];
|
2020-10-28 01:02:12 +01:00
|
|
|
final String argValue = argsValues[i];
|
|
|
|
// Finally parse it
|
|
|
|
final Object parsedValue = argument.parse(argValue);
|
|
|
|
final int conditionResult = argument.getConditionResult(parsedValue);
|
|
|
|
if (conditionResult != Argument.SUCCESS) {
|
2020-10-29 19:51:10 +01:00
|
|
|
// Condition of an argument not correct, use the argument callback if any
|
|
|
|
if (argument.hasErrorCallback()) {
|
|
|
|
result.callback = argument.getCallback();
|
|
|
|
result.value = argValue;
|
|
|
|
result.error = conditionResult;
|
2020-10-28 01:02:12 +01:00
|
|
|
|
2020-10-29 19:51:10 +01:00
|
|
|
return result;
|
|
|
|
}
|
2020-10-28 01:02:12 +01:00
|
|
|
}
|
2020-07-10 16:37:18 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-28 01:02:12 +01:00
|
|
|
|
2020-07-10 16:37:18 +02:00
|
|
|
}
|
|
|
|
|
2020-10-29 19:51:10 +01:00
|
|
|
// No all-correct syntax, find the closest one to use the argument callback
|
2020-10-28 01:02:12 +01:00
|
|
|
{
|
2020-07-10 16:37:18 +02:00
|
|
|
// Get closest valid syntax
|
|
|
|
if (!syntaxesSuggestions.isEmpty()) {
|
2020-10-15 14:36:21 +02:00
|
|
|
final int max = syntaxesSuggestions.firstKey(); // number of correct arguments
|
2020-10-17 08:18:52 +02:00
|
|
|
// Check if at least 1 argument of the syntax is correct
|
|
|
|
if (max > 0) {
|
|
|
|
// Get the data of the closest syntax
|
|
|
|
final CommandSuggestionHolder suggestionHolder = syntaxesSuggestions.get(max);
|
|
|
|
final CommandSyntax syntax = suggestionHolder.syntax;
|
|
|
|
final String argValue = suggestionHolder.argValue;
|
|
|
|
final int correctionResult = suggestionHolder.correctionResult;
|
|
|
|
final int argIndex = suggestionHolder.argIndex;
|
2020-07-10 16:37:18 +02:00
|
|
|
|
2020-10-15 14:36:21 +02:00
|
|
|
// Found the closest syntax with at least 1 correct argument
|
2020-10-29 22:52:07 +01:00
|
|
|
final Argument<?> argument = syntax.getArguments()[argIndex];
|
2020-10-29 19:51:10 +01:00
|
|
|
if (argument.hasErrorCallback()) {
|
|
|
|
result.callback = argument.getCallback();
|
|
|
|
result.value = argValue;
|
|
|
|
result.error = correctionResult;
|
2020-07-10 16:37:18 +02:00
|
|
|
|
2020-10-29 19:51:10 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2020-07-10 16:37:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-29 19:51:10 +01:00
|
|
|
// Use the default executor at last resort
|
2020-10-28 01:02:12 +01:00
|
|
|
result.executor = command.getDefaultExecutor();
|
2020-07-10 16:37:18 +02:00
|
|
|
result.arguments = executorArgs;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-10-29 19:51:10 +01:00
|
|
|
/**
|
|
|
|
* Retrieves from the valid syntax map the arguments condition result and get the one with the most
|
|
|
|
* valid arguments.
|
|
|
|
*
|
|
|
|
* @param validSyntaxes the list containing all the valid syntaxes
|
|
|
|
* @param syntaxesValues the map containing the argument raw string values
|
|
|
|
* @param executorArgs the recipient of the argument parsed values
|
|
|
|
* @return the command syntax with all of its arguments correct and with the most arguments count, null if not any
|
|
|
|
*/
|
2020-10-28 01:29:05 +01:00
|
|
|
@Nullable
|
2020-10-29 19:51:10 +01:00
|
|
|
private CommandSyntax findMostCorrectSyntax(@NotNull List<CommandSyntax> validSyntaxes,
|
|
|
|
@NotNull Map<CommandSyntax, String[]> syntaxesValues,
|
|
|
|
@NotNull Arguments executorArgs) {
|
2020-10-28 01:55:44 +01:00
|
|
|
Map<CommandSyntax, Arguments> argumentsValueMap = new HashMap<>();
|
|
|
|
|
2020-10-28 01:29:05 +01:00
|
|
|
CommandSyntax finalSyntax = null;
|
|
|
|
int maxArguments = 0;
|
|
|
|
for (CommandSyntax syntax : validSyntaxes) {
|
2020-10-28 01:55:44 +01:00
|
|
|
Arguments syntaxValues = new Arguments();
|
2020-10-28 01:29:05 +01:00
|
|
|
boolean fullyCorrect = true;
|
|
|
|
|
2020-10-29 22:52:07 +01:00
|
|
|
final Argument<?>[] arguments = syntax.getArguments();
|
2020-10-28 01:29:05 +01:00
|
|
|
final String[] argsValues = syntaxesValues.get(syntax);
|
|
|
|
for (int i = 0; i < arguments.length; i++) {
|
2020-10-29 22:52:07 +01:00
|
|
|
final Argument argument = arguments[i];
|
2020-10-28 01:29:05 +01:00
|
|
|
final String argValue = argsValues[i];
|
|
|
|
// Finally parse it
|
|
|
|
final Object parsedValue = argument.parse(argValue);
|
|
|
|
final int conditionResult = argument.getConditionResult(parsedValue);
|
|
|
|
if (conditionResult == Argument.SUCCESS) {
|
2020-10-28 01:55:44 +01:00
|
|
|
syntaxValues.setArg(argument.getId(), parsedValue);
|
2020-10-28 01:29:05 +01:00
|
|
|
} else {
|
2020-10-29 19:51:10 +01:00
|
|
|
// One argument is incorrect, stop the whole syntax check
|
2020-10-28 01:29:05 +01:00
|
|
|
fullyCorrect = false;
|
2020-10-29 19:20:25 +01:00
|
|
|
break;
|
2020-10-28 01:29:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final int argumentLength = arguments.length;
|
|
|
|
if (fullyCorrect && argumentLength > maxArguments) {
|
|
|
|
finalSyntax = syntax;
|
|
|
|
maxArguments = argumentLength;
|
2020-10-28 01:55:44 +01:00
|
|
|
argumentsValueMap.put(syntax, syntaxValues);
|
2020-10-28 01:29:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 01:55:44 +01:00
|
|
|
// Get the arguments values
|
|
|
|
if (finalSyntax != null) {
|
|
|
|
executorArgs.copy(argumentsValueMap.get(finalSyntax));
|
|
|
|
}
|
|
|
|
|
2020-10-28 01:29:05 +01:00
|
|
|
return finalSyntax;
|
|
|
|
}
|
|
|
|
|
2020-08-19 01:24:51 +02:00
|
|
|
private static class CommandSuggestionHolder {
|
2020-07-10 16:37:18 +02:00
|
|
|
private CommandSyntax syntax;
|
|
|
|
private String argValue;
|
|
|
|
private int correctionResult;
|
|
|
|
private int argIndex;
|
|
|
|
}
|
|
|
|
|
2020-10-08 01:28:40 +02:00
|
|
|
/**
|
2020-10-15 14:36:21 +02:00
|
|
|
* Represents a {@link Command} ready to be executed (already parsed).
|
2020-10-08 01:28:40 +02:00
|
|
|
*/
|
2020-08-19 01:24:51 +02:00
|
|
|
private static class CommandResult {
|
2020-10-08 01:28:40 +02:00
|
|
|
|
2020-07-10 16:37:18 +02:00
|
|
|
// Command
|
2020-07-14 13:35:07 +02:00
|
|
|
private Command command;
|
2020-07-10 16:37:18 +02:00
|
|
|
|
|
|
|
// Command Executor
|
2020-11-04 04:45:46 +01:00
|
|
|
private CommandSyntax syntax;
|
|
|
|
|
2020-07-10 16:37:18 +02:00
|
|
|
private CommandExecutor executor;
|
|
|
|
private Arguments arguments;
|
|
|
|
|
|
|
|
// Argument Callback
|
|
|
|
private ArgumentCallback callback;
|
|
|
|
private String value;
|
|
|
|
private int error;
|
|
|
|
|
2020-08-03 06:36:42 +02:00
|
|
|
/**
|
2020-10-29 19:51:10 +01:00
|
|
|
* Executes the command for the given source.
|
2020-08-03 06:36:42 +02:00
|
|
|
* <p>
|
2020-10-15 14:36:21 +02:00
|
|
|
* The command will not be executed if {@link Command#getCondition()}
|
|
|
|
* is not validated.
|
2020-08-03 06:36:42 +02:00
|
|
|
*
|
2020-10-08 01:21:15 +02:00
|
|
|
* @param source the command source
|
|
|
|
* @param commandString the command string
|
2020-08-03 06:36:42 +02:00
|
|
|
*/
|
2020-10-29 19:51:10 +01:00
|
|
|
public void execute(@NotNull CommandSender source, @NotNull String commandString) {
|
2020-10-08 01:21:15 +02:00
|
|
|
// Global listener
|
|
|
|
command.globalListener(source, arguments, commandString);
|
2020-11-04 04:45:46 +01:00
|
|
|
// Command condition check
|
2020-08-03 06:36:42 +02:00
|
|
|
final CommandCondition condition = command.getCondition();
|
2020-07-10 16:37:18 +02:00
|
|
|
if (condition != null) {
|
2020-11-04 04:45:46 +01:00
|
|
|
final boolean result = condition.canUse(source, commandString);
|
2020-07-10 16:37:18 +02:00
|
|
|
if (!result)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Condition is respected
|
|
|
|
if (executor != null) {
|
2020-08-03 06:36:42 +02:00
|
|
|
// An executor has been found
|
2020-11-04 04:45:46 +01:00
|
|
|
|
|
|
|
if (syntax != null) {
|
|
|
|
// The executor is from a syntax
|
|
|
|
final CommandCondition commandCondition = syntax.getCommandCondition();
|
|
|
|
if (commandCondition == null || commandCondition.canUse(source, commandString)) {
|
|
|
|
executor.apply(source, arguments);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The executor is probably the default one
|
|
|
|
executor.apply(source, arguments);
|
|
|
|
}
|
2020-07-10 16:37:18 +02:00
|
|
|
} else if (callback != null) {
|
2020-10-08 01:28:40 +02:00
|
|
|
// No syntax has been validated but the faulty argument with a callback has been found
|
2020-08-03 06:36:42 +02:00
|
|
|
// Execute the faulty argument callback
|
2020-07-10 16:37:18 +02:00
|
|
|
callback.apply(source, value, error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|