This commit is contained in:
themode 2021-03-11 02:42:15 +01:00
parent 60dc164cdc
commit f3335d25f6
4 changed files with 12 additions and 41 deletions

View File

@ -29,7 +29,7 @@ public abstract class Argument<T> {
private T defaultValue;
public SuggestionCallback suggestionCallback;
private SuggestionCallback suggestionCallback;
/**
* Creates a new argument.

View File

@ -62,7 +62,8 @@ public class CommandParser {
boolean useRemaining = false;
// Check the validity of the arguments...
for (int argIndex = 0; argIndex < commandArguments.length; argIndex++) {
ArgumentResult argumentResult = validate(commandArguments, argIndex, inputArguments, inputIndex);
final Argument<?> argument = commandArguments[argIndex];
ArgumentResult argumentResult = validate(argument, commandArguments, argIndex, inputArguments, inputIndex);
if (argumentResult == null) {
break;
}
@ -170,10 +171,11 @@ public class CommandParser {
ArgumentQueryResult maxArg = null;
int maxArgIndex = 0;
for (int argIndex = 0; argIndex < commandArguments.length; argIndex++) {
ArgumentResult argumentResult = validate(commandArguments, argIndex, args, inputIndex);
Argument<?> argument = commandArguments[argIndex];
ArgumentResult argumentResult = validate(argument, commandArguments, argIndex, args, inputIndex);
if (argumentResult == null) {
argumentResult = new ArgumentResult();
argumentResult.argument = commandArguments[argIndex];
argumentResult.argument = argument;
argumentResult.correct = false;
argumentResult.inputIndex = inputIndex;
argumentResult.rawArg = "";
@ -182,7 +184,6 @@ public class CommandParser {
// Update local var
inputIndex = argumentResult.inputIndex;
final Argument<?> argument = argumentResult.argument;
if (argumentResult.correct) {
// Fill context
context.setArg(argument.getId(), argumentResult.parsedValue, argumentResult.rawArg);
@ -218,10 +219,9 @@ public class CommandParser {
}
@Nullable
private static ArgumentResult validate(@NotNull Argument<?>[] arguments, int argIndex,
private static ArgumentResult validate(@NotNull Argument<?> argument,
@NotNull Argument<?>[] arguments, int argIndex,
@NotNull String[] inputArguments, int inputIndex) {
final Argument<?> argument = arguments[argIndex];
final boolean end = inputIndex == inputArguments.length;
if (end) // Stop if there is no input to analyze left
return null;

View File

@ -49,7 +49,7 @@ public class TabCompleteListener {
final Argument<?> argument = queryResult.argument;
final SuggestionCallback suggestionCallback = argument.suggestionCallback;
final SuggestionCallback suggestionCallback = argument.getSuggestionCallback();
if (suggestionCallback != null) {
final String input = queryResult.input;
final int inputLength = input.length();

View File

@ -1,13 +1,10 @@
package demo.commands;
import net.minestom.server.chat.ChatColor;
import net.minestom.server.chat.ColoredText;
import net.minestom.server.command.CommandSender;
import net.minestom.server.command.builder.Command;
import net.minestom.server.command.builder.CommandContext;
import net.minestom.server.command.builder.suggestion.SuggestionEntry;
import static net.minestom.server.command.builder.arguments.ArgumentType.Integer;
import static net.minestom.server.command.builder.arguments.ArgumentType.*;
public class TestCommand extends Command {
@ -16,15 +13,13 @@ public class TestCommand extends Command {
super("testcmd");
setDefaultExecutor(this::usage);
addSubcommand(new Sub());
var test1 = String("msg").setSuggestionCallback((sender, context, suggestion) -> {
var test1 = Word("msg").setSuggestionCallback((sender, context, suggestion) -> {
suggestion.addEntry(new SuggestionEntry("test"));
});
addSyntax((sender, context) -> {
System.out.println("input: " + context.get("msg"));
}, test1);
System.out.println("executed");
}, Group("test", test1));
}
@ -32,28 +27,4 @@ public class TestCommand extends Command {
sender.sendMessage("Incorrect usage");
}
private static class Sub extends Command {
public Sub() {
super("sub");
setDefaultExecutor((sender, context) -> sender.sendMessage("default sub"));
var test1 = Word("msg").setSuggestionCallback((sender, context, suggestion) -> {
final String input = suggestion.getInput();
if (!input.isEmpty()) {
int num = Integer.valueOf(input) * 2;
suggestion.addEntry(new SuggestionEntry(String.valueOf(num), ColoredText.of(ChatColor.RED, "Hover")));
System.out.println("test: " + context.get("msg3"));
}
});
var test3 = Integer("msg3");
addSyntax((sender, context) -> {
System.out.println("input: " + context.getInput());
}, test3, test1);
}
}
}