Added ArgumentGroup parsing, WIP nodes

This commit is contained in:
themode 2021-02-11 04:47:48 +01:00
parent e9d91fd21e
commit b34b490504
4 changed files with 108 additions and 55 deletions

View File

@ -272,11 +272,16 @@ public final class Arguments {
this.returnData = returnData;
}
protected void setArg(@NotNull String id, Object value) {
@NotNull
public Map<String, Object> getMap() {
return args;
}
public void setArg(@NotNull String id, Object value) {
this.args.put(id, value);
}
protected void copy(@NotNull Arguments arguments) {
public void copy(@NotNull Arguments arguments) {
this.args = arguments.args;
}

View File

@ -162,11 +162,13 @@ public class CommandDispatcher {
if (!validSyntaxes.isEmpty()) {
Arguments executorArgs = new Arguments();
// Search the syntax with all perfect args
final CommandSyntax finalSyntax = findMostCorrectSyntax(validSyntaxes, executorArgs);
if (finalSyntax != null) {
final ValidSyntaxHolder finalValidSyntax = CommandParser.findMostCorrectSyntax(validSyntaxes, executorArgs);
if (finalValidSyntax != null) {
// A fully correct syntax has been found, use it
parsedCommand.syntax = finalSyntax;
parsedCommand.executor = finalSyntax.getExecutor();
final CommandSyntax syntax = finalValidSyntax.syntax;
parsedCommand.syntax = syntax;
parsedCommand.executor = syntax.getExecutor();
parsedCommand.arguments = executorArgs;
return parsedCommand;
}
@ -197,52 +199,4 @@ public class CommandDispatcher {
// No syntax found
return null;
}
/**
* 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 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
*/
@Nullable
private CommandSyntax findMostCorrectSyntax(@NotNull List<ValidSyntaxHolder> validSyntaxes,
@NotNull Arguments executorArgs) {
CommandSyntax finalSyntax = null;
int maxArguments = 0;
Arguments finalArguments = null;
for (ValidSyntaxHolder validSyntaxHolder : validSyntaxes) {
final CommandSyntax syntax = validSyntaxHolder.syntax;
final Argument<?>[] arguments = syntax.getArguments();
final int argumentsCount = arguments.length;
final Map<Argument<?>, Object> argsValues = validSyntaxHolder.argumentsValue;
final int argsSize = argsValues.size();
if (argsSize > maxArguments) {
finalSyntax = syntax;
maxArguments = argsSize;
// Fill arguments map
Arguments syntaxValues = new Arguments();
for (Map.Entry<Argument<?>, Object> entry : argsValues.entrySet()) {
final Argument<?> argument = entry.getKey();
final Object argumentValue = entry.getValue();
syntaxValues.setArg(argument.getId(), argumentValue);
}
finalArguments = syntaxValues;
}
}
// Get the arguments values
if (finalSyntax != null) {
executorArgs.copy(finalArguments);
}
return finalSyntax;
}
}

View File

@ -0,0 +1,44 @@
package net.minestom.server.command.builder.arguments;
import net.minestom.server.command.builder.Arguments;
import net.minestom.server.command.builder.NodeMaker;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
import net.minestom.server.command.builder.parser.CommandParser;
import net.minestom.server.command.builder.parser.ValidSyntaxHolder;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
public class ArgumentGroup extends Argument<Arguments> {
public static final int INVALID_ARGUMENTS_ERROR = 1;
private final Argument<?>[] group;
public ArgumentGroup(@NotNull String id, @NotNull Argument<?>... group) {
super(id, true, false);
this.group = group;
}
@NotNull
@Override
public Arguments parse(@NotNull String input) throws ArgumentSyntaxException {
List<ValidSyntaxHolder> validSyntaxes = new ArrayList<>();
CommandParser.parse(null, group, input.split(StringUtils.SPACE), validSyntaxes, null);
Arguments arguments = new Arguments();
CommandParser.findMostCorrectSyntax(validSyntaxes, arguments);
if (validSyntaxes.isEmpty()) {
throw new ArgumentSyntaxException("Invalid arguments", input, INVALID_ARGUMENTS_ERROR);
}
return arguments;
}
@Override
public void processNodes(@NotNull NodeMaker nodeMaker, boolean executable) {
// TODO
}
}

View File

@ -1,6 +1,7 @@
package net.minestom.server.command.builder.parser;
import it.unimi.dsi.fastutil.ints.Int2ObjectRBTreeMap;
import net.minestom.server.command.builder.Arguments;
import net.minestom.server.command.builder.CommandSyntax;
import net.minestom.server.command.builder.arguments.Argument;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
@ -14,7 +15,7 @@ import java.util.Map;
public class CommandParser {
public static void parse(@NotNull CommandSyntax syntax, @NotNull Argument<?>[] commandArguments, @NotNull String[] inputArguments,
public static void parse(@Nullable CommandSyntax syntax, @NotNull Argument<?>[] commandArguments, @NotNull String[] inputArguments,
@Nullable List<ValidSyntaxHolder> validSyntaxes,
@Nullable Int2ObjectRBTreeMap<CommandSuggestionHolder> syntaxesSuggestions) {
final Map<Argument<?>, Object> argsValues = new HashMap<>();
@ -125,4 +126,53 @@ public class CommandParser {
}
}
/**
* 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 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
*/
@Nullable
public static ValidSyntaxHolder findMostCorrectSyntax(@NotNull List<ValidSyntaxHolder> validSyntaxes,
@NotNull Arguments executorArgs) {
if (validSyntaxes.isEmpty()) {
return null;
}
ValidSyntaxHolder finalSyntax = null;
int maxArguments = 0;
Arguments finalArguments = null;
for (ValidSyntaxHolder validSyntaxHolder : validSyntaxes) {
final Map<Argument<?>, Object> argsValues = validSyntaxHolder.argumentsValue;
final int argsSize = argsValues.size();
// Check if the syntax has more valid arguments
if (argsSize > maxArguments) {
finalSyntax = validSyntaxHolder;
maxArguments = argsSize;
// Fill arguments map
Arguments syntaxValues = new Arguments();
for (Map.Entry<Argument<?>, Object> entry : argsValues.entrySet()) {
final Argument<?> argument = entry.getKey();
final Object argumentValue = entry.getValue();
syntaxValues.setArg(argument.getId(), argumentValue);
}
finalArguments = syntaxValues;
}
}
// Get the arguments values
if (finalSyntax != null) {
executorArgs.copy(finalArguments);
}
return finalSyntax;
}
}