Add Brigadier support.
This commit is contained in:
parent
e1c17a4906
commit
7148e76bcd
25 changed files with 822 additions and 61 deletions
|
|
@ -89,6 +89,25 @@ public abstract class CommandManager<C extends CommandSender, M extends CommandM
|
|||
}, new AcceptingCommandPreprocessor<>());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tokenize an input string
|
||||
*
|
||||
* @param input Input string
|
||||
* @return List of tokens
|
||||
*/
|
||||
@Nonnull
|
||||
public static LinkedList<String> tokenize(@Nonnull final String input) {
|
||||
final StringTokenizer stringTokenizer = new StringTokenizer(input, " ");
|
||||
final LinkedList<String> tokens = new LinkedList<>();
|
||||
while (stringTokenizer.hasMoreElements()) {
|
||||
tokens.add(stringTokenizer.nextToken());
|
||||
}
|
||||
if (input.endsWith(" ")) {
|
||||
tokens.add("");
|
||||
}
|
||||
return tokens;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a command and get a future that completes with the result
|
||||
*
|
||||
|
|
@ -134,19 +153,6 @@ public abstract class CommandManager<C extends CommandSender, M extends CommandM
|
|||
}
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
private LinkedList<String> tokenize(@Nonnull final String input) {
|
||||
final StringTokenizer stringTokenizer = new StringTokenizer(input, " ");
|
||||
final LinkedList<String> tokens = new LinkedList<>();
|
||||
while (stringTokenizer.hasMoreElements()) {
|
||||
tokens.add(stringTokenizer.nextToken());
|
||||
}
|
||||
if (input.endsWith(" ")) {
|
||||
tokens.add("");
|
||||
}
|
||||
return tokens;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new command
|
||||
*
|
||||
|
|
@ -235,7 +241,7 @@ public abstract class CommandManager<C extends CommandSender, M extends CommandM
|
|||
* @return Command tree
|
||||
*/
|
||||
@Nonnull
|
||||
CommandTree<C, M> getCommandTree() {
|
||||
public CommandTree<C, M> getCommandTree() {
|
||||
return this.commandTree;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@ import com.intellectualsites.commands.sender.CommandSender;
|
|||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
|
|
@ -491,13 +492,35 @@ public final class CommandTree<C extends CommandSender, M extends CommandMeta> {
|
|||
return casted;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an immutable collection containing all of the root nodes
|
||||
* in the tree
|
||||
*
|
||||
* @return Root nodes
|
||||
*/
|
||||
@Nonnull
|
||||
public Collection<Node<CommandComponent<C, ?>>> getRootNodes() {
|
||||
return this.internalTree.getChildren();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a named root node, if it exists
|
||||
*
|
||||
* @param name Root node name
|
||||
* @return Root node, or {@code null}
|
||||
*/
|
||||
@Nullable
|
||||
public Node<CommandComponent<C, ?>> getNamedNode(@Nullable final String name) {
|
||||
return this.getRootNodes().stream().filter(node -> node.getValue() != null
|
||||
&& node.getValue().getName().equalsIgnoreCase(name)).findAny().orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Very simple tree structure
|
||||
*
|
||||
* @param <T> Node value type
|
||||
*/
|
||||
private static final class Node<T> {
|
||||
public static final class Node<T> {
|
||||
|
||||
private final Map<String, String> nodeMeta = new HashMap<>();
|
||||
private final List<Node<T>> children = new LinkedList<>();
|
||||
|
|
@ -508,7 +531,13 @@ public final class CommandTree<C extends CommandSender, M extends CommandMeta> {
|
|||
this.value = value;
|
||||
}
|
||||
|
||||
private List<Node<T>> getChildren() {
|
||||
/**
|
||||
* Get an immutable copy of the node's child list
|
||||
*
|
||||
* @return Children
|
||||
*/
|
||||
@Nonnull
|
||||
public List<Node<T>> getChildren() {
|
||||
return Collections.unmodifiableList(this.children);
|
||||
}
|
||||
|
||||
|
|
@ -529,10 +558,30 @@ public final class CommandTree<C extends CommandSender, M extends CommandMeta> {
|
|||
return null;
|
||||
}
|
||||
|
||||
private boolean isLeaf() {
|
||||
/**
|
||||
* Check if the node is a leaf node
|
||||
*
|
||||
* @return {@code true} if the node is a leaf node, else {@code false}
|
||||
*/
|
||||
public boolean isLeaf() {
|
||||
return this.children.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the node meta instance
|
||||
*
|
||||
* @return Node meta
|
||||
*/
|
||||
@Nonnull
|
||||
public Map<String, String> getNodeMeta() {
|
||||
return this.nodeMeta;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the node value
|
||||
*
|
||||
* @return Node value
|
||||
*/
|
||||
@Nullable
|
||||
public T getValue() {
|
||||
return this.value;
|
||||
|
|
@ -555,11 +604,21 @@ public final class CommandTree<C extends CommandSender, M extends CommandMeta> {
|
|||
return Objects.hash(getValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the parent node
|
||||
*
|
||||
* @return Parent node
|
||||
*/
|
||||
@Nullable
|
||||
public Node<T> getParent() {
|
||||
return this.parent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the parent node
|
||||
*
|
||||
* @param parent new parent node
|
||||
*/
|
||||
public void setParent(@Nullable final Node<T> parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -62,4 +62,14 @@ public interface ComponentParser<C extends CommandSender, T> {
|
|||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether or not this component parser is context free. A context free
|
||||
* parser will not use the provided command context, and so supports impromptu parsing
|
||||
*
|
||||
* @return {@code true} if the parser is context free, else {@code false}
|
||||
*/
|
||||
default boolean isContextFree() {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,12 +26,21 @@ package com.intellectualsites.commands.components.parser;
|
|||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.reflect.TypeToken;
|
||||
import com.intellectualsites.commands.annotations.specifier.Range;
|
||||
import com.intellectualsites.commands.components.standard.BooleanComponent;
|
||||
import com.intellectualsites.commands.components.standard.ByteComponent;
|
||||
import com.intellectualsites.commands.components.standard.CharComponent;
|
||||
import com.intellectualsites.commands.components.standard.DoubleComponent;
|
||||
import com.intellectualsites.commands.components.standard.EnumComponent;
|
||||
import com.intellectualsites.commands.components.standard.FloatComponent;
|
||||
import com.intellectualsites.commands.components.standard.IntegerComponent;
|
||||
import com.intellectualsites.commands.components.standard.ShortComponent;
|
||||
import com.intellectualsites.commands.components.standard.StringComponent;
|
||||
import com.intellectualsites.commands.sender.CommandSender;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
|
@ -69,9 +78,27 @@ public final class StandardParserRegistry<C extends CommandSender> implements Pa
|
|||
this.<Range, Number>registerAnnotationMapper(Range.class, new RangeMapper<>());
|
||||
|
||||
/* Register standard types */
|
||||
this.registerParserSupplier(TypeToken.of(Byte.class), options ->
|
||||
new ByteComponent.ByteParser<C>((byte) options.get(StandardParameters.RANGE_MIN, Byte.MIN_VALUE),
|
||||
(byte) options.get(StandardParameters.RANGE_MAX, Byte.MAX_VALUE)));
|
||||
this.registerParserSupplier(TypeToken.of(Short.class), options ->
|
||||
new ShortComponent.ShortParser<C>((short) options.get(StandardParameters.RANGE_MIN, Short.MIN_VALUE),
|
||||
(short) options.get(StandardParameters.RANGE_MAX, Short.MAX_VALUE)));
|
||||
this.registerParserSupplier(TypeToken.of(Integer.class), options ->
|
||||
new IntegerComponent.IntegerParser<C>((int) options.get(StandardParameters.RANGE_MIN, Integer.MIN_VALUE),
|
||||
(int) options.get(StandardParameters.RANGE_MAX, Integer.MAX_VALUE)));
|
||||
this.registerParserSupplier(TypeToken.of(Float.class), options ->
|
||||
new FloatComponent.FloatParser<C>((float) options.get(StandardParameters.RANGE_MIN, Float.MIN_VALUE),
|
||||
(float) options.get(StandardParameters.RANGE_MAX, Float.MAX_VALUE)));
|
||||
this.registerParserSupplier(TypeToken.of(Double.class), options ->
|
||||
new DoubleComponent.DoubleParser<C>((double) options.get(StandardParameters.RANGE_MIN, Double.MIN_VALUE),
|
||||
(double) options.get(StandardParameters.RANGE_MAX, Double.MAX_VALUE)));
|
||||
this.registerParserSupplier(TypeToken.of(Character.class), options -> new CharComponent.CharacterParser<C>());
|
||||
/* Make this one less awful */
|
||||
this.registerParserSupplier(TypeToken.of(String.class), options -> new StringComponent.StringParser<C>(
|
||||
StringComponent.StringMode.SINGLE, (context, s) -> Collections.emptyList()));
|
||||
/* Add options to this */
|
||||
this.registerParserSupplier(TypeToken.of(Boolean.class), options -> new BooleanComponent.BooleanParser<>(false));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -98,8 +125,8 @@ public final class StandardParserRegistry<C extends CommandSender> implements Pa
|
|||
if (mapper == null) {
|
||||
return;
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
final ParserParameters parserParametersCasted = (ParserParameters) mapper.apply(annotation, parsingType);
|
||||
@SuppressWarnings("unchecked") final ParserParameters parserParametersCasted = (ParserParameters) mapper.apply(
|
||||
annotation, parsingType);
|
||||
parserParameters.merge(parserParametersCasted);
|
||||
});
|
||||
return parserParameters;
|
||||
|
|
@ -117,10 +144,18 @@ public final class StandardParserRegistry<C extends CommandSender> implements Pa
|
|||
}
|
||||
final Function<ParserParameters, ComponentParser<C, ?>> producer = this.parserSuppliers.get(actualType);
|
||||
if (producer == null) {
|
||||
/* Give enums special treatment */
|
||||
if (actualType.isSubtypeOf(Enum.class)) {
|
||||
@SuppressWarnings("all")
|
||||
final EnumComponent.EnumParser enumComponent = new EnumComponent.EnumParser((Class<Enum>)
|
||||
actualType.getRawType());
|
||||
// noinspection all
|
||||
return Optional.of(enumComponent);
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
@SuppressWarnings("unchecked")
|
||||
final ComponentParser<C, T> parser = (ComponentParser<C, T>) producer.apply(parserParameters);
|
||||
@SuppressWarnings("unchecked") final ComponentParser<C, T> parser = (ComponentParser<C, T>) producer.apply(
|
||||
parserParameters);
|
||||
return Optional.of(parser);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -146,7 +146,12 @@ public final class BooleanComponent<C extends CommandSender> extends CommandComp
|
|||
|
||||
private final boolean liberal;
|
||||
|
||||
private BooleanParser(final boolean liberal) {
|
||||
/**
|
||||
* Construct a new boolean parser
|
||||
*
|
||||
* @param liberal Whether or not it'll accept boolean-esque strings, or just booleans
|
||||
*/
|
||||
public BooleanParser(final boolean liberal) {
|
||||
this.liberal = liberal;
|
||||
}
|
||||
|
||||
|
|
@ -195,6 +200,11 @@ public final class BooleanComponent<C extends CommandSender> extends CommandComp
|
|||
|
||||
return LIBERAL;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isContextFree() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -167,7 +167,13 @@ public final class ByteComponent<C extends CommandSender> extends CommandCompone
|
|||
private final byte min;
|
||||
private final byte max;
|
||||
|
||||
private ByteParser(final byte min, final byte max) {
|
||||
/**
|
||||
* Construct a new byte parser
|
||||
*
|
||||
* @param min Minimum value
|
||||
* @param max Maximum value
|
||||
*/
|
||||
public ByteParser(final byte min, final byte max) {
|
||||
this.min = min;
|
||||
this.max = max;
|
||||
}
|
||||
|
|
@ -198,6 +204,10 @@ public final class ByteComponent<C extends CommandSender> extends CommandCompone
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isContextFree() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -111,7 +111,7 @@ public final class CharComponent<C extends CommandSender> extends CommandCompone
|
|||
}
|
||||
|
||||
|
||||
private static final class CharacterParser<C extends CommandSender> implements ComponentParser<C, Character> {
|
||||
public static final class CharacterParser<C extends CommandSender> implements ComponentParser<C, Character> {
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
|
|
@ -128,6 +128,11 @@ public final class CharComponent<C extends CommandSender> extends CommandCompone
|
|||
|
||||
return ComponentParseResult.success(input.charAt(0));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isContextFree() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -166,12 +166,18 @@ public final class DoubleComponent<C extends CommandSender> extends CommandCompo
|
|||
}
|
||||
|
||||
|
||||
private static final class DoubleParser<C extends CommandSender> implements ComponentParser<C, Double> {
|
||||
public static final class DoubleParser<C extends CommandSender> implements ComponentParser<C, Double> {
|
||||
|
||||
private final double min;
|
||||
private final double max;
|
||||
|
||||
private DoubleParser(final double min, final double max) {
|
||||
/**
|
||||
* Construct a new double parser
|
||||
*
|
||||
* @param min Minimum value
|
||||
* @param max Maximum value
|
||||
*/
|
||||
public DoubleParser(final double min, final double max) {
|
||||
this.min = min;
|
||||
this.max = max;
|
||||
}
|
||||
|
|
@ -197,6 +203,10 @@ public final class DoubleComponent<C extends CommandSender> extends CommandCompo
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isContextFree() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -130,12 +130,17 @@ public class EnumComponent<C extends CommandSender, E extends Enum<E>> extends C
|
|||
}
|
||||
|
||||
|
||||
private static final class EnumParser<C extends CommandSender, E extends Enum<E>> implements ComponentParser<C, E> {
|
||||
public static final class EnumParser<C extends CommandSender, E extends Enum<E>> implements ComponentParser<C, E> {
|
||||
|
||||
private final Class<E> enumClass;
|
||||
private final EnumSet<E> allowedValues;
|
||||
|
||||
private EnumParser(@Nonnull final Class<E> enumClass) {
|
||||
/**
|
||||
* Construct a new enum parser
|
||||
*
|
||||
* @param enumClass Enum class
|
||||
*/
|
||||
public EnumParser(@Nonnull final Class<E> enumClass) {
|
||||
this.enumClass = enumClass;
|
||||
this.allowedValues = EnumSet.allOf(enumClass);
|
||||
}
|
||||
|
|
@ -164,6 +169,11 @@ public class EnumComponent<C extends CommandSender, E extends Enum<E>> extends C
|
|||
public List<String> suggestions(@Nonnull final CommandContext<C> commandContext, @Nonnull final String input) {
|
||||
return EnumSet.allOf(this.enumClass).stream().map(e -> e.name().toLowerCase()).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isContextFree() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -166,12 +166,18 @@ public final class FloatComponent<C extends CommandSender> extends CommandCompon
|
|||
}
|
||||
|
||||
|
||||
private static final class FloatParser<C extends CommandSender> implements ComponentParser<C, Float> {
|
||||
public static final class FloatParser<C extends CommandSender> implements ComponentParser<C, Float> {
|
||||
|
||||
private final float min;
|
||||
private final float max;
|
||||
|
||||
private FloatParser(final float min, final float max) {
|
||||
/**
|
||||
* Construct a new float parser
|
||||
*
|
||||
* @param min Minimum value
|
||||
* @param max Maximum value
|
||||
*/
|
||||
public FloatParser(final float min, final float max) {
|
||||
this.min = min;
|
||||
this.max = max;
|
||||
}
|
||||
|
|
@ -197,6 +203,10 @@ public final class FloatComponent<C extends CommandSender> extends CommandCompon
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isContextFree() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -221,6 +221,10 @@ public final class IntegerComponent<C extends CommandSender> extends CommandComp
|
|||
return this.max;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isContextFree() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -197,6 +197,10 @@ public final class LongComponent<C extends CommandSender> extends CommandCompone
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isContextFree() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -166,12 +166,18 @@ public final class ShortComponent<C extends CommandSender> extends CommandCompon
|
|||
}
|
||||
|
||||
|
||||
private static final class ShortParser<C extends CommandSender> implements ComponentParser<C, Short> {
|
||||
public static final class ShortParser<C extends CommandSender> implements ComponentParser<C, Short> {
|
||||
|
||||
private final short min;
|
||||
private final short max;
|
||||
|
||||
private ShortParser(final short min, final short max) {
|
||||
/**
|
||||
* Construct a new short parser
|
||||
*
|
||||
* @param min Minimum value
|
||||
* @param max Maximum value
|
||||
*/
|
||||
public ShortParser(final short min, final short max) {
|
||||
this.min = min;
|
||||
this.max = max;
|
||||
}
|
||||
|
|
@ -197,6 +203,10 @@ public final class ShortComponent<C extends CommandSender> extends CommandCompon
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isContextFree() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ public final class StringComponent<C extends CommandSender> extends CommandCompo
|
|||
@Nonnull final String name,
|
||||
@Nonnull final StringMode stringMode,
|
||||
@Nonnull final String defaultValue,
|
||||
@Nonnull final BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider) {
|
||||
@Nonnull final BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider) {
|
||||
super(required, name, new StringParser<>(stringMode, suggestionsProvider), defaultValue);
|
||||
this.stringMode = stringMode;
|
||||
}
|
||||
|
|
@ -121,7 +121,7 @@ public final class StringComponent<C extends CommandSender> extends CommandCompo
|
|||
public static final class Builder<C extends CommandSender> extends CommandComponent.Builder<C, String> {
|
||||
|
||||
private StringMode stringMode = StringMode.SINGLE;
|
||||
private BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider = (v1, v2) -> Collections.emptyList();
|
||||
private BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider = (v1, v2) -> Collections.emptyList();
|
||||
|
||||
protected Builder(@Nonnull final String name) {
|
||||
super(name);
|
||||
|
|
@ -188,13 +188,19 @@ public final class StringComponent<C extends CommandSender> extends CommandCompo
|
|||
}
|
||||
|
||||
|
||||
private static final class StringParser<C extends CommandSender> implements ComponentParser<C, String> {
|
||||
public static final class StringParser<C extends CommandSender> implements ComponentParser<C, String> {
|
||||
|
||||
private final StringMode stringMode;
|
||||
private final BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider;
|
||||
|
||||
private StringParser(@Nonnull final StringMode stringMode,
|
||||
@Nonnull final BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider) {
|
||||
/**
|
||||
* Construct a new string parser
|
||||
*
|
||||
* @param stringMode String parsing mode
|
||||
* @param suggestionsProvider Suggestions provider
|
||||
*/
|
||||
public StringParser(@Nonnull final StringMode stringMode,
|
||||
@Nonnull final BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider) {
|
||||
this.stringMode = stringMode;
|
||||
this.suggestionsProvider = suggestionsProvider;
|
||||
}
|
||||
|
|
@ -258,6 +264,11 @@ public final class StringComponent<C extends CommandSender> extends CommandCompo
|
|||
public List<String> suggestions(@Nonnull final CommandContext<C> commandContext, @Nonnull final String input) {
|
||||
return this.suggestionsProvider.apply(commandContext, input);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isContextFree() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue