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

View file

@ -111,13 +111,20 @@ public class CommandDispatcher<S> {
} }
next.add(child.copyFor(context.getSource())); next.add(child.copyFor(context.getSource()));
} else { } else {
final Collection<S> results = modifier.apply(context); try {
if (!results.isEmpty()) { final Collection<S> results = modifier.apply(context);
if (next == null) { if (!results.isEmpty()) {
next = new ArrayList<>(results.size()); if (next == null) {
next = new ArrayList<>(results.size());
}
for (final S source : results) {
next.add(child.copyFor(source));
}
} }
for (final S source : results) { } catch (final CommandSyntaxException ex) {
next.add(child.copyFor(source)); consumer.onCommandComplete(context, false, 0);
if (!forked) {
throw ex;
} }
} }
} }

View file

@ -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;
}

View file

@ -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);
} }