Skip to content

Commit f20bede

Browse files
authored
Fix wrong redirect behavior (MC-256419) (#124)
* Fix wrong redirect behavior * Set foundCommand to `true` if redirect modifier returns no result.
1 parent a3f3eb2 commit f20bede

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ public int execute(final ParseResults<S> parse) throws CommandSyntaxException {
231231
if (child != null) {
232232
forked |= context.isForked();
233233
if (child.hasNodes()) {
234-
foundCommand = true;
235234
final RedirectModifier<S> modifier = context.getRedirectModifier();
236235
if (modifier == null) {
237236
if (next == null) {
@@ -248,6 +247,8 @@ public int execute(final ParseResults<S> parse) throws CommandSyntaxException {
248247
for (final S source : results) {
249248
next.add(child.copyFor(source));
250249
}
250+
} else {
251+
foundCommand = true;
251252
}
252253
} catch (final CommandSyntaxException ex) {
253254
consumer.onCommandComplete(context, false, 0);

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package com.mojang.brigadier;
55

66
import com.google.common.collect.Lists;
7+
import com.mojang.brigadier.arguments.IntegerArgumentType;
78
import com.mojang.brigadier.context.CommandContext;
89
import com.mojang.brigadier.context.CommandContextBuilder;
910
import com.mojang.brigadier.exceptions.CommandSyntaxException;
@@ -14,6 +15,9 @@
1415
import org.mockito.Mock;
1516
import org.mockito.runners.MockitoJUnitRunner;
1617

18+
import java.util.Collections;
19+
import java.util.concurrent.atomic.AtomicBoolean;
20+
1721
import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
1822
import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal;
1923
import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument;
@@ -326,6 +330,37 @@ public void testExecuteRedirected() throws Exception {
326330
verify(command).run(argThat(hasProperty("source", is(source2))));
327331
}
328332

333+
@Test
334+
public void testIncompleteRedirectShouldThrow() {
335+
final LiteralCommandNode<Object> foo = subject.register(literal("foo")
336+
.then(literal("bar")
337+
.then(argument("value", integer()).executes(context -> IntegerArgumentType.getInteger(context, "value"))))
338+
.then(literal("awa").executes(context -> 2)));
339+
final LiteralCommandNode<Object> baz = subject.register(literal("baz").redirect(foo));
340+
try {
341+
int result = subject.execute("baz bar", source);
342+
fail("Should have thrown an exception");
343+
} catch (CommandSyntaxException e) {
344+
assertThat(e.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand()));
345+
}
346+
}
347+
348+
@Test
349+
public void testRedirectModifierEmptyResult() {
350+
final LiteralCommandNode<Object> foo = subject.register(literal("foo")
351+
.then(literal("bar")
352+
.then(argument("value", integer()).executes(context -> IntegerArgumentType.getInteger(context, "value"))))
353+
.then(literal("awa").executes(context -> 2)));
354+
final RedirectModifier<Object> emptyModifier = context -> Collections.emptyList();
355+
final LiteralCommandNode<Object> baz = subject.register(literal("baz").fork(foo, emptyModifier));
356+
try {
357+
int result = subject.execute("baz bar 100", source);
358+
assertThat(result, is(0)); // No commands executed, so result is 0
359+
} catch (CommandSyntaxException e) {
360+
fail("Should not throw an exception");
361+
}
362+
}
363+
329364
@Test
330365
public void testExecuteOrphanedSubcommand() throws Exception {
331366
subject.register(literal("foo").then(

0 commit comments

Comments
 (0)