Fix wrong redirect behavior (MC-256419) (#124)
* Fix wrong redirect behavior * Set foundCommand to `true` if redirect modifier returns no result.
This commit is contained in:
parent
a3f3eb2929
commit
f20bede62a
2 changed files with 37 additions and 1 deletions
|
@ -231,7 +231,6 @@ public class CommandDispatcher<S> {
|
||||||
if (child != null) {
|
if (child != null) {
|
||||||
forked |= context.isForked();
|
forked |= context.isForked();
|
||||||
if (child.hasNodes()) {
|
if (child.hasNodes()) {
|
||||||
foundCommand = true;
|
|
||||||
final RedirectModifier<S> modifier = context.getRedirectModifier();
|
final RedirectModifier<S> modifier = context.getRedirectModifier();
|
||||||
if (modifier == null) {
|
if (modifier == null) {
|
||||||
if (next == null) {
|
if (next == null) {
|
||||||
|
@ -248,6 +247,8 @@ public class CommandDispatcher<S> {
|
||||||
for (final S source : results) {
|
for (final S source : results) {
|
||||||
next.add(child.copyFor(source));
|
next.add(child.copyFor(source));
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
foundCommand = true;
|
||||||
}
|
}
|
||||||
} catch (final CommandSyntaxException ex) {
|
} catch (final CommandSyntaxException ex) {
|
||||||
consumer.onCommandComplete(context, false, 0);
|
consumer.onCommandComplete(context, false, 0);
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
package com.mojang.brigadier;
|
package com.mojang.brigadier;
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
|
import com.mojang.brigadier.arguments.IntegerArgumentType;
|
||||||
import com.mojang.brigadier.context.CommandContext;
|
import com.mojang.brigadier.context.CommandContext;
|
||||||
import com.mojang.brigadier.context.CommandContextBuilder;
|
import com.mojang.brigadier.context.CommandContextBuilder;
|
||||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||||
|
@ -14,6 +15,9 @@ import org.junit.runner.RunWith;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.runners.MockitoJUnitRunner;
|
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.arguments.IntegerArgumentType.integer;
|
||||||
import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal;
|
import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal;
|
||||||
import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument;
|
import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument;
|
||||||
|
@ -326,6 +330,37 @@ public class CommandDispatcherTest {
|
||||||
verify(command).run(argThat(hasProperty("source", is(source2))));
|
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
|
@Test
|
||||||
public void testExecuteOrphanedSubcommand() throws Exception {
|
public void testExecuteOrphanedSubcommand() throws Exception {
|
||||||
subject.register(literal("foo").then(
|
subject.register(literal("foo").then(
|
||||||
|
|
Loading…
Reference in a new issue