Add Brigadier support.
This commit is contained in:
parent
e1c17a4906
commit
7148e76bcd
25 changed files with 822 additions and 61 deletions
|
|
@ -146,7 +146,6 @@
|
|||
<module name="ParenPad"/>
|
||||
<module name="TypecastParenPad"/>
|
||||
<module name="WhitespaceAfter"/>
|
||||
<module name="WhitespaceAround"/>
|
||||
|
||||
<!-- Modifier Checks -->
|
||||
<!-- See https://checkstyle.org/config_modifiers.html -->
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,263 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
package com.intellectualsites.commands.brigadier;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.reflect.TypeToken;
|
||||
import com.intellectualsites.commands.CommandTree;
|
||||
import com.intellectualsites.commands.components.CommandComponent;
|
||||
import com.intellectualsites.commands.components.StaticComponent;
|
||||
import com.intellectualsites.commands.components.standard.BooleanComponent;
|
||||
import com.intellectualsites.commands.components.standard.ByteComponent;
|
||||
import com.intellectualsites.commands.components.standard.DoubleComponent;
|
||||
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 com.mojang.brigadier.arguments.ArgumentType;
|
||||
import com.mojang.brigadier.arguments.BoolArgumentType;
|
||||
import com.mojang.brigadier.arguments.DoubleArgumentType;
|
||||
import com.mojang.brigadier.arguments.FloatArgumentType;
|
||||
import com.mojang.brigadier.arguments.IntegerArgumentType;
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
|
||||
import com.mojang.brigadier.suggestion.SuggestionProvider;
|
||||
import com.mojang.brigadier.tree.CommandNode;
|
||||
import com.mojang.brigadier.tree.LiteralCommandNode;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiPredicate;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Manager used to map cloud {@link com.intellectualsites.commands.Command}
|
||||
* <p>
|
||||
* The structure of this class is largely inspired by
|
||||
* <a href="https://github.com/aikar/commands/blob/master/brigadier/src/main/java/co.aikar.commands/ACFBrigadierManager.java">
|
||||
* ACFBrigadiermanager</a> in the ACF project, which was originally written by MiniDigger and licensed under the MIT license.
|
||||
*
|
||||
* @param <C> Command sender type
|
||||
* @param <S> Brigadier sender type
|
||||
*/
|
||||
public final class CloudBrigadierManager<C extends CommandSender, S> {
|
||||
|
||||
private final Map<Class<?>, Function<? extends CommandComponent<C, ?>,
|
||||
? extends ArgumentType<?>>> mappers;
|
||||
|
||||
/**
|
||||
* Create a new cloud brigadier manager
|
||||
*/
|
||||
public CloudBrigadierManager() {
|
||||
this.mappers = Maps.newHashMap();
|
||||
this.registerInternalMappings();
|
||||
}
|
||||
|
||||
private void registerInternalMappings() {
|
||||
/* Map byte, short and int to IntegerArgumentType */
|
||||
this.registerMapping(new TypeToken<ByteComponent<C>>() {
|
||||
}, component -> {
|
||||
final boolean hasMin = component.getMin() != Byte.MIN_VALUE;
|
||||
final boolean hasMax = component.getMax() != Byte.MAX_VALUE;
|
||||
if (hasMin) {
|
||||
return IntegerArgumentType.integer(component.getMin(), component.getMax());
|
||||
} else if (hasMax) {
|
||||
return IntegerArgumentType.integer(Byte.MIN_VALUE, component.getMax());
|
||||
} else {
|
||||
return IntegerArgumentType.integer();
|
||||
}
|
||||
});
|
||||
this.registerMapping(new TypeToken<ShortComponent<C>>() {
|
||||
}, component -> {
|
||||
final boolean hasMin = component.getMin() != Short.MIN_VALUE;
|
||||
final boolean hasMax = component.getMax() != Short.MAX_VALUE;
|
||||
if (hasMin) {
|
||||
return IntegerArgumentType.integer(component.getMin(), component.getMax());
|
||||
} else if (hasMax) {
|
||||
return IntegerArgumentType.integer(Short.MIN_VALUE, component.getMax());
|
||||
} else {
|
||||
return IntegerArgumentType.integer();
|
||||
}
|
||||
});
|
||||
this.registerMapping(new TypeToken<IntegerComponent<C>>() {
|
||||
}, component -> {
|
||||
final boolean hasMin = component.getMin() != Integer.MIN_VALUE;
|
||||
final boolean hasMax = component.getMax() != Integer.MAX_VALUE;
|
||||
|
||||
System.out.println("Constructing new IntegerArgumentType with min " + hasMin + " | max " + hasMax);
|
||||
|
||||
if (hasMin) {
|
||||
return IntegerArgumentType.integer(component.getMin(), component.getMax());
|
||||
} else if (hasMax) {
|
||||
return IntegerArgumentType.integer(Integer.MIN_VALUE, component.getMax());
|
||||
} else {
|
||||
return IntegerArgumentType.integer();
|
||||
}
|
||||
});
|
||||
/* Map float to FloatArgumentType */
|
||||
this.registerMapping(new TypeToken<FloatComponent<C>>() {
|
||||
}, component -> {
|
||||
final boolean hasMin = component.getMin() != Float.MIN_VALUE;
|
||||
final boolean hasMax = component.getMax() != Float.MAX_VALUE;
|
||||
if (hasMin) {
|
||||
return FloatArgumentType.floatArg(component.getMin(), component.getMax());
|
||||
} else if (hasMax) {
|
||||
return FloatArgumentType.floatArg(Float.MIN_VALUE, component.getMax());
|
||||
} else {
|
||||
return FloatArgumentType.floatArg();
|
||||
}
|
||||
});
|
||||
/* Map double to DoubleArgumentType */
|
||||
this.registerMapping(new TypeToken<DoubleComponent<C>>() {
|
||||
}, component -> {
|
||||
final boolean hasMin = component.getMin() != Double.MIN_VALUE;
|
||||
final boolean hasMax = component.getMax() != Double.MAX_VALUE;
|
||||
if (hasMin) {
|
||||
return DoubleArgumentType.doubleArg(component.getMin(), component.getMax());
|
||||
} else if (hasMax) {
|
||||
return DoubleArgumentType.doubleArg(Double.MIN_VALUE, component.getMax());
|
||||
} else {
|
||||
return DoubleArgumentType.doubleArg();
|
||||
}
|
||||
});
|
||||
/* Map boolean to BoolArgumentType */
|
||||
this.registerMapping(new TypeToken<BooleanComponent<C>>() {
|
||||
}, component -> BoolArgumentType.bool());
|
||||
/* Map String properly to StringArgumentType */
|
||||
this.registerMapping(new TypeToken<StringComponent<C>>() {
|
||||
}, component -> {
|
||||
switch (component.getStringMode()) {
|
||||
case SINGLE:
|
||||
return StringArgumentType.word();
|
||||
case QUOTED:
|
||||
return StringArgumentType.string();
|
||||
case GREEDY:
|
||||
return StringArgumentType.greedyString();
|
||||
default:
|
||||
return StringArgumentType.word();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a cloud-Brigadier mapping
|
||||
*
|
||||
* @param componentType cloud component type
|
||||
* @param mapper mapper function
|
||||
* @param <T> cloud component value type
|
||||
* @param <K> cloud component type
|
||||
* @param <O> Brigadier argument type value
|
||||
*/
|
||||
public <T, K extends CommandComponent<C, T>, O> void registerMapping(@Nonnull final TypeToken<K> componentType,
|
||||
@Nonnull final Function<? extends K,
|
||||
? extends ArgumentType<O>> mapper) {
|
||||
this.mappers.put(componentType.getRawType(), mapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a Brigadier {@link ArgumentType} from a cloud {@link CommandComponent}
|
||||
*
|
||||
* @param componentType cloud component type
|
||||
* @param component cloud component
|
||||
* @param <T> cloud component value type (generic)
|
||||
* @param <K> cloud component type (generic)
|
||||
* @return Brigadier argument type
|
||||
*/
|
||||
@Nonnull
|
||||
@SuppressWarnings("all")
|
||||
public <T, K extends CommandComponent<?, ?>> ArgumentType<?> getArgument(@Nonnull final TypeToken<T> componentType,
|
||||
@Nonnull final K component) {
|
||||
final CommandComponent<C, ?> commandComponent = (CommandComponent<C, ?>) component;
|
||||
final Function function = this.mappers.getOrDefault(componentType.getRawType(), t ->
|
||||
createDefaultMapper((CommandComponent<C, T>) component));
|
||||
return (ArgumentType<?>) function.apply(commandComponent);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
private <T, K extends CommandComponent<C, T>> ArgumentType<?> createDefaultMapper(@Nonnull final CommandComponent<C, T>
|
||||
component) {
|
||||
return StringArgumentType.string();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a literal command from Brigadier command info, and a cloud command instance
|
||||
*
|
||||
* @param cloudCommand Cloud root command
|
||||
* @param root Brigadier root command
|
||||
* @param suggestionProvider Brigadier suggestions provider
|
||||
* @param executor Brigadier command executor
|
||||
* @param permissionChecker Permission checker
|
||||
* @return Constructed literal command node
|
||||
*/
|
||||
@Nonnull
|
||||
public LiteralCommandNode<S> createLiteralCommandNode(@Nonnull final CommandTree.Node<CommandComponent<C, ?>> cloudCommand,
|
||||
@Nonnull final LiteralCommandNode<S> root,
|
||||
@Nonnull final SuggestionProvider<S> suggestionProvider,
|
||||
@Nonnull final com.mojang.brigadier.Command<S> executor,
|
||||
@Nonnull final BiPredicate<S, String> permissionChecker) {
|
||||
final LiteralArgumentBuilder<S> literalArgumentBuilder = LiteralArgumentBuilder.<S>literal(root.getLiteral())
|
||||
.requires(sender -> permissionChecker.test(sender, cloudCommand.getNodeMeta().getOrDefault("permission", "")));
|
||||
if (cloudCommand.isLeaf() && cloudCommand.getValue() != null && cloudCommand.getValue().getOwningCommand() != null) {
|
||||
literalArgumentBuilder.executes(executor);
|
||||
}
|
||||
final LiteralCommandNode<S> constructedRoot = literalArgumentBuilder.build();
|
||||
for (final CommandTree.Node<CommandComponent<C, ?>> child : cloudCommand.getChildren()) {
|
||||
constructedRoot.addChild(this.constructCommandNode(child, permissionChecker, executor, suggestionProvider));
|
||||
}
|
||||
return constructedRoot;
|
||||
}
|
||||
|
||||
private CommandNode<S> constructCommandNode(@Nonnull final CommandTree.Node<CommandComponent<C, ?>> root,
|
||||
@Nonnull final BiPredicate<S, String> permissionChecker,
|
||||
@Nonnull final com.mojang.brigadier.Command<S> executor,
|
||||
@Nonnull final SuggestionProvider<S> suggestionProvider) {
|
||||
CommandNode<S> commandNode;
|
||||
if (root.getValue() instanceof StaticComponent) {
|
||||
final LiteralArgumentBuilder<S> argumentBuilder = LiteralArgumentBuilder.<S>literal(root.getValue().getName())
|
||||
.requires(sender -> permissionChecker.test(sender, root.getNodeMeta().getOrDefault("permission", "")));
|
||||
if (root.isLeaf()) {
|
||||
argumentBuilder.executes(executor);
|
||||
}
|
||||
commandNode = argumentBuilder.build();
|
||||
} else {
|
||||
@SuppressWarnings("unchecked") final RequiredArgumentBuilder<S, Object> builder = RequiredArgumentBuilder
|
||||
.<S, Object>argument(root.getValue().getName(),
|
||||
(ArgumentType<Object>) getArgument(TypeToken.of(root.getValue().getClass()),
|
||||
root.getValue()))
|
||||
.suggests(suggestionProvider)
|
||||
.requires(sender -> permissionChecker.test(sender, root.getNodeMeta().getOrDefault("permission", "")));
|
||||
if (root.isLeaf() || !root.getValue().isRequired()) {
|
||||
builder.executes(executor);
|
||||
}
|
||||
commandNode = builder.build();
|
||||
}
|
||||
for (final CommandTree.Node<CommandComponent<C, ?>> node : root.getChildren()) {
|
||||
commandNode.addChild(constructCommandNode(node, permissionChecker, executor, suggestionProvider));
|
||||
}
|
||||
return commandNode;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
|
||||
/**
|
||||
* Brigadier mappings
|
||||
*/
|
||||
package com.intellectualsites.commands.brigadier;
|
||||
|
|
@ -62,7 +62,7 @@
|
|||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.intellectualsites</groupId>
|
||||
<artifactId>cloud-bukkit</artifactId>
|
||||
<artifactId>cloud-paper</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ package com.intellectualsites.commands;
|
|||
|
||||
import com.intellectualsites.commands.components.StaticComponent;
|
||||
import com.intellectualsites.commands.components.standard.EnumComponent;
|
||||
import com.intellectualsites.commands.components.standard.IntegerComponent;
|
||||
import com.intellectualsites.commands.components.standard.StringComponent;
|
||||
import com.intellectualsites.commands.execution.CommandExecutionCoordinator;
|
||||
import org.bukkit.Bukkit;
|
||||
|
|
@ -39,32 +40,57 @@ import java.util.stream.Collectors;
|
|||
|
||||
public final class BukkitTest extends JavaPlugin {
|
||||
|
||||
private static final int PERC_MIN = 0;
|
||||
private static final int PERC_MAX = 100;
|
||||
|
||||
@Override
|
||||
public void onLoad() {
|
||||
public void onEnable() {
|
||||
try {
|
||||
final BukkitCommandManager commandManager = new BukkitCommandManager(this,
|
||||
CommandExecutionCoordinator.simpleCoordinator());
|
||||
final PaperCommandManager commandManager = new PaperCommandManager(this,
|
||||
CommandExecutionCoordinator.simpleCoordinator());
|
||||
commandManager.registerBrigadier();
|
||||
commandManager.registerCommand(commandManager.commandBuilder("gamemode",
|
||||
Collections.singleton("gajmöde"),
|
||||
BukkitCommandMetaBuilder.builder()
|
||||
.withDescription("Your ugli")
|
||||
.build())
|
||||
.withComponent(EnumComponent.required(GameMode.class, "gamemode"))
|
||||
.withComponent(StringComponent.<BukkitCommandSender>newBuilder("player")
|
||||
.withSuggestionsProvider((v1, v2) -> {
|
||||
final List<String> suggestions = new ArrayList<>(Bukkit.getOnlinePlayers().stream()
|
||||
.map(Player::getName).collect(Collectors.toList()));
|
||||
suggestions.add("dog");
|
||||
suggestions.add("cat");
|
||||
return suggestions;
|
||||
}).build())
|
||||
.withHandler(c -> c.getCommandSender()
|
||||
.asPlayer()
|
||||
.setGameMode(c.<GameMode>get("gamemode")
|
||||
.orElse(GameMode.SURVIVAL)))
|
||||
.build())
|
||||
Collections.singleton("gajmöde"),
|
||||
BukkitCommandMetaBuilder.builder()
|
||||
.withDescription("Your ugli")
|
||||
.build())
|
||||
.withComponent(EnumComponent.required(GameMode.class, "gamemode"))
|
||||
.withComponent(StringComponent.<BukkitCommandSender>newBuilder("player")
|
||||
.withSuggestionsProvider((v1, v2) -> {
|
||||
final List<String> suggestions =
|
||||
new ArrayList<>(
|
||||
Bukkit.getOnlinePlayers()
|
||||
.stream()
|
||||
.map(Player::getName)
|
||||
.collect(Collectors.toList()));
|
||||
suggestions.add("dog");
|
||||
suggestions.add("cat");
|
||||
return suggestions;
|
||||
}).build())
|
||||
.withHandler(c -> c.getCommandSender()
|
||||
.asPlayer()
|
||||
.setGameMode(c.<GameMode>get("gamemode")
|
||||
.orElse(GameMode.SURVIVAL)))
|
||||
.build())
|
||||
.registerCommand(commandManager.commandBuilder("kenny")
|
||||
.withComponent(StaticComponent.required("sux"))
|
||||
.withComponent(IntegerComponent
|
||||
.<BukkitCommandSender>newBuilder("perc")
|
||||
.withMin(PERC_MIN).withMax(PERC_MAX).build())
|
||||
.withHandler(context -> {
|
||||
context.getCommandSender().asPlayer().sendMessage(String.format(
|
||||
"Kenny sux %d%%",
|
||||
context.<Integer>get("perc").orElse(PERC_MIN)
|
||||
));
|
||||
})
|
||||
.build())
|
||||
.registerCommand(commandManager.commandBuilder("test")
|
||||
.withComponent(StaticComponent.required("one"))
|
||||
.withHandler(c -> c.getCommandSender().sendMessage("One!"))
|
||||
.build())
|
||||
.registerCommand(commandManager.commandBuilder("test")
|
||||
.withComponent(StaticComponent.required("two"))
|
||||
.withHandler(c -> c.getCommandSender().sendMessage("Two!"))
|
||||
.build());
|
||||
} catch (final Exception e) {
|
||||
e.printStackTrace();
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@
|
|||
package com.intellectualsites.commands;
|
||||
|
||||
import com.intellectualsites.commands.components.CommandComponent;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.PluginIdentifiableCommand;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
|
@ -56,6 +57,7 @@ final class BukkitCommand extends org.bukkit.command.Command implements PluginId
|
|||
this.bukkitCommandManager.executeCommand(BukkitCommandSender.of(commandSender), builder.toString())
|
||||
.whenComplete(((commandResult, throwable) -> {
|
||||
if (throwable != null) {
|
||||
commandSender.sendMessage(ChatColor.RED + throwable.getCause().getMessage());
|
||||
throwable.printStackTrace();
|
||||
} else {
|
||||
// Do something...
|
||||
|
|
|
|||
95
cloud-minecraft/cloud-paper/pom.xml
Normal file
95
cloud-minecraft/cloud-paper/pom.xml
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~
|
||||
~ MIT License
|
||||
~
|
||||
~ Copyright (c) 2020 Alexander Söderberg
|
||||
~
|
||||
~ Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
~ of this software and associated documentation files (the "Software"), to deal
|
||||
~ in the Software without restriction, including without limitation the rights
|
||||
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
~ copies of the Software, and to permit persons to whom the Software is
|
||||
~ furnished to do so, subject to the following conditions:
|
||||
~
|
||||
~ The above copyright notice and this permission notice shall be included in all
|
||||
~ copies or substantial portions of the Software.
|
||||
~
|
||||
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
~ SOFTWARE.
|
||||
~
|
||||
-->
|
||||
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>cloud</artifactId>
|
||||
<groupId>com.intellectualsites</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>cloud-paper</artifactId>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>papermc</id>
|
||||
<url>https://papermc.io/repo/repository/maven-public/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.intellectualsites</groupId>
|
||||
<artifactId>cloud-bukkit</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.intellectualsites</groupId>
|
||||
<artifactId>cloud-brigadier</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.destroystokyo.paper</groupId>
|
||||
<artifactId>paper-api</artifactId>
|
||||
<version>1.15.2-R0.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.destroystokyo.paper</groupId>
|
||||
<artifactId>paper-mojangapi</artifactId>
|
||||
<version>1.15.2-R0.1-SNAPSHOT</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<defaultGoal>clean package</defaultGoal>
|
||||
<finalName>CommandStuff-${project.artifactId}-${project.version}</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.2.4</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
package com.intellectualsites.commands;
|
||||
|
||||
import com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource;
|
||||
import com.destroystokyo.paper.event.brigadier.CommandRegisteredEvent;
|
||||
import com.intellectualsites.commands.brigadier.CloudBrigadierManager;
|
||||
import com.intellectualsites.commands.components.CommandComponent;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
class PaperBrigadierListener implements Listener {
|
||||
|
||||
private final CloudBrigadierManager<BukkitCommandSender, BukkitBrigadierCommandSource> brigadierManager;
|
||||
private final PaperCommandManager paperCommandManager;
|
||||
|
||||
PaperBrigadierListener(@Nonnull final PaperCommandManager paperCommandManager) throws Exception {
|
||||
this.paperCommandManager = paperCommandManager;
|
||||
this.brigadierManager = new CloudBrigadierManager<>();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onCommandRegister(@Nonnull final CommandRegisteredEvent<BukkitBrigadierCommandSource> event) {
|
||||
final CommandTree<BukkitCommandSender, BukkitCommandMeta> commandTree = this.paperCommandManager.getCommandTree();
|
||||
final CommandTree.Node<CommandComponent<BukkitCommandSender, ?>> node = commandTree.getNamedNode(event.getCommandLabel());
|
||||
if (node == null) {
|
||||
return;
|
||||
}
|
||||
event.setLiteral(this.brigadierManager.createLiteralCommandNode(node,
|
||||
event.getLiteral(),
|
||||
event.getBrigadierCommand(),
|
||||
event.getBrigadierCommand(),
|
||||
(s, p) -> s.getBukkitSender().hasPermission(p)));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
package com.intellectualsites.commands;
|
||||
|
||||
import com.intellectualsites.commands.execution.CommandExecutionCoordinator;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* Paper command manager that extends {@link BukkitCommandManager}
|
||||
*/
|
||||
public class PaperCommandManager extends BukkitCommandManager {
|
||||
|
||||
/**
|
||||
* Construct a new Paper command manager
|
||||
*
|
||||
* @param owningPlugin Plugin that is constructing the manager
|
||||
* @param commandExecutionCoordinator Coordinator provider
|
||||
* @throws Exception If the construction of the manager fails
|
||||
*/
|
||||
public PaperCommandManager(@Nonnull final Plugin owningPlugin,
|
||||
@Nonnull final Function<CommandTree<BukkitCommandSender, BukkitCommandMeta>,
|
||||
CommandExecutionCoordinator<BukkitCommandSender, BukkitCommandMeta>> commandExecutionCoordinator) throws
|
||||
Exception {
|
||||
super(owningPlugin, commandExecutionCoordinator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the brigadier listener
|
||||
*/
|
||||
public void registerBrigadier() {
|
||||
try {
|
||||
Bukkit.getPluginManager().registerEvents(new PaperBrigadierListener(this),
|
||||
this.getOwningPlugin());
|
||||
} catch (final Exception e) {
|
||||
this.getOwningPlugin().getLogger().severe("Failed to register Brigadier listener");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
//
|
||||
// MIT License
|
||||
//
|
||||
// Copyright (c) 2020 Alexander Söderberg
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
|
||||
/**
|
||||
* Paper specific implementation that extends the Bukkit implementation
|
||||
*/
|
||||
package com.intellectualsites.commands;
|
||||
4
pom.xml
4
pom.xml
|
|
@ -9,11 +9,13 @@
|
|||
<version>1.0-SNAPSHOT</version>
|
||||
<modules>
|
||||
<module>cloud-jline</module>
|
||||
<module>cloud-minecraft/cloud-bukkit</module>
|
||||
<module>cloud-core</module>
|
||||
<module>cloud-pipeline</module>
|
||||
<!-- REMOVE THIS -->
|
||||
<module>cloud-minecraft/cloud-bukkit-test</module>
|
||||
<module>cloud-minecraft/cloud-bukkit</module>
|
||||
<module>cloud-minecraft/cloud-paper</module>
|
||||
<module>cloud-minecraft/cloud-brigadier</module>
|
||||
</modules>
|
||||
<packaging>pom</packaging>
|
||||
<inceptionYear>2020</inceptionYear>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue