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

240 lines
9.7 KiB
Java
Raw Normal View History

package net.minestom.server.command.builder;
2021-06-20 22:32:06 +02:00
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
2021-01-15 22:04:57 +01:00
import it.unimi.dsi.fastutil.ints.Int2ObjectRBTreeMap;
import net.minestom.server.command.CommandSender;
import net.minestom.server.command.builder.arguments.Argument;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
import net.minestom.server.command.builder.parser.CommandParser;
import net.minestom.server.command.builder.parser.CommandQueryResult;
import net.minestom.server.command.builder.parser.CommandSuggestionHolder;
import net.minestom.server.command.builder.parser.ValidSyntaxHolder;
2021-05-15 08:31:24 +02:00
import net.minestom.server.utils.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
import java.util.concurrent.TimeUnit;
2020-10-15 14:36:21 +02:00
/**
* Class responsible for parsing {@link Command}.
*/
public class CommandDispatcher {
2020-10-15 14:36:21 +02:00
private final Map<String, Command> commandMap = new HashMap<>();
2021-02-10 00:24:23 +01:00
private final Set<Command> commands = new HashSet<>();
2021-06-20 22:32:06 +02:00
private final Cache<String, CommandResult> cache = Caffeine.newBuilder()
.expireAfterWrite(30, TimeUnit.SECONDS)
.build();
/**
* Registers a command,
* be aware that registering a command name or alias will override the previous entry.
*
* @param command the command to register
*/
public void register(@NotNull Command command) {
this.commandMap.put(command.getName().toLowerCase(), command);
// Register aliases
final String[] aliases = command.getAliases();
if (aliases != null) {
2021-02-09 18:24:50 +01:00
for (String alias : aliases) {
this.commandMap.put(alias.toLowerCase(), command);
}
}
2021-02-10 00:24:23 +01:00
this.commands.add(command);
}
public void unregister(@NotNull Command command) {
2020-12-12 06:13:50 +01:00
this.commandMap.remove(command.getName().toLowerCase());
final String[] aliases = command.getAliases();
if (aliases != null) {
for (String alias : aliases) {
this.commandMap.remove(alias.toLowerCase());
}
}
2021-02-10 00:24:23 +01:00
this.commands.remove(command);
// Clear cache
this.cache.invalidateAll();
}
2021-08-19 09:06:24 +02:00
public @NotNull Set<Command> getCommands() {
2021-02-13 00:34:25 +01:00
return Collections.unmodifiableSet(commands);
}
/**
* Gets the command class associated with the name.
*
* @param commandName the command name
* @return the {@link Command} associated with the name, null if not any
*/
2021-08-19 09:06:24 +02:00
public @Nullable Command findCommand(@NotNull String commandName) {
2021-02-13 00:34:25 +01:00
commandName = commandName.toLowerCase();
return commandMap.getOrDefault(commandName, null);
}
/**
* Checks if the command exists, and execute it.
*
* @param source the command source
* @param commandString the command with the argument(s)
* @return the command result
*/
2021-08-19 09:06:24 +02:00
public @NotNull CommandResult execute(@NotNull CommandSender source, @NotNull String commandString) {
CommandResult commandResult = parse(commandString);
ParsedCommand parsedCommand = commandResult.parsedCommand;
if (parsedCommand != null) {
commandResult.commandData = parsedCommand.execute(source);
}
return commandResult;
}
2020-08-03 06:36:42 +02: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 parsing result
2020-08-03 06:36:42 +02:00
*/
2021-08-19 09:06:24 +02:00
public @NotNull CommandResult parse(@NotNull String commandString) {
commandString = commandString.trim();
// Verify if the result is cached
{
final CommandResult cachedResult = cache.getIfPresent(commandString);
if (cachedResult != null) {
return cachedResult;
}
}
// Split space
final String[] parts = commandString.split(StringUtils.SPACE);
final String commandName = parts[0];
2022-01-23 23:30:57 +01:00
final CommandQueryResult commandQueryResult = CommandParser.findCommand(this, commandString);
2020-08-03 06:36:42 +02:00
// Check if the command exists
if (commandQueryResult == null) {
return CommandResult.of(CommandResult.Type.UNKNOWN, commandName);
}
CommandResult result = new CommandResult();
result.input = commandString;
// Find the used syntax and fill CommandResult#type and CommandResult#parsedCommand
2022-01-23 23:30:57 +01:00
findParsedCommand( commandQueryResult, commandName, commandString, result);
// Cache result
2021-08-19 09:06:24 +02:00
this.cache.put(commandString, result);
return result;
}
2022-01-23 23:30:57 +01:00
private @NotNull ParsedCommand findParsedCommand(@NotNull CommandQueryResult commandQueryResult,
@NotNull String commandName,
2021-08-19 09:06:24 +02:00
@NotNull String commandString,
@NotNull CommandResult result) {
2022-01-23 23:30:57 +01:00
final Command command = commandQueryResult.command();
String[] args = commandQueryResult.args();
final boolean hasArgument = args.length > 0;
2021-03-10 06:38:51 +01:00
final String input = commandName + StringUtils.SPACE + String.join(StringUtils.SPACE, args);
ParsedCommand parsedCommand = new ParsedCommand();
2022-01-23 23:30:57 +01:00
parsedCommand.parents = commandQueryResult.parents();
parsedCommand.command = command;
parsedCommand.commandString = commandString;
// The default executor should be used if no argument is provided
2021-08-19 09:06:24 +02:00
if (!hasArgument) {
Optional<CommandSyntax> optionalSyntax = command.getSyntaxes()
.stream()
.filter(syntax -> syntax.getArguments().length == 0)
.findFirst();
if (optionalSyntax.isPresent()) {
// Empty syntax found
final CommandSyntax syntax = optionalSyntax.get();
parsedCommand.syntax = syntax;
parsedCommand.executor = syntax.getExecutor();
parsedCommand.context = new CommandContext(input);
result.type = CommandResult.Type.SUCCESS;
result.parsedCommand = parsedCommand;
return parsedCommand;
} else {
// No empty syntax, use default executor if any
final CommandExecutor defaultExecutor = command.getDefaultExecutor();
if (defaultExecutor != null) {
parsedCommand.executor = defaultExecutor;
2021-03-12 22:10:03 +01:00
parsedCommand.context = new CommandContext(input);
result.type = CommandResult.Type.SUCCESS;
result.parsedCommand = parsedCommand;
return parsedCommand;
}
}
}
// SYNTAXES PARSING
// All the registered syntaxes of the command
2020-08-03 06:36:42 +02:00
final Collection<CommandSyntax> syntaxes = command.getSyntaxes();
// Contains all the fully validated syntaxes (we later find the one with the most amount of arguments)
2021-02-10 00:24:23 +01:00
List<ValidSyntaxHolder> validSyntaxes = new ArrayList<>(syntaxes.size());
// 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
2021-01-15 22:04:57 +01:00
Int2ObjectRBTreeMap<CommandSuggestionHolder> syntaxesSuggestions = new Int2ObjectRBTreeMap<>(Collections.reverseOrder());
for (CommandSyntax syntax : syntaxes) {
CommandParser.parse(syntax, syntax.getArguments(), args, commandString, validSyntaxes, syntaxesSuggestions);
}
// Check if there is at least one correct syntax
if (!validSyntaxes.isEmpty()) {
2021-03-10 06:38:51 +01:00
CommandContext context = new CommandContext(input);
// Search the syntax with all perfect args
2021-03-10 06:38:51 +01:00
final ValidSyntaxHolder finalValidSyntax = CommandParser.findMostCorrectSyntax(validSyntaxes, context);
2021-02-11 04:47:48 +01:00
if (finalValidSyntax != null) {
// A fully correct syntax has been found, use it
2022-01-23 23:30:57 +01:00
final CommandSyntax syntax = finalValidSyntax.syntax();
2021-02-11 04:47:48 +01:00
parsedCommand.syntax = syntax;
parsedCommand.executor = syntax.getExecutor();
2021-03-10 06:38:51 +01:00
parsedCommand.context = context;
result.type = CommandResult.Type.SUCCESS;
result.parsedCommand = parsedCommand;
return parsedCommand;
}
}
// No all-correct syntax, find the closest one to use the argument callback
2021-08-19 09:06:24 +02:00
if (!syntaxesSuggestions.isEmpty()) {
final int max = syntaxesSuggestions.firstIntKey(); // number of correct arguments in the most correct syntax
final CommandSuggestionHolder suggestionHolder = syntaxesSuggestions.get(max);
2022-01-23 23:30:57 +01:00
final CommandSyntax syntax = suggestionHolder.syntax();
final ArgumentSyntaxException argumentSyntaxException = suggestionHolder.argumentSyntaxException();
final int argIndex = suggestionHolder.argIndex();
2021-08-19 09:06:24 +02:00
// Found the closest syntax with at least 1 correct argument
final Argument<?> argument = syntax.getArguments()[argIndex];
if (argument.hasErrorCallback() && argumentSyntaxException != null) {
parsedCommand.callback = argument.getCallback();
parsedCommand.argumentSyntaxException = argumentSyntaxException;
result.type = CommandResult.Type.INVALID_SYNTAX;
result.parsedCommand = parsedCommand;
return parsedCommand;
}
}
// No syntax found
result.type = CommandResult.Type.INVALID_SYNTAX;
2021-03-10 06:38:51 +01:00
result.parsedCommand = ParsedCommand.withDefaultExecutor(command, input);
return result.parsedCommand;
}
}