mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-24 09:01:54 +01:00
Added ArgumentGroup parsing, WIP nodes
This commit is contained in:
parent
e9d91fd21e
commit
b34b490504
@ -272,11 +272,16 @@ public final class Arguments {
|
|||||||
this.returnData = returnData;
|
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);
|
this.args.put(id, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void copy(@NotNull Arguments arguments) {
|
public void copy(@NotNull Arguments arguments) {
|
||||||
this.args = arguments.args;
|
this.args = arguments.args;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,11 +162,13 @@ public class CommandDispatcher {
|
|||||||
if (!validSyntaxes.isEmpty()) {
|
if (!validSyntaxes.isEmpty()) {
|
||||||
Arguments executorArgs = new Arguments();
|
Arguments executorArgs = new Arguments();
|
||||||
// Search the syntax with all perfect args
|
// Search the syntax with all perfect args
|
||||||
final CommandSyntax finalSyntax = findMostCorrectSyntax(validSyntaxes, executorArgs);
|
final ValidSyntaxHolder finalValidSyntax = CommandParser.findMostCorrectSyntax(validSyntaxes, executorArgs);
|
||||||
if (finalSyntax != null) {
|
if (finalValidSyntax != null) {
|
||||||
// A fully correct syntax has been found, use it
|
// A fully correct syntax has been found, use it
|
||||||
parsedCommand.syntax = finalSyntax;
|
final CommandSyntax syntax = finalValidSyntax.syntax;
|
||||||
parsedCommand.executor = finalSyntax.getExecutor();
|
|
||||||
|
parsedCommand.syntax = syntax;
|
||||||
|
parsedCommand.executor = syntax.getExecutor();
|
||||||
parsedCommand.arguments = executorArgs;
|
parsedCommand.arguments = executorArgs;
|
||||||
return parsedCommand;
|
return parsedCommand;
|
||||||
}
|
}
|
||||||
@ -197,52 +199,4 @@ public class CommandDispatcher {
|
|||||||
// No syntax found
|
// No syntax found
|
||||||
return null;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
package net.minestom.server.command.builder.parser;
|
package net.minestom.server.command.builder.parser;
|
||||||
|
|
||||||
import it.unimi.dsi.fastutil.ints.Int2ObjectRBTreeMap;
|
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.CommandSyntax;
|
||||||
import net.minestom.server.command.builder.arguments.Argument;
|
import net.minestom.server.command.builder.arguments.Argument;
|
||||||
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
||||||
@ -14,7 +15,7 @@ import java.util.Map;
|
|||||||
|
|
||||||
public class CommandParser {
|
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 List<ValidSyntaxHolder> validSyntaxes,
|
||||||
@Nullable Int2ObjectRBTreeMap<CommandSuggestionHolder> syntaxesSuggestions) {
|
@Nullable Int2ObjectRBTreeMap<CommandSuggestionHolder> syntaxesSuggestions) {
|
||||||
final Map<Argument<?>, Object> argsValues = new HashMap<>();
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user