Store component type in the component. Add mappings to native (NMS) Brigadier types. Shorten builder names. Make the Bukkit command manager take in a generic command sender type.

This commit is contained in:
Alexander Söderberg 2020-09-15 13:36:13 +02:00
parent b8db1d3cb7
commit d144c3ea8c
No known key found for this signature in database
GPG key ID: C0207FF7EA146678
29 changed files with 524 additions and 158 deletions

View file

@ -244,7 +244,7 @@ public class Command<C extends CommandSender, M extends CommandMeta> {
* @return New builder instance with the command component inserted into the component list * @return New builder instance with the command component inserted into the component list
*/ */
@Nonnull @Nonnull
public <T> Builder<C, M> withComponent(@Nonnull final CommandComponent<C, T> component) { public <T> Builder<C, M> component(@Nonnull final CommandComponent<C, T> component) {
final List<CommandComponent<C, ?>> commandComponents = new LinkedList<>(this.commandComponents); final List<CommandComponent<C, ?>> commandComponents = new LinkedList<>(this.commandComponents);
commandComponents.add(component); commandComponents.add(component);
return new Builder<>(this.commandMeta, this.senderType, commandComponents, this.commandExecutionHandler, return new Builder<>(this.commandMeta, this.senderType, commandComponents, this.commandExecutionHandler,
@ -261,12 +261,12 @@ public class Command<C extends CommandSender, M extends CommandMeta> {
* @return New builder instance with the command component inserted into the component list * @return New builder instance with the command component inserted into the component list
*/ */
@Nonnull @Nonnull
public <T> Builder<C, M> withComponent(@Nonnull final Class<T> clazz, public <T> Builder<C, M> component(@Nonnull final Class<T> clazz,
@Nonnull final String name, @Nonnull final String name,
@Nonnull final Consumer<CommandComponent.Builder<C, T>> builderConsumer) { @Nonnull final Consumer<CommandComponent.Builder<C, T>> builderConsumer) {
final CommandComponent.Builder<C, T> builder = CommandComponent.ofType(clazz, name); final CommandComponent.Builder<C, T> builder = CommandComponent.ofType(clazz, name);
builderConsumer.accept(builder); builderConsumer.accept(builder);
return this.withComponent(builder.build()); return this.component(builder.build());
} }
/** /**
@ -276,7 +276,7 @@ public class Command<C extends CommandSender, M extends CommandMeta> {
* @return New builder instance using the command execution handler * @return New builder instance using the command execution handler
*/ */
@Nonnull @Nonnull
public Builder<C, M> withHandler(@Nonnull final CommandExecutionHandler<C> commandExecutionHandler) { public Builder<C, M> handler(@Nonnull final CommandExecutionHandler<C> commandExecutionHandler) {
return new Builder<>(this.commandMeta, this.senderType, this.commandComponents, commandExecutionHandler, return new Builder<>(this.commandMeta, this.senderType, this.commandComponents, commandExecutionHandler,
this.commandPermission); this.commandPermission);
} }

View file

@ -159,7 +159,7 @@ public abstract class CommandManager<C extends CommandSender, M extends CommandM
* @param command Command to register * @param command Command to register
* @return The command manager instance * @return The command manager instance
*/ */
public CommandManager<C, M> registerCommand(@Nonnull final Command<C, M> command) { public CommandManager<C, M> command(@Nonnull final Command<C, M> command) {
this.commandTree.insertCommand(command); this.commandTree.insertCommand(command);
return this; return this;
} }

View file

@ -107,12 +107,12 @@ public final class CommandTree<C extends CommandSender, M extends CommandMeta> {
private Optional<Command<C, M>> parseCommand(@Nonnull final CommandContext<C> commandContext, private Optional<Command<C, M>> parseCommand(@Nonnull final CommandContext<C> commandContext,
@Nonnull final Queue<String> commandQueue, @Nonnull final Queue<String> commandQueue,
@Nonnull final Node<CommandComponent<C, ?>> root) { @Nonnull final Node<CommandComponent<C, ?>> root) {
String permission = this.isPermitted(commandContext.getCommandSender(), root); String permission = this.isPermitted(commandContext.getSender(), root);
if (permission != null) { if (permission != null) {
throw new NoPermissionException(permission, commandContext.getCommandSender(), this.getChain(root) throw new NoPermissionException(permission, commandContext.getSender(), this.getChain(root)
.stream() .stream()
.map(Node::getValue) .map(Node::getValue)
.collect(Collectors.toList())); .collect(Collectors.toList()));
} }
final Optional<Command<C, M>> parsedChild = this.attemptParseUnambiguousChild(commandContext, root, commandQueue); final Optional<Command<C, M>> parsedChild = this.attemptParseUnambiguousChild(commandContext, root, commandQueue);
@ -133,10 +133,10 @@ public final class CommandTree<C extends CommandSender, M extends CommandMeta> {
.apply(root.getValue() .apply(root.getValue()
.getOwningCommand() .getOwningCommand()
.getComponents()), .getComponents()),
commandContext.getCommandSender(), this.getChain(root) commandContext.getSender(), this.getChain(root)
.stream() .stream()
.map(Node::getValue) .map(Node::getValue)
.collect(Collectors.toList())); .collect(Collectors.toList()));
} }
} else { } else {
/* Too many arguments. We have a unique path, so we can send the entire context */ /* Too many arguments. We have a unique path, so we can send the entire context */
@ -145,10 +145,10 @@ public final class CommandTree<C extends CommandSender, M extends CommandMeta> {
Objects.requireNonNull(root.getValue()) Objects.requireNonNull(root.getValue())
.getOwningCommand()) .getOwningCommand())
.getComponents()), .getComponents()),
commandContext.getCommandSender(), this.getChain(root) commandContext.getSender(), this.getChain(root)
.stream() .stream()
.map(Node::getValue) .map(Node::getValue)
.collect(Collectors.toList())); .collect(Collectors.toList()));
} }
} else { } else {
final Iterator<Node<CommandComponent<C, ?>>> childIterator = root.getChildren().iterator(); final Iterator<Node<CommandComponent<C, ?>>> childIterator = root.getChildren().iterator();
@ -165,7 +165,7 @@ public final class CommandTree<C extends CommandSender, M extends CommandMeta> {
} }
} }
/* We could not find a match */ /* We could not find a match */
throw new NoSuchCommandException(commandContext.getCommandSender(), throw new NoSuchCommandException(commandContext.getSender(),
getChain(root).stream().map(Node::getValue).collect(Collectors.toList()), getChain(root).stream().map(Node::getValue).collect(Collectors.toList()),
stringOrEmpty(commandQueue.peek())); stringOrEmpty(commandQueue.peek()));
} }
@ -180,12 +180,12 @@ public final class CommandTree<C extends CommandSender, M extends CommandMeta> {
if (children.size() == 1 && !(children.get(0).getValue() instanceof StaticComponent)) { if (children.size() == 1 && !(children.get(0).getValue() instanceof StaticComponent)) {
// The value has to be a variable // The value has to be a variable
final Node<CommandComponent<C, ?>> child = children.get(0); final Node<CommandComponent<C, ?>> child = children.get(0);
permission = this.isPermitted(commandContext.getCommandSender(), child); permission = this.isPermitted(commandContext.getSender(), child);
if (permission != null) { if (permission != null) {
throw new NoPermissionException(permission, commandContext.getCommandSender(), this.getChain(child) throw new NoPermissionException(permission, commandContext.getSender(), this.getChain(child)
.stream() .stream()
.map(Node::getValue) .map(Node::getValue)
.collect(Collectors.toList())); .collect(Collectors.toList()));
} }
if (child.getValue() != null) { if (child.getValue() != null) {
if (commandQueue.isEmpty()) { if (commandQueue.isEmpty()) {
@ -199,12 +199,12 @@ public final class CommandTree<C extends CommandSender, M extends CommandMeta> {
.apply(Objects.requireNonNull( .apply(Objects.requireNonNull(
child.getValue().getOwningCommand()) child.getValue().getOwningCommand())
.getComponents()), .getComponents()),
commandContext.getCommandSender(), this.getChain(root) commandContext.getSender(), this.getChain(root)
.stream() .stream()
.map(Node::getValue) .map(Node::getValue)
.collect(Collectors.toList())); .collect(Collectors.toList()));
} else { } else {
throw new NoSuchCommandException(commandContext.getCommandSender(), throw new NoSuchCommandException(commandContext.getSender(),
this.getChain(root) this.getChain(root)
.stream() .stream()
.map(Node::getValue) .map(Node::getValue)
@ -224,17 +224,17 @@ public final class CommandTree<C extends CommandSender, M extends CommandMeta> {
.apply(Objects.requireNonNull(child.getValue() .apply(Objects.requireNonNull(child.getValue()
.getOwningCommand()) .getOwningCommand())
.getComponents()), .getComponents()),
commandContext.getCommandSender(), this.getChain(root) commandContext.getSender(), this.getChain(root)
.stream() .stream()
.map(Node::getValue) .map(Node::getValue)
.collect( .collect(
Collectors.toList())); Collectors.toList()));
} }
} else { } else {
return this.parseCommand(commandContext, commandQueue, child); return this.parseCommand(commandContext, commandQueue, child);
} }
} else if (result.getFailure().isPresent()) { } else if (result.getFailure().isPresent()) {
throw new ComponentParseException(result.getFailure().get(), commandContext.getCommandSender(), throw new ComponentParseException(result.getFailure().get(), commandContext.getSender(),
this.getChain(child) this.getChain(child)
.stream() .stream()
.map(Node::getValue) .map(Node::getValue)
@ -264,7 +264,7 @@ public final class CommandTree<C extends CommandSender, M extends CommandMeta> {
@Nonnull final Node<CommandComponent<C, ?>> root) { @Nonnull final Node<CommandComponent<C, ?>> root) {
/* If the sender isn't allowed to access the root node, no suggestions are needed */ /* If the sender isn't allowed to access the root node, no suggestions are needed */
if (this.isPermitted(commandContext.getCommandSender(), root) != null) { if (this.isPermitted(commandContext.getSender(), root) != null) {
return Collections.emptyList(); return Collections.emptyList();
} }
final List<Node<CommandComponent<C, ?>>> children = root.getChildren(); final List<Node<CommandComponent<C, ?>>> children = root.getChildren();
@ -307,7 +307,7 @@ public final class CommandTree<C extends CommandSender, M extends CommandMeta> {
} }
final List<String> suggestions = new LinkedList<>(); final List<String> suggestions = new LinkedList<>();
for (final Node<CommandComponent<C, ?>> component : root.getChildren()) { for (final Node<CommandComponent<C, ?>> component : root.getChildren()) {
if (component.getValue() == null || this.isPermitted(commandContext.getCommandSender(), component) != null) { if (component.getValue() == null || this.isPermitted(commandContext.getSender(), component) != null) {
continue; continue;
} }
suggestions.addAll( suggestions.addAll(

View file

@ -69,6 +69,10 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
* Default value, will be empty if none was supplied * Default value, will be empty if none was supplied
*/ */
private final String defaultValue; private final String defaultValue;
/**
* The type that is produces by the component's parser
*/
private final Class<T> valueType;
private Command<C, ?> owningCommand; private Command<C, ?> owningCommand;
@ -79,9 +83,13 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
* @param name The component name * @param name The component name
* @param parser The component parser * @param parser The component parser
* @param defaultValue Default value used when no value is provided by the command sender * @param defaultValue Default value used when no value is provided by the command sender
* @param valueType Type produced by the parser
*/ */
public CommandComponent(final boolean required, @Nonnull final String name, public CommandComponent(final boolean required,
@Nonnull final ComponentParser<C, T> parser, @Nonnull final String defaultValue) { @Nonnull final String name,
@Nonnull final ComponentParser<C, T> parser,
@Nonnull final String defaultValue,
@Nonnull final Class<T> valueType) {
this.required = required; this.required = required;
this.name = Objects.requireNonNull(name, "Name may not be null"); this.name = Objects.requireNonNull(name, "Name may not be null");
if (!NAME_PATTERN.asPredicate().test(name)) { if (!NAME_PATTERN.asPredicate().test(name)) {
@ -89,6 +97,7 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
} }
this.parser = Objects.requireNonNull(parser, "Parser may not be null"); this.parser = Objects.requireNonNull(parser, "Parser may not be null");
this.defaultValue = defaultValue; this.defaultValue = defaultValue;
this.valueType = valueType;
} }
/** /**
@ -97,10 +106,13 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
* @param required Whether or not the component is required * @param required Whether or not the component is required
* @param name The component name * @param name The component name
* @param parser The component parser * @param parser The component parser
* @param valueType Type produced by the parser
*/ */
public CommandComponent(final boolean required, @Nonnull final String name, public CommandComponent(final boolean required,
@Nonnull final ComponentParser<C, T> parser) { @Nonnull final String name,
this(required, name, parser, ""); @Nonnull final ComponentParser<C, T> parser,
@Nonnull final Class<T> valueType) {
this(required, name, parser, "", valueType);
} }
/** /**
@ -115,7 +127,7 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
@Nonnull @Nonnull
public static <C extends CommandSender, T> CommandComponent.Builder<C, T> ofType(@Nonnull final Class<T> clazz, public static <C extends CommandSender, T> CommandComponent.Builder<C, T> ofType(@Nonnull final Class<T> clazz,
@Nonnull final String name) { @Nonnull final String name) {
return new Builder<>(name); return new Builder<>(clazz, name);
} }
/** /**
@ -230,6 +242,15 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
&& !this.getDefaultValue().isEmpty(); && !this.getDefaultValue().isEmpty();
} }
/**
* Get the type of this component's value
*
* @return Value type
*/
@Nonnull
public Class<T> getValueType() {
return this.valueType;
}
/** /**
* Mutable builder for {@link CommandComponent} instances * Mutable builder for {@link CommandComponent} instances
@ -239,13 +260,17 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
*/ */
public static class Builder<C extends CommandSender, T> { public static class Builder<C extends CommandSender, T> {
private final Class<T> valueType;
private final String name; private final String name;
private boolean required = true; private boolean required = true;
private ComponentParser<C, T> parser = (c, i) -> ComponentParseResult.failure( private ComponentParser<C, T> parser = (c, i) -> ComponentParseResult.failure(
new UnsupportedOperationException("No parser was specified")); new UnsupportedOperationException("No parser was specified"));
private String defaultValue = ""; private String defaultValue = "";
protected Builder(@Nonnull final String name) { protected Builder(@Nonnull final Class<T> valueType,
@Nonnull final String name) {
this.valueType = valueType;
this.name = name; this.name = name;
} }
@ -315,7 +340,7 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
*/ */
@Nonnull @Nonnull
public CommandComponent<C, T> build() { public CommandComponent<C, T> build() {
return new CommandComponent<>(this.required, this.name, this.parser, this.defaultValue); return new CommandComponent<>(this.required, this.name, this.parser, this.defaultValue, this.valueType);
} }
@Nonnull @Nonnull

View file

@ -44,7 +44,7 @@ import java.util.Set;
public final class StaticComponent<C extends CommandSender> extends CommandComponent<C, String> { public final class StaticComponent<C extends CommandSender> extends CommandComponent<C, String> {
private StaticComponent(final boolean required, @Nonnull final String name, @Nonnull final String... aliases) { private StaticComponent(final boolean required, @Nonnull final String name, @Nonnull final String... aliases) {
super(required, name, new StaticComponentParser<>(name, aliases)); super(required, name, new StaticComponentParser<>(name, aliases), String.class);
} }
/** /**

View file

@ -40,7 +40,7 @@ public final class BooleanComponent<C extends CommandSender> extends CommandComp
private BooleanComponent(final boolean required, @Nonnull final String name, private BooleanComponent(final boolean required, @Nonnull final String name,
final boolean liberal, @Nonnull final String defaultValue) { final boolean liberal, @Nonnull final String defaultValue) {
super(required, name, new BooleanParser<>(liberal), defaultValue); super(required, name, new BooleanParser<>(liberal), defaultValue, Boolean.class);
this.liberal = liberal; this.liberal = liberal;
} }
@ -100,7 +100,7 @@ public final class BooleanComponent<C extends CommandSender> extends CommandComp
private boolean liberal = false; private boolean liberal = false;
protected Builder(@Nonnull final String name) { protected Builder(@Nonnull final String name) {
super(name); super(Boolean.class, name);
} }
/** /**

View file

@ -41,7 +41,7 @@ public final class ByteComponent<C extends CommandSender> extends CommandCompone
private ByteComponent(final boolean required, @Nonnull final String name, final byte min, private ByteComponent(final boolean required, @Nonnull final String name, final byte min,
final byte max, final String defaultValue) { final byte max, final String defaultValue) {
super(required, name, new ByteParser<>(min, max), defaultValue); super(required, name, new ByteParser<>(min, max), defaultValue, Byte.class);
this.min = min; this.min = min;
this.max = max; this.max = max;
} }
@ -102,7 +102,7 @@ public final class ByteComponent<C extends CommandSender> extends CommandCompone
private byte max = Byte.MAX_VALUE; private byte max = Byte.MAX_VALUE;
protected Builder(@Nonnull final String name) { protected Builder(@Nonnull final String name) {
super(name); super(Byte.class, name);
} }
/** /**

View file

@ -37,7 +37,7 @@ public final class CharComponent<C extends CommandSender> extends CommandCompone
private CharComponent(final boolean required, @Nonnull final String name, private CharComponent(final boolean required, @Nonnull final String name,
@Nonnull final String defaultValue) { @Nonnull final String defaultValue) {
super(required, name, new CharacterParser<>(), defaultValue); super(required, name, new CharacterParser<>(), defaultValue, Character.class);
} }
/** /**
@ -94,7 +94,7 @@ public final class CharComponent<C extends CommandSender> extends CommandCompone
public static final class Builder<C extends CommandSender> extends CommandComponent.Builder<C, Character> { public static final class Builder<C extends CommandSender> extends CommandComponent.Builder<C, Character> {
protected Builder(@Nonnull final String name) { protected Builder(@Nonnull final String name) {
super(name); super(Character.class, name);
} }
/** /**

View file

@ -44,7 +44,7 @@ public final class DoubleComponent<C extends CommandSender> extends CommandCompo
final double min, final double min,
final double max, final double max,
final String defaultValue) { final String defaultValue) {
super(required, name, new DoubleParser<>(min, max), defaultValue); super(required, name, new DoubleParser<>(min, max), defaultValue, Double.class);
this.min = min; this.min = min;
this.max = max; this.max = max;
} }
@ -106,7 +106,7 @@ public final class DoubleComponent<C extends CommandSender> extends CommandCompo
private double max = Double.MAX_VALUE; private double max = Double.MAX_VALUE;
protected Builder(@Nonnull final String name) { protected Builder(@Nonnull final String name) {
super(name); super(Double.class, name);
} }
/** /**

View file

@ -48,7 +48,7 @@ public class EnumComponent<C extends CommandSender, E extends Enum<E>> extends C
final boolean required, final boolean required,
@Nonnull final String name, @Nonnull final String name,
@Nonnull final String defaultValue) { @Nonnull final String defaultValue) {
super(required, name, new EnumParser<>(enumClass), defaultValue); super(required, name, new EnumParser<>(enumClass), defaultValue, enumClass);
} }
/** /**
@ -118,7 +118,7 @@ public class EnumComponent<C extends CommandSender, E extends Enum<E>> extends C
private final Class<E> enumClass; private final Class<E> enumClass;
protected Builder(@Nonnull final String name, @Nonnull final Class<E> enumClass) { protected Builder(@Nonnull final String name, @Nonnull final Class<E> enumClass) {
super(name); super(enumClass, name);
this.enumClass = enumClass; this.enumClass = enumClass;
} }

View file

@ -44,7 +44,7 @@ public final class FloatComponent<C extends CommandSender> extends CommandCompon
final float min, final float min,
final float max, final float max,
final String defaultValue) { final String defaultValue) {
super(required, name, new FloatParser<>(min, max), defaultValue); super(required, name, new FloatParser<>(min, max), defaultValue, Float.class);
this.min = min; this.min = min;
this.max = max; this.max = max;
} }
@ -106,7 +106,7 @@ public final class FloatComponent<C extends CommandSender> extends CommandCompon
private float max = Float.MAX_VALUE; private float max = Float.MAX_VALUE;
protected Builder(@Nonnull final String name) { protected Builder(@Nonnull final String name) {
super(name); super(Float.class, name);
} }
/** /**

View file

@ -44,7 +44,7 @@ public final class IntegerComponent<C extends CommandSender> extends CommandComp
final int min, final int min,
final int max, final int max,
final String defaultValue) { final String defaultValue) {
super(required, name, new IntegerParser<>(min, max), defaultValue); super(required, name, new IntegerParser<>(min, max), defaultValue, Integer.class);
this.min = min; this.min = min;
this.max = max; this.max = max;
} }
@ -106,7 +106,7 @@ public final class IntegerComponent<C extends CommandSender> extends CommandComp
private int max = Integer.MAX_VALUE; private int max = Integer.MAX_VALUE;
protected Builder(@Nonnull final String name) { protected Builder(@Nonnull final String name) {
super(name); super(Integer.class, name);
} }
/** /**

View file

@ -44,7 +44,7 @@ public final class LongComponent<C extends CommandSender> extends CommandCompone
final long min, final long min,
final long max, final long max,
final String defaultValue) { final String defaultValue) {
super(required, name, new LongParser<>(min, max), defaultValue); super(required, name, new LongParser<>(min, max), defaultValue, Long.class);
this.min = min; this.min = min;
this.max = max; this.max = max;
} }
@ -106,7 +106,7 @@ public final class LongComponent<C extends CommandSender> extends CommandCompone
private long max = Long.MAX_VALUE; private long max = Long.MAX_VALUE;
protected Builder(@Nonnull final String name) { protected Builder(@Nonnull final String name) {
super(name); super(Long.class, name);
} }
/** /**

View file

@ -44,7 +44,7 @@ public final class ShortComponent<C extends CommandSender> extends CommandCompon
final short min, final short min,
final short max, final short max,
final String defaultValue) { final String defaultValue) {
super(required, name, new ShortParser<>(min, max), defaultValue); super(required, name, new ShortParser<>(min, max), defaultValue, Short.class);
this.min = min; this.min = min;
this.max = max; this.max = max;
} }
@ -106,7 +106,7 @@ public final class ShortComponent<C extends CommandSender> extends CommandCompon
private short max = Short.MAX_VALUE; private short max = Short.MAX_VALUE;
protected Builder(@Nonnull final String name) { protected Builder(@Nonnull final String name) {
super(name); super(Short.class, name);
} }
/** /**

View file

@ -46,7 +46,7 @@ public final class StringComponent<C extends CommandSender> extends CommandCompo
@Nonnull final StringMode stringMode, @Nonnull final StringMode stringMode,
@Nonnull final String defaultValue, @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); super(required, name, new StringParser<>(stringMode, suggestionsProvider), defaultValue, String.class);
this.stringMode = stringMode; this.stringMode = stringMode;
} }
@ -124,7 +124,7 @@ public final class StringComponent<C extends CommandSender> extends CommandCompo
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) { protected Builder(@Nonnull final String name) {
super(name); super(String.class, name);
} }
/** /**

View file

@ -55,7 +55,7 @@ public final class CommandContext<C extends CommandSender> {
* @return Command sender * @return Command sender
*/ */
@Nonnull @Nonnull
public C getCommandSender() { public C getSender() {
return this.commandSender; return this.commandSender;
} }
@ -77,6 +77,7 @@ public final class CommandContext<C extends CommandSender> {
* @param <T> Value type * @param <T> Value type
* @return Value * @return Value
*/ */
@Nonnull
public <T> Optional<T> get(@Nonnull final String key) { public <T> Optional<T> get(@Nonnull final String key) {
final Object value = this.internalStorage.get(key); final Object value = this.internalStorage.get(key);
if (value != null) { if (value != null) {
@ -87,4 +88,22 @@ public final class CommandContext<C extends CommandSender> {
} }
} }
/**
* Get a required argument from the context
*
* @param key Argument key
* @param <T> Argument type
* @return Argument
* @throws NullPointerException If no such argument is stored
*/
@Nonnull
@SuppressWarnings("unchecked")
public <T> T getRequired(@Nonnull final String key) {
final Object value = this.internalStorage.get(key);
if (value == null) {
throw new NullPointerException("No such object stored in the context: " + key);
}
return (T) value;
}
} }

View file

@ -42,9 +42,9 @@ public class CommandPreProcessorTest {
@BeforeAll @BeforeAll
static void newTree() { static void newTree() {
manager = new TestCommandManager(); manager = new TestCommandManager();
manager.registerCommand(manager.commandBuilder("test", SimpleCommandMeta.empty()) manager.command(manager.commandBuilder("test", SimpleCommandMeta.empty())
.withComponent(EnumComponent.required(SampleEnum.class, "enum")) .component(EnumComponent.required(SampleEnum.class, "enum"))
.withHandler( .handler(
commandContext -> System.out.printf("enum = %s | integer = %d\n", commandContext -> System.out.printf("enum = %s | integer = %d\n",
commandContext.<SampleEnum>get( commandContext.<SampleEnum>get(
"enum").orElse( "enum").orElse(

View file

@ -38,8 +38,8 @@ class CommandTest {
@Test @Test
void ensureOrdering() { void ensureOrdering() {
Assertions.assertThrows(IllegalArgumentException.class, () -> Assertions.assertThrows(IllegalArgumentException.class, () ->
Command.newBuilder("test", SimpleCommandMeta.empty()).withComponent(StaticComponent.optional("something")) Command.newBuilder("test", SimpleCommandMeta.empty()).component(StaticComponent.optional("something"))
.withComponent(StaticComponent.required("somethingelse")).build()); .component(StaticComponent.required("somethingelse")).build());
} }
} }

View file

@ -46,14 +46,14 @@ class CommandTreeTest {
@BeforeAll @BeforeAll
static void newTree() { static void newTree() {
commandManager = new TestCommandManager(); commandManager = new TestCommandManager();
commandManager.registerCommand(commandManager.commandBuilder("test", SimpleCommandMeta.empty()) commandManager.command(commandManager.commandBuilder("test", SimpleCommandMeta.empty())
.withComponent(StaticComponent.required("one")).build()) .component(StaticComponent.required("one")).build())
.registerCommand(commandManager.commandBuilder("test", SimpleCommandMeta.empty()) .command(commandManager.commandBuilder("test", SimpleCommandMeta.empty())
.withComponent(StaticComponent.required("two")).withPermission("no").build()) .component(StaticComponent.required("two")).withPermission("no").build())
.registerCommand(commandManager.commandBuilder("test", Collections.singleton("other"), .command(commandManager.commandBuilder("test", Collections.singleton("other"),
SimpleCommandMeta.empty()) SimpleCommandMeta.empty())
.withComponent(StaticComponent.required("opt", "öpt")) .component(StaticComponent.required("opt", "öpt"))
.withComponent(IntegerComponent .component(IntegerComponent
.optional("num", EXPECTED_INPUT_NUMBER)) .optional("num", EXPECTED_INPUT_NUMBER))
.build()); .build());
} }

View file

@ -75,13 +75,13 @@ public class JLineCommandManager extends CommandManager<JLineCommandSender, Simp
.appName("Test") .appName("Test")
.build(); .build();
boolean[] shouldStop = new boolean[]{false}; boolean[] shouldStop = new boolean[]{false};
jLineCommandManager.registerCommand( jLineCommandManager.command(
jLineCommandManager.commandBuilder("stop", SimpleCommandMeta.empty()) jLineCommandManager.commandBuilder("stop", SimpleCommandMeta.empty())
.withHandler(commandContext -> .handler(commandContext ->
shouldStop[0] = true) shouldStop[0] = true)
.build()) .build())
.registerCommand(jLineCommandManager.commandBuilder("echo", SimpleCommandMeta.empty()) .command(jLineCommandManager.commandBuilder("echo", SimpleCommandMeta.empty())
.withComponent(String.class, "string", builder -> .component(String.class, "string", builder ->
builder.asRequired() builder.asRequired()
.withParser(((commandContext, inputQueue) -> { .withParser(((commandContext, inputQueue) -> {
final StringBuilder stringBuilder = final StringBuilder stringBuilder =
@ -95,17 +95,17 @@ public class JLineCommandManager extends CommandManager<JLineCommandSender, Simp
return ComponentParseResult.success( return ComponentParseResult.success(
stringBuilder.toString()); stringBuilder.toString());
})).build()) })).build())
.withHandler(commandContext -> commandContext.get("string") .handler(commandContext -> commandContext.get("string")
.ifPresent( .ifPresent(
System.out::println)) System.out::println))
.build()) .build())
.registerCommand(jLineCommandManager.commandBuilder("test", SimpleCommandMeta.empty()) .command(jLineCommandManager.commandBuilder("test", SimpleCommandMeta.empty())
.withComponent(StaticComponent.required("one")) .component(StaticComponent.required("one"))
.withHandler(commandContext -> System.out.println("Test (1)")) .handler(commandContext -> System.out.println("Test (1)"))
.build()) .build())
.registerCommand(jLineCommandManager.commandBuilder("test", SimpleCommandMeta.empty()) .command(jLineCommandManager.commandBuilder("test", SimpleCommandMeta.empty())
.withComponent(StaticComponent.required("two")) .component(StaticComponent.required("two"))
.withHandler(commandContext -> System.out.println("Test (2)")) .handler(commandContext -> System.out.println("Test (2)"))
.build()); .build());
System.out.println("Ready..."); System.out.println("Ready...");
while (!shouldStop[0]) { while (!shouldStop[0]) {

View file

@ -52,6 +52,7 @@ import javax.annotation.Nonnull;
import java.util.Map; import java.util.Map;
import java.util.function.BiPredicate; import java.util.function.BiPredicate;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier;
/** /**
* Manager used to map cloud {@link com.intellectualsites.commands.Command} * Manager used to map cloud {@link com.intellectualsites.commands.Command}
@ -67,12 +68,14 @@ public final class CloudBrigadierManager<C extends CommandSender, S> {
private final Map<Class<?>, Function<? extends CommandComponent<C, ?>, private final Map<Class<?>, Function<? extends CommandComponent<C, ?>,
? extends ArgumentType<?>>> mappers; ? extends ArgumentType<?>>> mappers;
private final Map<Class<?>, Supplier<ArgumentType<?>>> defaultArgumentTypeSuppliers;
/** /**
* Create a new cloud brigadier manager * Create a new cloud brigadier manager
*/ */
public CloudBrigadierManager() { public CloudBrigadierManager() {
this.mappers = Maps.newHashMap(); this.mappers = Maps.newHashMap();
this.defaultArgumentTypeSuppliers = Maps.newHashMap();
this.registerInternalMappings(); this.registerInternalMappings();
} }
@ -106,9 +109,6 @@ public final class CloudBrigadierManager<C extends CommandSender, S> {
}, component -> { }, component -> {
final boolean hasMin = component.getMin() != Integer.MIN_VALUE; final boolean hasMin = component.getMin() != Integer.MIN_VALUE;
final boolean hasMax = component.getMax() != Integer.MAX_VALUE; final boolean hasMax = component.getMax() != Integer.MAX_VALUE;
System.out.println("Constructing new IntegerArgumentType with min " + hasMin + " | max " + hasMax);
if (hasMin) { if (hasMin) {
return IntegerArgumentType.integer(component.getMin(), component.getMax()); return IntegerArgumentType.integer(component.getMin(), component.getMax());
} else if (hasMax) { } else if (hasMax) {
@ -150,8 +150,6 @@ public final class CloudBrigadierManager<C extends CommandSender, S> {
this.registerMapping(new TypeToken<StringComponent<C>>() { this.registerMapping(new TypeToken<StringComponent<C>>() {
}, component -> { }, component -> {
switch (component.getStringMode()) { switch (component.getStringMode()) {
case SINGLE:
return StringArgumentType.word();
case QUOTED: case QUOTED:
return StringArgumentType.string(); return StringArgumentType.string();
case GREEDY: case GREEDY:
@ -177,6 +175,17 @@ public final class CloudBrigadierManager<C extends CommandSender, S> {
this.mappers.put(componentType.getRawType(), mapper); this.mappers.put(componentType.getRawType(), mapper);
} }
/**
* Register a default mapping to between a class and a Brigadier argument type
*
* @param clazz Type to map
* @param supplier Supplier that supplies the argument type
*/
public void registerDefaultArgumentTypeSupplier(@Nonnull final Class<?> clazz,
@Nonnull final Supplier<ArgumentType<?>> supplier) {
this.defaultArgumentTypeSuppliers.put(clazz, supplier);
}
/** /**
* Get a Brigadier {@link ArgumentType} from a cloud {@link CommandComponent} * Get a Brigadier {@link ArgumentType} from a cloud {@link CommandComponent}
* *
@ -199,6 +208,11 @@ public final class CloudBrigadierManager<C extends CommandSender, S> {
@Nonnull @Nonnull
private <T, K extends CommandComponent<C, T>> ArgumentType<?> createDefaultMapper(@Nonnull final CommandComponent<C, T> private <T, K extends CommandComponent<C, T>> ArgumentType<?> createDefaultMapper(@Nonnull final CommandComponent<C, T>
component) { component) {
final Supplier<ArgumentType<?>> argumentTypeSupplier = this.defaultArgumentTypeSuppliers.get(component.getValueType());
if (argumentTypeSupplier != null) {
return argumentTypeSupplier.get();
}
System.err.printf("Found not native mapping for '%s'\n", component.getValueType().getCanonicalName());
return StringArgumentType.string(); return StringArgumentType.string();
} }

View file

@ -24,18 +24,24 @@
package com.intellectualsites.commands; package com.intellectualsites.commands;
import com.intellectualsites.commands.components.StaticComponent; import com.intellectualsites.commands.components.StaticComponent;
import com.intellectualsites.commands.components.parser.ComponentParseResult;
import com.intellectualsites.commands.components.standard.EnumComponent; import com.intellectualsites.commands.components.standard.EnumComponent;
import com.intellectualsites.commands.components.standard.IntegerComponent; import com.intellectualsites.commands.components.standard.IntegerComponent;
import com.intellectualsites.commands.components.standard.StringComponent; import com.intellectualsites.commands.components.standard.StringComponent;
import com.intellectualsites.commands.execution.CommandExecutionCoordinator; import com.intellectualsites.commands.execution.CommandExecutionCoordinator;
import com.intellectualsites.commands.parsers.WorldComponent;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public final class BukkitTest extends JavaPlugin { public final class BukkitTest extends JavaPlugin {
@ -46,52 +52,89 @@ public final class BukkitTest extends JavaPlugin {
@Override @Override
public void onEnable() { public void onEnable() {
try { try {
final PaperCommandManager commandManager = new PaperCommandManager(this, final PaperCommandManager<BukkitCommandSender> mgr = new PaperCommandManager<>(this,
CommandExecutionCoordinator.simpleCoordinator()); CommandExecutionCoordinator.simpleCoordinator());
commandManager.registerBrigadier(); mgr.registerBrigadier();
commandManager.registerCommand(commandManager.commandBuilder("gamemode", mgr.command(mgr.commandBuilder("gamemode",
Collections.singleton("gajmöde"), Collections.singleton("gajmöde"),
BukkitCommandMetaBuilder.builder() BukkitCommandMetaBuilder.builder()
.withDescription("Your ugli") .withDescription("Your ugli")
.build()) .build())
.withComponent(EnumComponent.required(GameMode.class, "gamemode")) .component(EnumComponent.required(GameMode.class, "gamemode"))
.withComponent(StringComponent.<BukkitCommandSender>newBuilder("player") .component(StringComponent.<BukkitCommandSender>newBuilder("player")
.withSuggestionsProvider((v1, v2) -> { .withSuggestionsProvider((v1, v2) -> {
final List<String> suggestions = final List<String> suggestions =
new ArrayList<>( new ArrayList<>(
Bukkit.getOnlinePlayers() Bukkit.getOnlinePlayers()
.stream() .stream()
.map(Player::getName) .map(Player::getName)
.collect(Collectors.toList())); .collect(Collectors.toList()));
suggestions.add("dog"); suggestions.add("dog");
suggestions.add("cat"); suggestions.add("cat");
return suggestions; return suggestions;
}).build()) }).build())
.withHandler(c -> c.getCommandSender() .handler(c -> c.getSender()
.asPlayer() .asPlayer()
.setGameMode(c.<GameMode>get("gamemode") .setGameMode(c.<GameMode>get("gamemode")
.orElse(GameMode.SURVIVAL))) .orElse(GameMode.SURVIVAL)))
.build()) .build())
.registerCommand(commandManager.commandBuilder("kenny") .command(mgr.commandBuilder("kenny")
.withComponent(StaticComponent.required("sux")) .component(StaticComponent.required("sux"))
.withComponent(IntegerComponent .component(IntegerComponent
.<BukkitCommandSender>newBuilder("perc") .<BukkitCommandSender>newBuilder("perc")
.withMin(PERC_MIN).withMax(PERC_MAX).build()) .withMin(PERC_MIN).withMax(PERC_MAX).build())
.withHandler(context -> { .handler(context -> {
context.getCommandSender().asPlayer().sendMessage(String.format( context.getSender().asPlayer().sendMessage(String.format(
"Kenny sux %d%%", "Kenny sux %d%%",
context.<Integer>get("perc").orElse(PERC_MIN) context.<Integer>get("perc").orElse(PERC_MIN)
)); ));
}) })
.build()) .build())
.registerCommand(commandManager.commandBuilder("test") .command(mgr.commandBuilder("test")
.withComponent(StaticComponent.required("one")) .component(StaticComponent.required("one"))
.withHandler(c -> c.getCommandSender().sendMessage("One!")) .handler(c -> c.getSender().sendMessage("One!"))
.build()) .build())
.registerCommand(commandManager.commandBuilder("test") .command(mgr.commandBuilder("test")
.withComponent(StaticComponent.required("two")) .component(StaticComponent.required("two"))
.withHandler(c -> c.getCommandSender().sendMessage("Two!")) .handler(c -> c.getSender().sendMessage("Two!"))
.build()); .build())
.command(mgr.commandBuilder("uuidtest")
.component(UUID.class, "uuid", builder -> builder
.asRequired()
.withParser((c, i) -> {
final String string = i.peek();
try {
final UUID uuid = UUID.fromString(string);
i.remove();
return ComponentParseResult.success(uuid);
} catch (final Exception e) {
return ComponentParseResult.failure(e);
}
}).build())
.handler(c -> c.getSender()
.sendMessage(String.format("UUID: %s\n", c.<UUID>get("uuid").orElse(null))))
.build())
.command(mgr.commandBuilder("give")
.component(EnumComponent.required(Material.class, "material"))
.component(IntegerComponent.required("amount"))
.handler(c -> {
final Material material = c.getRequired("material");
final int amount = c.getRequired("amount");
final ItemStack itemStack = new ItemStack(material, amount);
c.getSender().asPlayer().getInventory().addItem(itemStack);
c.getSender().sendMessage("You've been given stuff, bro.");
})
.build())
.command(mgr.commandBuilder("worldtp", BukkitCommandMetaBuilder.builder()
.withDescription("Teleport to a world")
.build())
.component(WorldComponent.required("world"))
.handler(c -> {
final World world = c.getRequired("world");
c.getSender().asPlayer().teleport(world.getSpawnLocation());
c.getSender().sendMessage("Teleported.");
})
.build());
} catch (final Exception e) { } catch (final Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View file

@ -32,15 +32,15 @@ import org.bukkit.plugin.Plugin;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.util.List; import java.util.List;
final class BukkitCommand extends org.bukkit.command.Command implements PluginIdentifiableCommand { final class BukkitCommand<C extends BukkitCommandSender> extends org.bukkit.command.Command implements PluginIdentifiableCommand {
private final CommandComponent<BukkitCommandSender, ?> command; private final CommandComponent<C, ?> command;
private final BukkitCommandManager bukkitCommandManager; private final BukkitCommandManager<C> bukkitCommandManager;
private final com.intellectualsites.commands.Command<BukkitCommandSender, BukkitCommandMeta> cloudCommand; private final com.intellectualsites.commands.Command<C, BukkitCommandMeta> cloudCommand;
BukkitCommand(@Nonnull final com.intellectualsites.commands.Command<BukkitCommandSender, BukkitCommandMeta> cloudCommand, BukkitCommand(@Nonnull final com.intellectualsites.commands.Command<C, BukkitCommandMeta> cloudCommand,
@Nonnull final CommandComponent<BukkitCommandSender, ?> command, @Nonnull final CommandComponent<C, ?> command,
@Nonnull final BukkitCommandManager bukkitCommandManager) { @Nonnull final BukkitCommandManager<C> bukkitCommandManager) {
super(command.getName()); super(command.getName());
this.command = command; this.command = command;
this.bukkitCommandManager = bukkitCommandManager; this.bukkitCommandManager = bukkitCommandManager;
@ -54,7 +54,7 @@ final class BukkitCommand extends org.bukkit.command.Command implements PluginId
for (final String string : strings) { for (final String string : strings) {
builder.append(" ").append(string); builder.append(" ").append(string);
} }
this.bukkitCommandManager.executeCommand(BukkitCommandSender.of(commandSender), builder.toString()) this.bukkitCommandManager.executeCommand((C) BukkitCommandSender.of(commandSender), builder.toString())
.whenComplete(((commandResult, throwable) -> { .whenComplete(((commandResult, throwable) -> {
if (throwable != null) { if (throwable != null) {
commandSender.sendMessage(ChatColor.RED + throwable.getCause().getMessage()); commandSender.sendMessage(ChatColor.RED + throwable.getCause().getMessage());
@ -79,7 +79,7 @@ final class BukkitCommand extends org.bukkit.command.Command implements PluginId
for (final String string : args) { for (final String string : args) {
builder.append(" ").append(string); builder.append(" ").append(string);
} }
return this.bukkitCommandManager.suggest(BukkitCommandSender.of(sender), builder.toString()); return this.bukkitCommandManager.suggest((C) BukkitCommandSender.of(sender), builder.toString());
} }
@Override @Override

View file

@ -23,7 +23,10 @@
// //
package com.intellectualsites.commands; package com.intellectualsites.commands;
import com.google.common.reflect.TypeToken;
import com.intellectualsites.commands.execution.CommandExecutionCoordinator; import com.intellectualsites.commands.execution.CommandExecutionCoordinator;
import com.intellectualsites.commands.parsers.WorldComponent;
import org.bukkit.World;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
@ -33,7 +36,7 @@ import java.util.function.Function;
* Command manager for the Bukkit platform, using {@link BukkitCommandSender} as the * Command manager for the Bukkit platform, using {@link BukkitCommandSender} as the
* command sender type * command sender type
*/ */
public class BukkitCommandManager extends CommandManager<BukkitCommandSender, BukkitCommandMeta> { public class BukkitCommandManager<C extends BukkitCommandSender> extends CommandManager<C, BukkitCommandMeta> {
private final Plugin owningPlugin; private final Plugin owningPlugin;
@ -45,12 +48,15 @@ public class BukkitCommandManager extends CommandManager<BukkitCommandSender, Bu
* @throws Exception If the construction of the manager fails * @throws Exception If the construction of the manager fails
*/ */
public BukkitCommandManager(@Nonnull final Plugin owningPlugin, public BukkitCommandManager(@Nonnull final Plugin owningPlugin,
@Nonnull final Function<CommandTree<BukkitCommandSender, BukkitCommandMeta>, @Nonnull final Function<CommandTree<C, BukkitCommandMeta>,
CommandExecutionCoordinator<BukkitCommandSender, BukkitCommandMeta>> commandExecutionCoordinator) CommandExecutionCoordinator<C, BukkitCommandMeta>> commandExecutionCoordinator)
throws Exception { throws Exception {
super(commandExecutionCoordinator, new BukkitPluginRegistrationHandler()); super(commandExecutionCoordinator, new BukkitPluginRegistrationHandler());
((BukkitPluginRegistrationHandler) this.getCommandRegistrationHandler()).initialize(this); ((BukkitPluginRegistrationHandler) this.getCommandRegistrationHandler()).initialize(this);
this.owningPlugin = owningPlugin; this.owningPlugin = owningPlugin;
/* Register Bukkit parsers */
this.getParserRegistry().registerParserSupplier(TypeToken.of(World.class), params -> new WorldComponent.WorldParser<>());
} }
/** /**

View file

@ -37,7 +37,12 @@ public abstract class BukkitCommandSender implements CommandSender {
private final org.bukkit.command.CommandSender internalSender; private final org.bukkit.command.CommandSender internalSender;
BukkitCommandSender(@Nonnull final org.bukkit.command.CommandSender internalSender) { /**
* Create a new command sender from a Bukkit {@link CommandSender}
*
* @param internalSender Bukkit command sender
*/
public BukkitCommandSender(@Nonnull final org.bukkit.command.CommandSender internalSender) {
this.internalSender = internalSender; this.internalSender = internalSender;
} }

View file

@ -0,0 +1,174 @@
//
// 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.parsers;
import com.intellectualsites.commands.BukkitCommandSender;
import com.intellectualsites.commands.components.CommandComponent;
import com.intellectualsites.commands.components.parser.ComponentParseResult;
import com.intellectualsites.commands.components.parser.ComponentParser;
import com.intellectualsites.commands.context.CommandContext;
import org.bukkit.Bukkit;
import org.bukkit.World;
import javax.annotation.Nonnull;
import java.util.List;
import java.util.Queue;
import java.util.stream.Collectors;
/**
* cloud component type that parses Bukkit {@link org.bukkit.World worlds}
*
* @param <C> Command sender type
*/
public class WorldComponent<C extends BukkitCommandSender> extends CommandComponent<C, World> {
protected WorldComponent(final boolean required,
@Nonnull final String name,
@Nonnull final String defaultValue) {
super(required, name, new WorldParser<>(), defaultValue, World.class);
}
/**
* Create a new builder
*
* @param name Name of the component
* @param <C> Command sender type
* @return Created builder
*/
@Nonnull
public static <C extends BukkitCommandSender> CommandComponent.Builder<C, World> newBuilder(@Nonnull final String name) {
return new WorldComponent.Builder<>(name);
}
/**
* Create a new required component
*
* @param name Component name
* @param <C> Command sender type
* @return Created component
*/
@Nonnull
public static <C extends BukkitCommandSender> CommandComponent<C, World> required(@Nonnull final String name) {
return WorldComponent.<C>newBuilder(name).asRequired().build();
}
/**
* Create a new optional component
*
* @param name Component name
* @param <C> Command sender type
* @return Created component
*/
@Nonnull
public static <C extends BukkitCommandSender> CommandComponent<C, World> optional(@Nonnull final String name) {
return WorldComponent.<C>newBuilder(name).asOptional().build();
}
/**
* Create a new optional component with a default value
*
* @param name Component name
* @param defaultValue Default value
* @param <C> Command sender type
* @return Created component
*/
@Nonnull
public static <C extends BukkitCommandSender> CommandComponent<C, World> optional(@Nonnull final String name,
@Nonnull final String defaultValue) {
return WorldComponent.<C>newBuilder(name).asOptionalWithDefault(defaultValue).build();
}
public static final class Builder<C extends BukkitCommandSender> extends CommandComponent.Builder<C, World> {
protected Builder(@Nonnull final String name) {
super(World.class, name);
}
@Nonnull
@Override
public CommandComponent<C, World> build() {
return new WorldComponent<>(this.isRequired(), this.getName(), this.getDefaultValue());
}
}
public static final class WorldParser<C extends BukkitCommandSender> implements ComponentParser<C, World> {
@Nonnull
@Override
public ComponentParseResult<World> parse(@Nonnull final CommandContext<C> commandContext,
@Nonnull final Queue<String> inputQueue) {
final String input = inputQueue.peek();
if (input == null) {
return ComponentParseResult.failure(new NullPointerException("No input was provided"));
}
final World world = Bukkit.getWorld(input);
if (world == null) {
return ComponentParseResult.failure(new WorldParseException(input));
}
inputQueue.remove();
return ComponentParseResult.success(world);
}
@Nonnull
@Override
public List<String> suggestions(@Nonnull final CommandContext<C> commandContext, @Nonnull final String input) {
return Bukkit.getWorlds().stream().map(World::getName).collect(Collectors.toList());
}
}
public static final class WorldParseException extends IllegalArgumentException {
private final String input;
/**
* Construct a new WorldParseException
*
* @param input Input
*/
public WorldParseException(@Nonnull final String input) {
this.input = input;
}
/**
* Get the input provided by the sender
*
* @return Input
*/
public String getInput() {
return this.input;
}
@Override
public String getMessage() {
return String.format("'%s' is not a valid Minecraft world", this.input);
}
}
}

View file

@ -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.
//
/**
* Bukkit specific command components
*/
package com.intellectualsites.commands.parsers;

View file

@ -27,10 +27,19 @@ import com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource;
import com.destroystokyo.paper.event.brigadier.CommandRegisteredEvent; import com.destroystokyo.paper.event.brigadier.CommandRegisteredEvent;
import com.intellectualsites.commands.brigadier.CloudBrigadierManager; import com.intellectualsites.commands.brigadier.CloudBrigadierManager;
import com.intellectualsites.commands.components.CommandComponent; import com.intellectualsites.commands.components.CommandComponent;
import com.mojang.brigadier.arguments.ArgumentType;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.UUID;
class PaperBrigadierListener implements Listener { class PaperBrigadierListener implements Listener {
@ -40,6 +49,49 @@ class PaperBrigadierListener implements Listener {
PaperBrigadierListener(@Nonnull final PaperCommandManager paperCommandManager) throws Exception { PaperBrigadierListener(@Nonnull final PaperCommandManager paperCommandManager) throws Exception {
this.paperCommandManager = paperCommandManager; this.paperCommandManager = paperCommandManager;
this.brigadierManager = new CloudBrigadierManager<>(); this.brigadierManager = new CloudBrigadierManager<>();
/* Register default mappings */
final String version = Bukkit.getServer().getClass().getPackage().getName();
final String nms = version.substring(version.lastIndexOf(".") + 1);
try {
/* Map UUID */
this.mapSimpleNMS(UUID.class, this.getNMSArgument("UUID", nms).getConstructor());
/* Map World */
this.mapSimpleNMS(World.class, this.getNMSArgument("Dimension", nms).getConstructor());
/* Map Enchantment */
this.mapSimpleNMS(Enchantment.class, this.getNMSArgument("Enchantment", nms).getConstructor());
/* Map EntityType */
this.mapSimpleNMS(EntityType.class, this.getNMSArgument("EntitySummon", nms).getConstructor());
/* Map Material */
this.mapSimpleNMS(Material.class, this.getNMSArgument("ItemStack", nms).getConstructor());
} catch (final Exception e) {
this.paperCommandManager.getOwningPlugin()
.getLogger()
.warning("Failed to map Bukkit types to NMS argument types");
}
}
@Nonnull
private Class<?> getNMSArgument(@Nonnull final String argument, @Nonnull final String nms) throws Exception {
return Class.forName(String.format("net.minecraft.server.%s.Argument%s", nms, argument));
}
private void mapSimpleNMS(@Nonnull final Class<?> type,
@Nonnull final Constructor<?> constructor) {
try {
this.brigadierManager.registerDefaultArgumentTypeSupplier(type, () -> {
try {
return (ArgumentType<?>) constructor.newInstance();
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
return null;
});
} catch (final Exception e) {
this.paperCommandManager.getOwningPlugin()
.getLogger()
.warning(String.format("Failed to map '%s' to a Mojang serializable argument type",
type.getCanonicalName()));
}
} }
@EventHandler @EventHandler

View file

@ -33,7 +33,7 @@ import java.util.function.Function;
/** /**
* Paper command manager that extends {@link BukkitCommandManager} * Paper command manager that extends {@link BukkitCommandManager}
*/ */
public class PaperCommandManager extends BukkitCommandManager { public class PaperCommandManager<C extends BukkitCommandSender> extends BukkitCommandManager<C> {
/** /**
* Construct a new Paper command manager * Construct a new Paper command manager
@ -43,8 +43,8 @@ public class PaperCommandManager extends BukkitCommandManager {
* @throws Exception If the construction of the manager fails * @throws Exception If the construction of the manager fails
*/ */
public PaperCommandManager(@Nonnull final Plugin owningPlugin, public PaperCommandManager(@Nonnull final Plugin owningPlugin,
@Nonnull final Function<CommandTree<BukkitCommandSender, BukkitCommandMeta>, @Nonnull final Function<CommandTree<C, BukkitCommandMeta>,
CommandExecutionCoordinator<BukkitCommandSender, BukkitCommandMeta>> commandExecutionCoordinator) throws CommandExecutionCoordinator<C, BukkitCommandMeta>> commandExecutionCoordinator) throws
Exception { Exception {
super(owningPlugin, commandExecutionCoordinator); super(owningPlugin, commandExecutionCoordinator);
} }