Allow redirect modifiers to throw exceptions

This commit is contained in:
Nathan Adams 2017-11-06 14:45:54 +01:00
parent 9d65991ae8
commit 43e18f6a04
7 changed files with 25 additions and 10 deletions

View file

@ -87,7 +87,7 @@ public class CommandDispatcher<S> {
final CommandContextBuilder<S> child = context.getChild();
if (child != null) {
if (!child.getNodes().isEmpty()) {
final Function<CommandContext<S>, Collection<S>> modifier = Iterators.getLast(context.getNodes().keySet().iterator()).getRedirectModifier();
final RedirectModifier<S> modifier = Iterators.getLast(context.getNodes().keySet().iterator()).getRedirectModifier();
for (final S source : modifier.apply(context.build())) {
contexts.add(child.copy().withSource(source));
}

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 RedirectModifier<S> {
Collection<S> apply(CommandContext<S> context) throws CommandSyntaxException;
}

View file

@ -1,6 +1,7 @@
package com.mojang.brigadier.builder;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.RedirectModifier;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.tree.CommandNode;
import com.mojang.brigadier.tree.RootCommandNode;
@ -15,7 +16,7 @@ public abstract class ArgumentBuilder<S, T extends ArgumentBuilder<S, T>> {
private Command<S> command;
private Predicate<S> requirement = s -> true;
private CommandNode<S> target;
private Function<CommandContext<S>, Collection<S>> modifier = s -> Collections.singleton(s.getSource());
private RedirectModifier<S> modifier = s -> Collections.singleton(s.getSource());
protected abstract T getThis();
@ -53,7 +54,7 @@ public abstract class ArgumentBuilder<S, T extends ArgumentBuilder<S, T>> {
return redirect(target, modifier);
}
public T redirect(final CommandNode<S> target, final Function<CommandContext<S>, Collection<S>> modifier) {
public T redirect(final CommandNode<S> target, final RedirectModifier<S> modifier) {
if (!arguments.getChildren().isEmpty()) {
throw new IllegalStateException("Cannot redirect a node with children");
}
@ -66,7 +67,7 @@ public abstract class ArgumentBuilder<S, T extends ArgumentBuilder<S, T>> {
return target;
}
public Function<CommandContext<S>, Collection<S>> getRedirectModifier() {
public RedirectModifier<S> getRedirectModifier() {
return modifier;
}

View file

@ -1,6 +1,7 @@
package com.mojang.brigadier.tree;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.RedirectModifier;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
@ -21,7 +22,7 @@ public class ArgumentCommandNode<S, T> extends CommandNode<S> {
private final String name;
private final ArgumentType<T> type;
public ArgumentCommandNode(final String name, final ArgumentType<T> type, final Command<S> command, final Predicate<S> requirement, final CommandNode<S> redirect, final Function<CommandContext<S>, Collection<S>> modifier) {
public ArgumentCommandNode(final String name, final ArgumentType<T> type, final Command<S> command, final Predicate<S> requirement, final CommandNode<S> redirect, final RedirectModifier<S> modifier) {
super(command, requirement, redirect, modifier);
this.name = name;
this.type = type;

View file

@ -3,6 +3,7 @@ package com.mojang.brigadier.tree;
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.Maps;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.RedirectModifier;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
@ -21,10 +22,10 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
private Map<Object, CommandNode<S>> children = Maps.newLinkedHashMap();
private final Predicate<S> requirement;
private final CommandNode<S> redirect;
private final Function<CommandContext<S>, Collection<S>> modifier;
private final RedirectModifier<S> modifier;
private Command<S> command;
protected CommandNode(final Command<S> command, final Predicate<S> requirement, final CommandNode<S> redirect, final Function<CommandContext<S>, Collection<S>> modifier) {
protected CommandNode(final Command<S> command, final Predicate<S> requirement, final CommandNode<S> redirect, final RedirectModifier<S> modifier) {
this.command = command;
this.requirement = requirement;
this.redirect = redirect;
@ -43,7 +44,7 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
return redirect;
}
public Function<CommandContext<S>, Collection<S>> getRedirectModifier() {
public RedirectModifier<S> getRedirectModifier() {
return modifier;
}

View file

@ -1,6 +1,7 @@
package com.mojang.brigadier.tree;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.RedirectModifier;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext;
@ -18,7 +19,7 @@ public class LiteralCommandNode<S> extends CommandNode<S> {
private final String literal;
public LiteralCommandNode(final String literal, final Command<S> command, final Predicate<S> requirement, final CommandNode<S> redirect, final Function<CommandContext<S>, Collection<S>> modifier) {
public LiteralCommandNode(final String literal, final Command<S> command, final Predicate<S> requirement, final CommandNode<S> redirect, final RedirectModifier<S> modifier) {
super(command, requirement, redirect, modifier);
this.literal = literal;
}

View file

@ -254,7 +254,7 @@ public class CommandDispatcherTest {
@SuppressWarnings("unchecked")
@Test
public void testExecuteRedirected() throws Exception {
final Function<CommandContext<Object>, Collection<Object>> modifier = mock(Function.class);
final RedirectModifier<Object> modifier = mock(RedirectModifier.class);
final Object source1 = new Object();
final Object source2 = new Object();