mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-16 04:11:39 +01:00
* 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:
parent
7320437640
commit
fe46ed4ac2
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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());
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user