Fix suggestion chaining

This commit is contained in:
themode 2021-03-12 22:34:33 +01:00
parent a47bf24034
commit 158df922bc
4 changed files with 18 additions and 6 deletions

View File

@ -1,5 +1,6 @@
package net.minestom.server.command.builder.arguments; package net.minestom.server.command.builder.arguments;
import com.google.common.annotations.Beta;
import net.minestom.server.command.builder.ArgumentCallback; import net.minestom.server.command.builder.ArgumentCallback;
import net.minestom.server.command.builder.Command; import net.minestom.server.command.builder.Command;
import net.minestom.server.command.builder.CommandExecutor; import net.minestom.server.command.builder.CommandExecutor;
@ -202,6 +203,7 @@ public abstract class Argument<T> {
return suggestionCallback; return suggestionCallback;
} }
@Beta
public Argument<T> setSuggestionCallback(@NotNull SuggestionCallback suggestionCallback) { public Argument<T> setSuggestionCallback(@NotNull SuggestionCallback suggestionCallback) {
this.suggestionCallback = suggestionCallback; this.suggestionCallback = suggestionCallback;
return this; return this;

View File

@ -153,7 +153,8 @@ public class CommandParser {
} }
@Nullable @Nullable
public static ArgumentQueryResult findSuggestibleArgument(@NotNull Command command, String[] args, String commandString) { public static ArgumentQueryResult findSuggestibleArgument(@NotNull Command command, String[] args, String commandString,
boolean trailingSpace) {
final Collection<CommandSyntax> syntaxes = command.getSyntaxes(); final Collection<CommandSyntax> syntaxes = command.getSyntaxes();
Int2ObjectRBTreeMap<ArgumentQueryResult> suggestions = new Int2ObjectRBTreeMap<>(Collections.reverseOrder()); Int2ObjectRBTreeMap<ArgumentQueryResult> suggestions = new Int2ObjectRBTreeMap<>(Collections.reverseOrder());
@ -200,9 +201,17 @@ public class CommandParser {
maxArgIndex = argIndex; maxArgIndex = argIndex;
} }
// Don't compute following arguments if the syntax is incorrect
if (!argumentResult.correct) { if (!argumentResult.correct) {
break; break;
} }
// Don't compute unrelated arguments
final boolean isLast = inputIndex == args.length;
if (isLast && !trailingSpace) {
break;
}
} }
if (maxArg != null) { if (maxArg != null) {
suggestions.put(maxArgIndex, maxArg); suggestions.put(maxArgIndex, maxArg);

View File

@ -34,7 +34,7 @@ public class TabCompleteListener {
} }
final ArgumentQueryResult queryResult = CommandParser.findSuggestibleArgument(commandQueryResult.command, final ArgumentQueryResult queryResult = CommandParser.findSuggestibleArgument(commandQueryResult.command,
commandQueryResult.args, commandString); commandQueryResult.args, commandString, text.endsWith(StringUtils.SPACE));
if (queryResult == null) { if (queryResult == null) {
// Suggestible argument not found // Suggestible argument not found
return; return;

View File

@ -6,6 +6,7 @@ import net.minestom.server.command.builder.CommandContext;
import net.minestom.server.command.builder.suggestion.SuggestionEntry; 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.Integer;
import static net.minestom.server.command.builder.arguments.ArgumentType.Word;
public class TestCommand extends Command { public class TestCommand extends Command {
@ -13,17 +14,17 @@ public class TestCommand extends Command {
super("testcmd"); super("testcmd");
setDefaultExecutor(this::usage); setDefaultExecutor(this::usage);
var test1 = Integer("msg").setSuggestionCallback((sender, context, suggestion) -> { var test1 = Word("msg").setSuggestionCallback((sender, context, suggestion) -> {
suggestion.addEntry(new SuggestionEntry("test")); suggestion.addEntry(new SuggestionEntry("test"));
}); });
addSyntax((sender, context) -> { var test2 = Word("msg2").setSuggestionCallback((sender, context, suggestion) -> {
sender.sendMessage("no argument syntax"); suggestion.addEntry(new SuggestionEntry("greer"));
}); });
addSyntax((sender, context) -> { addSyntax((sender, context) -> {
System.out.println("executed"); System.out.println("executed");
}, test1); }, test1, test2);
} }