Prioritise errors by chain length, build subcommands first (fix #1934) (#1935)

* Fix empty command chain causing NullPointerException

* Add test for empty command input

* Add test for #1934; subcommand priority issue

* Fix ConversionNode; process subcommands first

* Fix command error priority, add test for literal suggestions

* Test for subcommand priority in graph tests

(cherry picked from commit 2cdb3911b0)
This commit is contained in:
Spanner 2023-09-07 12:58:34 +00:00 committed by mworzala
parent 7320437640
commit fe46ed4ac2
No known key found for this signature in database
GPG Key ID: B148F922E64797C7
2 changed files with 72 additions and 3 deletions

View File

@ -70,6 +70,10 @@ final class CommandParserImpl implements CommandParser {
return nodeResults.stream().map(x -> x.node.argument()).collect(Collectors.toList());
}
int size() {
return nodeResults.size();
}
Chain() {}
Chain(CommandExecutor defaultExecutor,
@ -133,7 +137,7 @@ final class CommandParserImpl implements CommandParser {
NodeResult nodeResult = new NodeResult(node, chain, (ArgumentResult<Object>) result, suggestionCallback);
chain.append(nodeResult);
if (suggestionCallback != null) chain.suggestionCallback = suggestionCallback;
if (chain.nodeResults.size() == 1) { // If this is the root node (usually "Literal<>")
if (chain.size() == 1) { // If this is the root node (usually "Literal<>")
reader.cursor(start);
} else {
if (!(result instanceof ArgumentResult.Success<?>)) {
@ -170,11 +174,11 @@ final class CommandParserImpl implements CommandParser {
// Assume that there is only one successful node for a given chain of arguments
return childResult;
} else {
if (error == null) {
if (error == null || error.chain.size() < childResult.chain.size()) {
// If this is the base argument (e.g. "teleport" in /teleport) then
// do not report an argument to be incompatible, since the more
// correct thing would be to say that the command is unknown.
if (!(childResult.chain.nodeResults.size() == 2 && childResult.argumentResult instanceof ArgumentResult.IncompatibleType<?>)) {
if (!(childResult.chain.size() == 2 && childResult.argumentResult instanceof ArgumentResult.IncompatibleType<?>)) {
error = childResult;
}
}

View File

@ -73,4 +73,69 @@ public class CommandSuggestionIntegrationTest {
assertEquals(List.of(new TabCompletePacket.Match("suggestion", null)), tabCompletePacket.matches());
});
}
@Test
public void suggestionWithSubcommand(Env env) {
var instance = env.createFlatInstance();
var connection = env.createConnection();
var player = connection.connect(instance, new Pos(0, 42, 0)).join();
var command = new Command("foo");
var subCommand = new Command("bar");
var wordArg1 = Word("wordArg1").setSuggestionCallback((sender, context, suggestion) -> {
suggestion.addEntry(new SuggestionEntry("suggestionA"));
});
var wordArg2 = Word("wordArg2").setSuggestionCallback((sender, context, suggestion) -> {
suggestion.addEntry(new SuggestionEntry("suggestionB"));
});
subCommand.addSyntax((sender, context) -> {}, wordArg1, wordArg2);
command.addSyntax((sender,context)->{}, Literal("literal"), wordArg2);
command.addSubcommand(subCommand);
env.process().command().register(command);
var listener = connection.trackIncoming(TabCompletePacket.class);
player.addPacketToQueue(new ClientTabCompletePacket(1, "foo bar "));
player.interpretPacketQueue();
listener.assertSingle(tabCompletePacket -> {
assertEquals(List.of(new TabCompletePacket.Match("suggestionA", null)), tabCompletePacket.matches());
});
}
@Test
public void suggestionWithTwoLiterals(Env env) {
var instance = env.createFlatInstance();
var connection = env.createConnection();
var player = connection.connect(instance, new Pos(0, 42, 0)).join();
var command = new Command("foo");
var wordArg1 = Word("wordArg1").setSuggestionCallback((sender, context, suggestion) -> {
suggestion.addEntry(new SuggestionEntry("suggestionA"));
});
var wordArg2 = Word("wordArg2").setSuggestionCallback((sender, context, suggestion) -> {
suggestion.addEntry(new SuggestionEntry("suggestionB"));
});
command.addSyntax((sender,context)->{}, Literal("literal1"), wordArg1);
command.addSyntax((sender,context)->{}, Literal("literal2"), wordArg2);
env.process().command().register(command);
var listener = connection.trackIncoming(TabCompletePacket.class);
player.addPacketToQueue(new ClientTabCompletePacket(1, "foo literal2 "));
player.interpretPacketQueue();
listener.assertSingle(tabCompletePacket -> {
assertEquals(List.of(new TabCompletePacket.Match("suggestionB", null)), tabCompletePacket.matches());
});
}
}