Skip to content

Commit

Permalink
Fix wrong redirect behavior (MC-256419) (#124)
Browse files Browse the repository at this point in the history
* Fix wrong redirect behavior

* Set foundCommand to `true` if redirect modifier returns no result.
  • Loading branch information
zly2006 authored Mar 30, 2023
1 parent a3f3eb2 commit f20bede
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/java/com/mojang/brigadier/CommandDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ public int execute(final ParseResults<S> parse) throws CommandSyntaxException {
if (child != null) {
forked |= context.isForked();
if (child.hasNodes()) {
foundCommand = true;
final RedirectModifier<S> modifier = context.getRedirectModifier();
if (modifier == null) {
if (next == null) {
Expand All @@ -248,6 +247,8 @@ public int execute(final ParseResults<S> parse) throws CommandSyntaxException {
for (final S source : results) {
next.add(child.copyFor(source));
}
} else {
foundCommand = true;
}
} catch (final CommandSyntaxException ex) {
consumer.onCommandComplete(context, false, 0);
Expand Down
35 changes: 35 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.IntegerArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.context.CommandContextBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
Expand All @@ -14,6 +15,9 @@
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.Collections;
import java.util.concurrent.atomic.AtomicBoolean;

import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal;
import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument;
Expand Down Expand Up @@ -326,6 +330,37 @@ public void testExecuteRedirected() throws Exception {
verify(command).run(argThat(hasProperty("source", is(source2))));
}

@Test
public void testIncompleteRedirectShouldThrow() {
final LiteralCommandNode<Object> foo = subject.register(literal("foo")
.then(literal("bar")
.then(argument("value", integer()).executes(context -> IntegerArgumentType.getInteger(context, "value"))))
.then(literal("awa").executes(context -> 2)));
final LiteralCommandNode<Object> baz = subject.register(literal("baz").redirect(foo));
try {
int result = subject.execute("baz bar", source);
fail("Should have thrown an exception");
} catch (CommandSyntaxException e) {
assertThat(e.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownCommand()));
}
}

@Test
public void testRedirectModifierEmptyResult() {
final LiteralCommandNode<Object> foo = subject.register(literal("foo")
.then(literal("bar")
.then(argument("value", integer()).executes(context -> IntegerArgumentType.getInteger(context, "value"))))
.then(literal("awa").executes(context -> 2)));
final RedirectModifier<Object> emptyModifier = context -> Collections.emptyList();
final LiteralCommandNode<Object> baz = subject.register(literal("baz").fork(foo, emptyModifier));
try {
int result = subject.execute("baz bar 100", source);
assertThat(result, is(0)); // No commands executed, so result is 0
} catch (CommandSyntaxException e) {
fail("Should not throw an exception");
}
}

@Test
public void testExecuteOrphanedSubcommand() throws Exception {
subject.register(literal("foo").then(
Expand Down

0 comments on commit f20bede

Please sign in to comment.