Skip to content

Commit

Permalink
Consider arguments when literal is impermissible
Browse files Browse the repository at this point in the history
Fixes Mojang#87
  • Loading branch information
hugmanrique committed Apr 3, 2021
1 parent cf754c4 commit 151eb06
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/main/java/com/mojang/brigadier/tree/CommandNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -160,7 +161,15 @@ public Collection<? extends CommandNode<S>> getRelevantNodes(final StringReader
input.setCursor(cursor);
final LiteralCommandNode<S> literal = literals.get(text);
if (literal != null) {
return Collections.singleton(literal);
final int argumentsCount = arguments.size();
if (argumentsCount == 0) {
return Collections.singletonList(literal);
} else {
final Collection<CommandNode<S>> nodes = new ArrayList<>(argumentsCount + 1);
nodes.add(literal); // literals have priority over arguments
nodes.addAll(arguments.values());
return nodes;
}
} else {
return arguments.values();
}
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/com/mojang/brigadier/CommandDispatcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package com.mojang.brigadier;

import com.google.common.collect.Lists;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
Expand Down Expand Up @@ -256,6 +257,27 @@ public void testExecuteAmbiguiousParentSubcommandViaRedirect() throws Exception
verify(command, never()).run(any());
}

@SuppressWarnings("unchecked")
@Test
public void testExecuteAmbiguousArgumentIfImpermissibleLiteral() throws Exception {
final Command<Object> command = mock(Command.class);
when(command.run(any())).thenReturn(100);

subject.register(literal("foo")
.then(
literal("bar")
.requires(source -> false)
)
.then(
argument("argument", StringArgumentType.word())
.executes(command)
)
);

assertThat(subject.execute("foo bar", source), is(100));
verify(command).run(any(CommandContext.class));
}

@SuppressWarnings("unchecked")
@Test
public void testExecuteRedirectedMultipleTimes() throws Exception {
Expand Down

0 comments on commit 151eb06

Please sign in to comment.