Added a redirect option on nodes
This commit is contained in:
@@ -7,7 +7,6 @@ import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import com.mojang.brigadier.context.CommandContextBuilder;
|
||||
import com.mojang.brigadier.exceptions.CommandException;
|
||||
import com.mojang.brigadier.exceptions.ParameterizedCommandExceptionType;
|
||||
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
|
||||
import com.mojang.brigadier.tree.CommandNode;
|
||||
import com.mojang.brigadier.tree.LiteralCommandNode;
|
||||
@@ -98,7 +97,11 @@ public class CommandDispatcher<S> {
|
||||
throw ERROR_EXPECTED_ARGUMENT_SEPARATOR.createWithContext(reader);
|
||||
}
|
||||
reader.skip();
|
||||
return parseNodes(child, reader, context);
|
||||
if (child.getRedirect() != null) {
|
||||
return parseNodes(child.getRedirect(), reader, context.redirect());
|
||||
} else {
|
||||
return parseNodes(child, reader, context);
|
||||
}
|
||||
} else {
|
||||
return new ParseResults<>(context);
|
||||
}
|
||||
@@ -122,7 +125,9 @@ public class CommandDispatcher<S> {
|
||||
result.add(prefix);
|
||||
}
|
||||
|
||||
if (!node.getChildren().isEmpty()) {
|
||||
if (node.getRedirect() != null) {
|
||||
result.add(prefix.isEmpty() ? node.getUsageText() + ARGUMENT_SEPARATOR + "..." : prefix + ARGUMENT_SEPARATOR + "...");
|
||||
} else if (!node.getChildren().isEmpty()) {
|
||||
for (final CommandNode<S> child : node.getChildren()) {
|
||||
getAllUsage(child, source, result, prefix.isEmpty() ? child.getUsageText() : prefix + ARGUMENT_SEPARATOR + child.getUsageText());
|
||||
}
|
||||
@@ -153,36 +158,40 @@ public class CommandDispatcher<S> {
|
||||
final String close = childOptional ? USAGE_OPTIONAL_CLOSE : USAGE_REQUIRED_CLOSE;
|
||||
|
||||
if (!deep) {
|
||||
final Collection<CommandNode<S>> children = node.getChildren().stream().filter(c -> c.canUse(source)).collect(Collectors.toList());
|
||||
if (children.size() == 1) {
|
||||
final String usage = getSmartUsage(children.iterator().next(), source, childOptional, childOptional);
|
||||
if (usage != null) {
|
||||
return self + ARGUMENT_SEPARATOR + usage;
|
||||
}
|
||||
} else if (children.size() > 1) {
|
||||
final Set<String> childUsage = Sets.newLinkedHashSet();
|
||||
for (final CommandNode<S> child : children) {
|
||||
final String usage = getSmartUsage(child, source, childOptional, true);
|
||||
if (node.getRedirect() != null) {
|
||||
return self + ARGUMENT_SEPARATOR + "...";
|
||||
} else {
|
||||
final Collection<CommandNode<S>> children = node.getChildren().stream().filter(c -> c.canUse(source)).collect(Collectors.toList());
|
||||
if (children.size() == 1) {
|
||||
final String usage = getSmartUsage(children.iterator().next(), source, childOptional, childOptional);
|
||||
if (usage != null) {
|
||||
childUsage.add(usage);
|
||||
return self + ARGUMENT_SEPARATOR + usage;
|
||||
}
|
||||
}
|
||||
if (childUsage.size() == 1) {
|
||||
final String usage = childUsage.iterator().next();
|
||||
return self + ARGUMENT_SEPARATOR + (childOptional ? USAGE_OPTIONAL_OPEN + usage + USAGE_OPTIONAL_CLOSE : usage);
|
||||
} else if (childUsage.size() > 1) {
|
||||
final StringBuilder builder = new StringBuilder(open);
|
||||
int count = 0;
|
||||
} else if (children.size() > 1) {
|
||||
final Set<String> childUsage = Sets.newLinkedHashSet();
|
||||
for (final CommandNode<S> child : children) {
|
||||
if (count > 0) {
|
||||
builder.append(USAGE_OR);
|
||||
final String usage = getSmartUsage(child, source, childOptional, true);
|
||||
if (usage != null) {
|
||||
childUsage.add(usage);
|
||||
}
|
||||
builder.append(child.getUsageText());
|
||||
count++;
|
||||
}
|
||||
if (count > 0) {
|
||||
builder.append(close);
|
||||
return self + ARGUMENT_SEPARATOR + builder.toString();
|
||||
if (childUsage.size() == 1) {
|
||||
final String usage = childUsage.iterator().next();
|
||||
return self + ARGUMENT_SEPARATOR + (childOptional ? USAGE_OPTIONAL_OPEN + usage + USAGE_OPTIONAL_CLOSE : usage);
|
||||
} else if (childUsage.size() > 1) {
|
||||
final StringBuilder builder = new StringBuilder(open);
|
||||
int count = 0;
|
||||
for (final CommandNode<S> child : children) {
|
||||
if (count > 0) {
|
||||
builder.append(USAGE_OR);
|
||||
}
|
||||
builder.append(child.getUsageText());
|
||||
count++;
|
||||
}
|
||||
if (count > 0) {
|
||||
builder.append(close);
|
||||
return self + ARGUMENT_SEPARATOR + builder.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -192,6 +201,9 @@ public class CommandDispatcher<S> {
|
||||
}
|
||||
|
||||
private Set<String> findSuggestions(final CommandNode<S> node, final StringReader reader, final CommandContextBuilder<S> contextBuilder, final Set<String> result) {
|
||||
if (node.getRedirect() != null) {
|
||||
return findSuggestions(node.getRedirect(), reader, contextBuilder, result);
|
||||
}
|
||||
final S source = contextBuilder.getSource();
|
||||
for (final CommandNode<S> child : node.getChildren()) {
|
||||
if (!child.canUse(source)) {
|
||||
|
||||
@@ -11,10 +11,14 @@ public abstract class ArgumentBuilder<S, T extends ArgumentBuilder<S, T>> {
|
||||
private final RootCommandNode<S> arguments = new RootCommandNode<>();
|
||||
private Command<S> command;
|
||||
private Predicate<S> requirement = s -> true;
|
||||
private CommandNode<S> target;
|
||||
|
||||
protected abstract T getThis();
|
||||
|
||||
public T then(final ArgumentBuilder<S, ?> argument) {
|
||||
if (target != null) {
|
||||
throw new IllegalStateException("Cannot add children to a redirected node");
|
||||
}
|
||||
arguments.addChild(argument.build());
|
||||
return getThis();
|
||||
}
|
||||
@@ -41,5 +45,17 @@ public abstract class ArgumentBuilder<S, T extends ArgumentBuilder<S, T>> {
|
||||
return requirement;
|
||||
}
|
||||
|
||||
public T redirect(final CommandNode<S> target) {
|
||||
if (!arguments.getChildren().isEmpty()) {
|
||||
throw new IllegalStateException("Cannot redirect a node with children");
|
||||
}
|
||||
this.target = target;
|
||||
return getThis();
|
||||
}
|
||||
|
||||
public CommandNode<S> getRedirect() {
|
||||
return target;
|
||||
}
|
||||
|
||||
public abstract CommandNode<S> build();
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ public class LiteralArgumentBuilder<S> extends ArgumentBuilder<S, LiteralArgumen
|
||||
|
||||
@Override
|
||||
public LiteralCommandNode<S> build() {
|
||||
final LiteralCommandNode<S> result = new LiteralCommandNode<>(getLiteral(), getCommand(), getRequirement());
|
||||
final LiteralCommandNode<S> result = new LiteralCommandNode<>(getLiteral(), getCommand(), getRequirement(), getRedirect());
|
||||
|
||||
for (final CommandNode<S> argument : getArguments()) {
|
||||
result.addChild(argument);
|
||||
|
||||
@@ -31,7 +31,7 @@ public class RequiredArgumentBuilder<S, T> extends ArgumentBuilder<S, RequiredAr
|
||||
}
|
||||
|
||||
public ArgumentCommandNode<S, T> build() {
|
||||
final ArgumentCommandNode<S, T> result = new ArgumentCommandNode<>(getName(), getType(), getCommand(), getRequirement());
|
||||
final ArgumentCommandNode<S, T> result = new ArgumentCommandNode<>(getName(), getType(), getCommand(), getRequirement(), getRedirect());
|
||||
|
||||
for (final CommandNode<S> argument : getArguments()) {
|
||||
result.addChild(argument);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.mojang.brigadier.context;
|
||||
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.primitives.Primitives;
|
||||
import com.mojang.brigadier.Command;
|
||||
import com.mojang.brigadier.tree.CommandNode;
|
||||
@@ -14,13 +13,19 @@ public class CommandContext<S> {
|
||||
private final Map<String, ParsedArgument<S, ?>> arguments;
|
||||
private final Map<CommandNode<S>, String> nodes;
|
||||
private final String input;
|
||||
private final CommandContext<S> parent;
|
||||
|
||||
public CommandContext(final S source, final Map<String, ParsedArgument<S, ?>> arguments, final Command<S> command, final Map<CommandNode<S>, String> nodes, final String input) {
|
||||
public CommandContext(final S source, final Map<String, ParsedArgument<S, ?>> arguments, final Command<S> command, final Map<CommandNode<S>, String> nodes, final String input, final CommandContext<S> parent) {
|
||||
this.source = source;
|
||||
this.arguments = arguments;
|
||||
this.command = command;
|
||||
this.nodes = nodes;
|
||||
this.input = input;
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public CommandContext<S> getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public Command<S> getCommand() {
|
||||
@@ -58,6 +63,7 @@ public class CommandContext<S> {
|
||||
if (!Iterables.elementsEqual(nodes.entrySet(), that.nodes.entrySet())) return false;
|
||||
if (command != null ? !command.equals(that.command) : that.command != null) return false;
|
||||
if (!source.equals(that.source)) return false;
|
||||
if (parent != null ? !parent.equals(that.parent) : that.parent != null) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -68,6 +74,7 @@ public class CommandContext<S> {
|
||||
result = 31 * result + arguments.hashCode();
|
||||
result = 31 * result + (command != null ? command.hashCode() : 0);
|
||||
result = 31 * result + nodes.hashCode();
|
||||
result = 31 * result + (parent != null ? parent.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ public class CommandContextBuilder<S> {
|
||||
private final StringBuilder input = new StringBuilder();
|
||||
private S source;
|
||||
private Command<S> command;
|
||||
private CommandContext<S> parent;
|
||||
|
||||
public CommandContextBuilder(final CommandDispatcher<S> dispatcher, final S source) {
|
||||
this.dispatcher = dispatcher;
|
||||
@@ -54,13 +55,24 @@ public class CommandContextBuilder<S> {
|
||||
|
||||
public CommandContextBuilder<S> copy() {
|
||||
final CommandContextBuilder<S> copy = new CommandContextBuilder<>(dispatcher, source);
|
||||
copy.command = this.command;
|
||||
copy.command = command;
|
||||
copy.arguments.putAll(arguments);
|
||||
copy.nodes.putAll(this.nodes);
|
||||
copy.nodes.putAll(nodes);
|
||||
copy.input.append(input);
|
||||
copy.parent = parent;
|
||||
return copy;
|
||||
}
|
||||
|
||||
public CommandContextBuilder<S> redirect() {
|
||||
final CommandContextBuilder<S> result = new CommandContextBuilder<>(dispatcher, source);
|
||||
result.parent = build();
|
||||
return result;
|
||||
}
|
||||
|
||||
public CommandContext<S> getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public String getInput() {
|
||||
return input.toString();
|
||||
}
|
||||
@@ -70,7 +82,7 @@ public class CommandContextBuilder<S> {
|
||||
}
|
||||
|
||||
public CommandContext<S> build() {
|
||||
return new CommandContext<>(source, arguments, command, nodes, input.toString());
|
||||
return new CommandContext<>(source, arguments, command, nodes, input.toString(), parent);
|
||||
}
|
||||
|
||||
public CommandDispatcher<S> getDispatcher() {
|
||||
|
||||
@@ -18,8 +18,8 @@ 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) {
|
||||
super(command, requirement);
|
||||
public ArgumentCommandNode(final String name, final ArgumentType<T> type, final Command<S> command, final Predicate<S> requirement, final CommandNode<S> redirect) {
|
||||
super(command, requirement, redirect);
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
@@ -69,6 +69,7 @@ public class ArgumentCommandNode<S, T> extends CommandNode<S> {
|
||||
public RequiredArgumentBuilder<S, T> createBuilder() {
|
||||
final RequiredArgumentBuilder<S, T> builder = RequiredArgumentBuilder.argument(name, type);
|
||||
builder.requires(getRequirement());
|
||||
builder.redirect(getRedirect());
|
||||
if (getCommand() != null) {
|
||||
builder.executes(getCommand());
|
||||
}
|
||||
|
||||
@@ -18,11 +18,13 @@ import java.util.stream.Collectors;
|
||||
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 Command<S> command;
|
||||
|
||||
protected CommandNode(final Command<S> command, final Predicate<S> requirement) {
|
||||
protected CommandNode(final Command<S> command, final Predicate<S> requirement, final CommandNode<S> redirect) {
|
||||
this.command = command;
|
||||
this.requirement = requirement;
|
||||
this.redirect = redirect;
|
||||
}
|
||||
|
||||
public Command<S> getCommand() {
|
||||
@@ -33,6 +35,10 @@ public abstract class CommandNode<S> implements Comparable<CommandNode<S>> {
|
||||
return children.values();
|
||||
}
|
||||
|
||||
public CommandNode<S> getRedirect() {
|
||||
return redirect;
|
||||
}
|
||||
|
||||
public boolean canUse(final S source) {
|
||||
return requirement.test(source);
|
||||
}
|
||||
|
||||
@@ -15,8 +15,8 @@ public class LiteralCommandNode<S> extends CommandNode<S> {
|
||||
|
||||
private final String literal;
|
||||
|
||||
public LiteralCommandNode(final String literal, final Command<S> command, final Predicate<S> requirement) {
|
||||
super(command, requirement);
|
||||
public LiteralCommandNode(final String literal, final Command<S> command, final Predicate<S> requirement, final CommandNode<S> redirect) {
|
||||
super(command, requirement, redirect);
|
||||
this.literal = literal;
|
||||
}
|
||||
|
||||
@@ -78,6 +78,7 @@ public class LiteralCommandNode<S> extends CommandNode<S> {
|
||||
public LiteralArgumentBuilder<S> createBuilder() {
|
||||
final LiteralArgumentBuilder<S> builder = LiteralArgumentBuilder.literal(this.literal);
|
||||
builder.requires(getRequirement());
|
||||
builder.redirect(getRedirect());
|
||||
if (getCommand() != null) {
|
||||
builder.executes(getCommand());
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import java.util.Set;
|
||||
|
||||
public class RootCommandNode<S> extends CommandNode<S> {
|
||||
public RootCommandNode() {
|
||||
super(null, c -> true);
|
||||
super(null, c -> true, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user