mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-04 23:47:59 +01:00
Cleanup basic command parsing
This commit is contained in:
parent
7d752cc323
commit
987e355a70
@ -99,22 +99,17 @@ public final class CommandManager {
|
||||
* @return the execution result
|
||||
*/
|
||||
public @NotNull CommandResult execute(@NotNull CommandSender sender, @NotNull String command) {
|
||||
|
||||
// Command event
|
||||
if (sender instanceof Player) {
|
||||
Player player = (Player) sender;
|
||||
|
||||
final Player player = (Player) sender;
|
||||
PlayerCommandEvent playerCommandEvent = new PlayerCommandEvent(player, command);
|
||||
EventDispatcher.call(playerCommandEvent);
|
||||
|
||||
if (playerCommandEvent.isCancelled())
|
||||
return CommandResult.of(CommandResult.Type.CANCELLED, command);
|
||||
|
||||
command = playerCommandEvent.getCommand();
|
||||
}
|
||||
|
||||
// Process the command
|
||||
final var result = dispatcher.execute(sender, command);
|
||||
final CommandResult result = dispatcher.execute(sender, command);
|
||||
if (result.getType() == CommandResult.Type.UNKNOWN) {
|
||||
if (unknownCommandCallback != null) {
|
||||
this.unknownCommandCallback.apply(sender, command);
|
||||
|
@ -65,8 +65,7 @@ public class CommandDispatcher {
|
||||
this.cache.invalidateAll();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Set<Command> getCommands() {
|
||||
public @NotNull Set<Command> getCommands() {
|
||||
return Collections.unmodifiableSet(commands);
|
||||
}
|
||||
|
||||
@ -76,8 +75,7 @@ public class CommandDispatcher {
|
||||
* @param commandName the command name
|
||||
* @return the {@link Command} associated with the name, null if not any
|
||||
*/
|
||||
@Nullable
|
||||
public Command findCommand(@NotNull String commandName) {
|
||||
public @Nullable Command findCommand(@NotNull String commandName) {
|
||||
commandName = commandName.toLowerCase();
|
||||
return commandMap.getOrDefault(commandName, null);
|
||||
}
|
||||
@ -89,8 +87,7 @@ public class CommandDispatcher {
|
||||
* @param commandString the command with the argument(s)
|
||||
* @return the command result
|
||||
*/
|
||||
@NotNull
|
||||
public CommandResult execute(@NotNull CommandSender source, @NotNull String commandString) {
|
||||
public @NotNull CommandResult execute(@NotNull CommandSender source, @NotNull String commandString) {
|
||||
CommandResult commandResult = parse(commandString);
|
||||
ParsedCommand parsedCommand = commandResult.parsedCommand;
|
||||
if (parsedCommand != null) {
|
||||
@ -105,10 +102,8 @@ public class CommandDispatcher {
|
||||
* @param commandString the command (containing the command name and the args if any)
|
||||
* @return the parsing result
|
||||
*/
|
||||
@NotNull
|
||||
public CommandResult parse(@NotNull String commandString) {
|
||||
public @NotNull CommandResult parse(@NotNull String commandString) {
|
||||
commandString = commandString.trim();
|
||||
|
||||
// Verify if the result is cached
|
||||
{
|
||||
final CommandResult cachedResult = cache.getIfPresent(commandString);
|
||||
@ -134,15 +129,12 @@ public class CommandDispatcher {
|
||||
findParsedCommand(command, commandName, commandQueryResult.args, commandString, result);
|
||||
|
||||
// Cache result
|
||||
{
|
||||
this.cache.put(commandString, result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private ParsedCommand findParsedCommand(@NotNull Command command,
|
||||
private @Nullable ParsedCommand findParsedCommand(@NotNull Command command,
|
||||
@NotNull String commandName, @NotNull String[] args,
|
||||
@NotNull String commandString,
|
||||
@NotNull CommandResult result) {
|
||||
@ -162,13 +154,11 @@ public class CommandDispatcher {
|
||||
|
||||
final String input = commandName + StringUtils.SPACE + String.join(StringUtils.SPACE, args);
|
||||
|
||||
|
||||
ParsedCommand parsedCommand = new ParsedCommand();
|
||||
parsedCommand.command = command;
|
||||
parsedCommand.commandString = commandString;
|
||||
|
||||
// The default executor should be used if no argument is provided
|
||||
{
|
||||
if (!hasArgument) {
|
||||
Optional<CommandSyntax> optionalSyntax = command.getSyntaxes()
|
||||
.stream()
|
||||
@ -178,7 +168,6 @@ public class CommandDispatcher {
|
||||
if (optionalSyntax.isPresent()) {
|
||||
// Empty syntax found
|
||||
final CommandSyntax syntax = optionalSyntax.get();
|
||||
|
||||
parsedCommand.syntax = syntax;
|
||||
parsedCommand.executor = syntax.getExecutor();
|
||||
parsedCommand.context = new CommandContext(input);
|
||||
@ -199,7 +188,6 @@ public class CommandDispatcher {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SYNTAXES PARSING
|
||||
|
||||
@ -207,7 +195,6 @@ public class CommandDispatcher {
|
||||
final Collection<CommandSyntax> syntaxes = command.getSyntaxes();
|
||||
// Contains all the fully validated syntaxes (we later find the one with the most amount of arguments)
|
||||
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
|
||||
Int2ObjectRBTreeMap<CommandSuggestionHolder> syntaxesSuggestions = new Int2ObjectRBTreeMap<>(Collections.reverseOrder());
|
||||
@ -233,12 +220,9 @@ public class CommandDispatcher {
|
||||
result.parsedCommand = parsedCommand;
|
||||
return parsedCommand;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// No all-correct syntax, find the closest one to use the argument callback
|
||||
{
|
||||
// Get closest valid syntax
|
||||
if (!syntaxesSuggestions.isEmpty()) {
|
||||
final int max = syntaxesSuggestions.firstIntKey(); // number of correct arguments in the most correct syntax
|
||||
final CommandSuggestionHolder suggestionHolder = syntaxesSuggestions.get(max);
|
||||
@ -248,7 +232,7 @@ public class CommandDispatcher {
|
||||
|
||||
// Found the closest syntax with at least 1 correct argument
|
||||
final Argument<?> argument = syntax.getArguments()[argIndex];
|
||||
if (argument.hasErrorCallback()) {
|
||||
if (argument.hasErrorCallback() && argumentSyntaxException != null) {
|
||||
parsedCommand.callback = argument.getCallback();
|
||||
parsedCommand.argumentSyntaxException = argumentSyntaxException;
|
||||
|
||||
@ -257,7 +241,6 @@ public class CommandDispatcher {
|
||||
return parsedCommand;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// No syntax found
|
||||
result.type = CommandResult.Type.INVALID_SYNTAX;
|
||||
|
Loading…
Reference in New Issue
Block a user