Catch redirect errors when forked

This commit is contained in:
Nathan Adams
2018-01-17 12:46:19 +01:00
parent 9fbf7bfe42
commit 928912de68
3 changed files with 26 additions and 9 deletions
@@ -111,6 +111,7 @@ public class CommandDispatcher<S> {
} }
next.add(child.copyFor(context.getSource())); next.add(child.copyFor(context.getSource()));
} else { } else {
try {
final Collection<S> results = modifier.apply(context); final Collection<S> results = modifier.apply(context);
if (!results.isEmpty()) { if (!results.isEmpty()) {
if (next == null) { if (next == null) {
@@ -120,6 +121,12 @@ public class CommandDispatcher<S> {
next.add(child.copyFor(source)); next.add(child.copyFor(source));
} }
} }
} catch (final CommandSyntaxException ex) {
consumer.onCommandComplete(context, false, 0);
if (!forked) {
throw ex;
}
}
} }
} }
} else if (context.getCommand() != null) { } else if (context.getCommand() != null) {
@@ -0,0 +1,11 @@
package com.mojang.brigadier;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import java.util.Collection;
@FunctionalInterface
public interface SingleRedirectModifier<S> {
S apply(CommandContext<S> context) throws CommandSyntaxException;
}
@@ -2,13 +2,12 @@ package com.mojang.brigadier.builder;
import com.mojang.brigadier.Command; import com.mojang.brigadier.Command;
import com.mojang.brigadier.RedirectModifier; import com.mojang.brigadier.RedirectModifier;
import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.SingleRedirectModifier;
import com.mojang.brigadier.tree.CommandNode; import com.mojang.brigadier.tree.CommandNode;
import com.mojang.brigadier.tree.RootCommandNode; import com.mojang.brigadier.tree.RootCommandNode;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
public abstract class ArgumentBuilder<S, T extends ArgumentBuilder<S, T>> { public abstract class ArgumentBuilder<S, T extends ArgumentBuilder<S, T>> {
@@ -63,7 +62,7 @@ public abstract class ArgumentBuilder<S, T extends ArgumentBuilder<S, T>> {
return forward(target, null, false); return forward(target, null, false);
} }
public T redirect(final CommandNode<S> target, final Function<CommandContext<S>, S> modifier) { public T redirect(final CommandNode<S> target, final SingleRedirectModifier<S> modifier) {
return forward(target, modifier == null ? null : o -> Collections.singleton(modifier.apply(o)), false); return forward(target, modifier == null ? null : o -> Collections.singleton(modifier.apply(o)), false);
} }