Skip to content

Commit de0ddb7

Browse files
committed
Fix childless redirects
Makes parseNodes consider the Command of a redirect target when no contents can be read. For example, given a "foo" literal with a non-null Command, and a "bar" literal forwarding to the "foo" node, the parse results' context contains the command of the "foo" node, and the parsed node is the "bar" node. This follows the same logic as a regular redirect. CommandDispatcherTest#testExecuteRedirectedMultipleTimes highlights the parse tree contains the redirected node (`redirectNode`), while the executed Command is that of the target node (`concreteNode`). Fixes Mojang#46
1 parent 1c9d8fc commit de0ddb7

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

src/main/java/com/mojang/brigadier/CommandDispatcher.java

+4
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,10 @@ private ParseResults<S> parseNodes(final CommandNode<S> node, final StringReader
401401
potentials.add(parse);
402402
}
403403
} else {
404+
final CommandNode<S> redirect = child.getRedirect();
405+
if (redirect != null && redirect.getCommand() != null) {
406+
context.withCommand(redirect.getCommand());
407+
}
404408
final ParseResults<S> parse = new ParseResults<>(context, reader, Collections.emptyMap());
405409
if (!child.canUse(parse)) {
406410
continue;

src/test/java/com/mojang/brigadier/CommandDispatcherTest.java

+22-11
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,6 @@
33

44
package com.mojang.brigadier;
55

6-
import com.google.common.collect.Lists;
7-
import com.mojang.brigadier.context.CommandContext;
8-
import com.mojang.brigadier.context.CommandContextBuilder;
9-
import com.mojang.brigadier.exceptions.CommandSyntaxException;
10-
import com.mojang.brigadier.tree.LiteralCommandNode;
11-
import org.junit.Before;
12-
import org.junit.Test;
13-
import org.junit.runner.RunWith;
14-
import org.mockito.Mock;
15-
import org.mockito.runners.MockitoJUnitRunner;
16-
176
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
187
import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal;
198
import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument;
@@ -32,6 +21,19 @@
3221
import static org.mockito.Mockito.verify;
3322
import static org.mockito.Mockito.when;
3423

24+
import com.google.common.collect.Lists;
25+
import com.mojang.brigadier.context.CommandContext;
26+
import com.mojang.brigadier.context.CommandContextBuilder;
27+
import com.mojang.brigadier.exceptions.CommandSyntaxException;
28+
import com.mojang.brigadier.tree.CommandNode;
29+
import com.mojang.brigadier.tree.LiteralCommandNode;
30+
import java.util.Collections;
31+
import org.junit.Before;
32+
import org.junit.Test;
33+
import org.junit.runner.RunWith;
34+
import org.mockito.Mock;
35+
import org.mockito.runners.MockitoJUnitRunner;
36+
3537
@RunWith(MockitoJUnitRunner.class)
3638
public class CommandDispatcherTest {
3739
private CommandDispatcher<Object> subject;
@@ -202,6 +204,15 @@ public void testParseIncompleteArgument() throws Exception {
202204
assertThat(parse.getContext().getNodes().size(), is(1));
203205
}
204206

207+
@Test
208+
public void testParseChildlessRedirect() throws Exception {
209+
final CommandNode<Object> target = subject.register(literal("foo").executes(command));
210+
subject.register(literal("bar").redirect(target));
211+
212+
final ParseResults<Object> parse = subject.parse("bar", source);
213+
assertThat(parse.getContext().getCommand(), equalTo(target.getCommand()));
214+
}
215+
205216
@SuppressWarnings("unchecked")
206217
@Test
207218
public void testExecuteAmbiguiousParentSubcommand() throws Exception {

0 commit comments

Comments
 (0)