Skip to content

Commit

Permalink
Merge pull request #7 from hugmanrique/fix-87-velo
Browse files Browse the repository at this point in the history
Consider arguments when literal is impermissible
  • Loading branch information
astei authored Apr 22, 2021
2 parents e936e6c + 7b21e49 commit 70c9ac4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/main/java/com/mojang/brigadier/tree/CommandNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,15 @@
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;

import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiPredicate;
import java.util.function.Predicate;
Expand Down Expand Up @@ -183,7 +190,16 @@ public Collection<? extends CommandNode<S>> getRelevantNodes(final StringReader
input.setCursor(cursor);
final CommandNode<S> node = children.get(text);
if (node instanceof LiteralCommandNode<?>) {
return Collections.singleton(node);
final int argumentsCount = arguments.size();
if (argumentsCount == 0) {
return Collections.singletonList(node);
} else {
final List<CommandNode<S>> nodes =
new ArrayList<>(argumentsCount + 1);
nodes.add(node); // literals have priority over arguments
nodes.addAll(arguments.values());
return nodes;
}
} else {
return arguments.values();
}
Expand Down
42 changes: 42 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.context.StringRange;
Expand Down Expand Up @@ -302,6 +303,47 @@ public void testExecuteAmbiguiousParentSubcommandViaRedirect() throws Exception
verify(command, never()).run(any());
}

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

subject.register(
literal("test")
.then(
argument("incorrect", StringArgumentType.word())
.executes(command)
)
.then(
literal("hello")
.executes(literalCommand)
)
);

assertThat(subject.execute("test hello", source), is(100));
verify(literalCommand).run(any(CommandContext.class));
verify(command, never()).run(any());
}

@SuppressWarnings("unchecked")
@Test
public void testExecuteAmbiguousArgumentIfImpermissibleLiteral() throws Exception {
subject.register(literal("foo")
.then(
literal("bar")
.requires(source -> false)
)
.then(
argument("argument", StringArgumentType.word())
.executes(command)
)
);

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

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

0 comments on commit 70c9ac4

Please sign in to comment.