Add limited support for completions, add .editorconfig and reformat.
This commit is contained in:
parent
10aba61110
commit
762bdb7ff4
22 changed files with 676 additions and 203 deletions
|
|
@ -29,7 +29,13 @@ import com.intellectualsites.commands.execution.CommandExecutionHandler;
|
|||
import com.intellectualsites.commands.sender.CommandSender;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.*;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* A command consists out of a chain of {@link com.intellectualsites.commands.components.CommandComponent command components}.
|
||||
|
|
@ -38,10 +44,13 @@ import java.util.*;
|
|||
*/
|
||||
public class Command<C extends CommandSender> {
|
||||
|
||||
private final CommandComponent<C, ?>[] components;
|
||||
private final CommandExecutionHandler<C> commandExecutionHandler;
|
||||
@Nonnull private final CommandComponent<C, ?>[] components;
|
||||
@Nonnull private final CommandExecutionHandler<C> commandExecutionHandler;
|
||||
@Nullable private final Class<? extends C> senderType;
|
||||
|
||||
protected Command(@Nonnull final CommandComponent<C, ?>[] commandComponents, @Nonnull final CommandExecutionHandler<C> commandExecutionHandler) {
|
||||
protected Command(@Nonnull final CommandComponent<C, ?>[] commandComponents,
|
||||
@Nonnull final CommandExecutionHandler<C> commandExecutionHandler,
|
||||
@Nullable final Class<? extends C> senderType) {
|
||||
this.components = Objects.requireNonNull(commandComponents, "Command components may not be null");
|
||||
if (this.components.length == 0) {
|
||||
throw new IllegalArgumentException("At least one command component is required");
|
||||
|
|
@ -50,12 +59,15 @@ public class Command<C extends CommandSender> {
|
|||
boolean foundOptional = false;
|
||||
for (final CommandComponent<C, ?> component : this.components) {
|
||||
if (foundOptional && component.isRequired()) {
|
||||
throw new IllegalArgumentException(String.format("Command component '%s' cannot be placed after an optional component", component.getName()));
|
||||
throw new IllegalArgumentException(
|
||||
String.format("Command component '%s' cannot be placed after an optional component",
|
||||
component.getName()));
|
||||
} else if (!component.isRequired()) {
|
||||
foundOptional = true;
|
||||
}
|
||||
}
|
||||
this.commandExecutionHandler = commandExecutionHandler;
|
||||
this.senderType = senderType;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -66,8 +78,8 @@ public class Command<C extends CommandSender> {
|
|||
*/
|
||||
@Nonnull
|
||||
public static <C extends CommandSender> Builder<C> newBuilder(@Nonnull final String commandName) {
|
||||
return new Builder<>(Collections.singletonList(StaticComponent.required(commandName)),
|
||||
new CommandExecutionHandler.NullCommandExecutionHandler<>());
|
||||
return new Builder<>(null, Collections.singletonList(StaticComponent.required(commandName)),
|
||||
new CommandExecutionHandler.NullCommandExecutionHandler<>());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -75,7 +87,8 @@ public class Command<C extends CommandSender> {
|
|||
*
|
||||
* @return Copy of the command component array
|
||||
*/
|
||||
@Nonnull @SuppressWarnings("ALL")
|
||||
@Nonnull
|
||||
@SuppressWarnings("ALL")
|
||||
public CommandComponent<C, ?>[] getComponents() {
|
||||
return (CommandComponent<C, ?>[]) Arrays.asList(this.components).toArray();
|
||||
}
|
||||
|
|
@ -85,10 +98,21 @@ public class Command<C extends CommandSender> {
|
|||
*
|
||||
* @return Command execution handler
|
||||
*/
|
||||
@Nonnull public CommandExecutionHandler<C> getCommandExecutionHandler() {
|
||||
@Nonnull
|
||||
public CommandExecutionHandler<C> getCommandExecutionHandler() {
|
||||
return this.commandExecutionHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the required sender type, if one has been specified
|
||||
*
|
||||
* @return Required sender type
|
||||
*/
|
||||
@Nonnull
|
||||
public Optional<Class<? extends C>> getSenderType() {
|
||||
return Optional.ofNullable(this.senderType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the longest chain of similar components for
|
||||
* two commands
|
||||
|
|
@ -110,12 +134,16 @@ public class Command<C extends CommandSender> {
|
|||
|
||||
public static class Builder<C extends CommandSender> {
|
||||
|
||||
private final List<CommandComponent<C, ?>> commandComponents;
|
||||
private final CommandExecutionHandler<C> commandExecutionHandler;
|
||||
@Nonnull private final List<CommandComponent<C, ?>> commandComponents;
|
||||
@Nonnull private final CommandExecutionHandler<C> commandExecutionHandler;
|
||||
@Nullable private final Class<? extends C> senderType;
|
||||
|
||||
private Builder(@Nonnull final List<CommandComponent<C, ?>> commandComponents, @Nonnull final CommandExecutionHandler<C> commandExecutionHandler) {
|
||||
private Builder(@Nullable final Class<? extends C> senderType,
|
||||
@Nonnull final List<CommandComponent<C, ?>> commandComponents,
|
||||
@Nonnull final CommandExecutionHandler<C> commandExecutionHandler) {
|
||||
this.commandComponents = commandComponents;
|
||||
this.commandExecutionHandler = commandExecutionHandler;
|
||||
this.senderType = senderType;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -129,7 +157,7 @@ public class Command<C extends CommandSender> {
|
|||
public <T> Builder<C> withComponent(@Nonnull final CommandComponent<C, T> component) {
|
||||
final List<CommandComponent<C, ?>> commandComponents = new LinkedList<>(this.commandComponents);
|
||||
commandComponents.add(component);
|
||||
return new Builder<>(commandComponents, this.commandExecutionHandler);
|
||||
return new Builder<>(this.senderType, commandComponents, this.commandExecutionHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -140,7 +168,18 @@ public class Command<C extends CommandSender> {
|
|||
*/
|
||||
@Nonnull
|
||||
public Builder<C> withHandler(@Nonnull final CommandExecutionHandler<C> commandExecutionHandler) {
|
||||
return new Builder<>(this.commandComponents, commandExecutionHandler);
|
||||
return new Builder<>(this.senderType, this.commandComponents, commandExecutionHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify a required sender type
|
||||
*
|
||||
* @param senderType Required sender type
|
||||
* @return New builder instance using the command execution handler
|
||||
*/
|
||||
@Nonnull
|
||||
public Builder<C> withSenderType(@Nonnull final Class<? extends C> senderType) {
|
||||
return new Builder<>(senderType, this.commandComponents, this.commandExecutionHandler);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -150,7 +189,8 @@ public class Command<C extends CommandSender> {
|
|||
*/
|
||||
@Nonnull
|
||||
public Command<C> build() {
|
||||
return new Command<>(this.commandComponents.toArray(new CommandComponent[0]), this.commandExecutionHandler);
|
||||
return new Command<>(this.commandComponents.toArray(new CommandComponent[0]), this.commandExecutionHandler,
|
||||
this.senderType);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@ import com.intellectualsites.commands.sender.CommandSender;
|
|||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.StringTokenizer;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
|
@ -56,13 +57,23 @@ public abstract class CommandManager<C extends CommandSender> {
|
|||
}
|
||||
|
||||
public CompletableFuture<CommandResult> executeCommand(@Nonnull final C commandSender, @Nonnull final String input) {
|
||||
final CommandContext<C> context = new CommandContext<>(commandSender);
|
||||
return this.commandExecutionCoordinator.coordinateExecution(context, tokenize(input));
|
||||
}
|
||||
|
||||
public List<String> suggest(@Nonnull final C commandSender, @Nonnull final String input) {
|
||||
final CommandContext<C> context = new CommandContext<>(commandSender);
|
||||
return this.commandTree.getSuggestions(context, tokenize(input));
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
private Queue<String> tokenize(@Nonnull final String input) {
|
||||
final StringTokenizer stringTokenizer = new StringTokenizer(input, " ");
|
||||
final Queue<String> tokens = new LinkedList<>();
|
||||
while (stringTokenizer.hasMoreElements()) {
|
||||
tokens.add(stringTokenizer.nextToken());
|
||||
}
|
||||
final CommandContext<C> context = new CommandContext<>(commandSender);
|
||||
return this.commandExecutionCoordinator.coordinateExecution(context, tokens);
|
||||
return tokens;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -81,7 +92,8 @@ public abstract class CommandManager<C extends CommandSender> {
|
|||
*
|
||||
* @return Command syntax formatter
|
||||
*/
|
||||
@Nonnull public CommandSyntaxFormatter getCommandSyntaxFormatter() {
|
||||
@Nonnull
|
||||
public CommandSyntaxFormatter getCommandSyntaxFormatter() {
|
||||
return this.commandSyntaxFormatter;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -36,7 +36,14 @@ import com.intellectualsites.commands.sender.CommandSender;
|
|||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Queue;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
|
|
@ -50,7 +57,8 @@ public class CommandTree<C extends CommandSender> {
|
|||
private final CommandManager<C> commandManager;
|
||||
private final CommandRegistrationHandler commandRegistrationHandler;
|
||||
|
||||
private CommandTree(@Nonnull final CommandManager<C> commandManager, @Nonnull final CommandRegistrationHandler commandRegistrationHandler) {
|
||||
private CommandTree(@Nonnull final CommandManager<C> commandManager,
|
||||
@Nonnull final CommandRegistrationHandler commandRegistrationHandler) {
|
||||
this.commandManager = commandManager;
|
||||
this.commandRegistrationHandler = commandRegistrationHandler;
|
||||
}
|
||||
|
|
@ -65,17 +73,19 @@ public class CommandTree<C extends CommandSender> {
|
|||
*/
|
||||
@Nonnull
|
||||
public static <C extends CommandSender> CommandTree<C> newTree(@Nonnull final CommandManager<C> commandManager,
|
||||
@Nonnull final CommandRegistrationHandler commandRegistrationHandler) {
|
||||
@Nonnull
|
||||
final CommandRegistrationHandler commandRegistrationHandler) {
|
||||
return new CommandTree<>(commandManager, commandRegistrationHandler);
|
||||
}
|
||||
|
||||
public Optional<Command<C>> parse(@Nonnull final CommandContext<C> commandContext, @Nonnull final Queue<String> args) throws NoSuchCommandException {
|
||||
public Optional<Command<C>> parse(@Nonnull final CommandContext<C> commandContext, @Nonnull final Queue<String> args) throws
|
||||
NoSuchCommandException {
|
||||
return parseCommand(commandContext, args, this.internalTree);
|
||||
}
|
||||
|
||||
private Optional<Command<C>> parseCommand(@Nonnull final CommandContext<C> commandContext, @Nonnull final Queue<String> commandQueue,
|
||||
@Nonnull final Node<CommandComponent<C, ?>> root) throws NoSuchCommandException {
|
||||
|
||||
private Optional<Command<C>> parseCommand(@Nonnull final CommandContext<C> commandContext,
|
||||
@Nonnull final Queue<String> commandQueue,
|
||||
@Nonnull final Node<CommandComponent<C, ?>> root) throws NoSuchCommandException {
|
||||
final List<Node<CommandComponent<C, ?>>> children = root.getChildren();
|
||||
if (children.size() == 1 && !(children.get(0).getValue() instanceof StaticComponent)) {
|
||||
// The value has to be a variable
|
||||
|
|
@ -84,12 +94,21 @@ public class CommandTree<C extends CommandSender> {
|
|||
if (commandQueue.isEmpty()) {
|
||||
if (child.isLeaf()) {
|
||||
/* Not enough arguments */
|
||||
throw new InvalidSyntaxException(this.commandManager.getCommandSyntaxFormatter().apply(Arrays.asList(child.getValue().getOwningCommand().getComponents())),
|
||||
commandContext.getCommandSender(), this.getChain(root).stream().map(Node::getValue).collect(Collectors.toList()));
|
||||
throw new InvalidSyntaxException(this.commandManager.getCommandSyntaxFormatter()
|
||||
.apply(Arrays.asList(child.getValue()
|
||||
.getOwningCommand()
|
||||
.getComponents())),
|
||||
commandContext.getCommandSender(), this.getChain(root)
|
||||
.stream()
|
||||
.map(Node::getValue)
|
||||
.collect(Collectors.toList()));
|
||||
} else {
|
||||
throw new NoSuchCommandException(commandContext.getCommandSender(),
|
||||
this.getChain(root).stream().map(Node::getValue).collect(Collectors.toList()),
|
||||
"");
|
||||
this.getChain(root)
|
||||
.stream()
|
||||
.map(Node::getValue)
|
||||
.collect(Collectors.toList()),
|
||||
"");
|
||||
}
|
||||
}
|
||||
final ComponentParseResult<?> result = child.getValue().getParser().parse(commandContext, commandQueue);
|
||||
|
|
@ -100,8 +119,15 @@ public class CommandTree<C extends CommandSender> {
|
|||
return Optional.ofNullable(child.getValue().getOwningCommand());
|
||||
} else {
|
||||
/* Too many arguments. We have a unique path, so we can send the entire context */
|
||||
throw new InvalidSyntaxException(this.commandManager.getCommandSyntaxFormatter().apply(Arrays.asList(child.getValue().getOwningCommand().getComponents())),
|
||||
commandContext.getCommandSender(), this.getChain(root).stream().map(Node::getValue).collect(Collectors.toList()));
|
||||
throw new InvalidSyntaxException(this.commandManager.getCommandSyntaxFormatter()
|
||||
.apply(Arrays.asList(child.getValue()
|
||||
.getOwningCommand()
|
||||
.getComponents())),
|
||||
commandContext.getCommandSender(), this.getChain(root)
|
||||
.stream()
|
||||
.map(Node::getValue)
|
||||
.collect(
|
||||
Collectors.toList()));
|
||||
}
|
||||
} else {
|
||||
return this.parseCommand(commandContext, commandQueue, child);
|
||||
|
|
@ -111,7 +137,6 @@ public class CommandTree<C extends CommandSender> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* There are 0 or more static components as children. No variable child components are present */
|
||||
if (children.isEmpty()) {
|
||||
/* We are at the bottom. Check if there's a command attached, in which case we're done */
|
||||
|
|
@ -120,44 +145,23 @@ public class CommandTree<C extends CommandSender> {
|
|||
return Optional.of(root.getValue().getOwningCommand());
|
||||
} else {
|
||||
/* Too many arguments. We have a unique path, so we can send the entire context */
|
||||
throw new InvalidSyntaxException(this.commandManager.getCommandSyntaxFormatter().apply(Arrays.asList(root.getValue().getOwningCommand().getComponents())),
|
||||
commandContext.getCommandSender(), this.getChain(root).stream().map(Node::getValue).collect(Collectors.toList()));
|
||||
throw new InvalidSyntaxException(this.commandManager.getCommandSyntaxFormatter()
|
||||
.apply(Arrays.asList(root.getValue()
|
||||
.getOwningCommand()
|
||||
.getComponents())),
|
||||
commandContext.getCommandSender(), this.getChain(root)
|
||||
.stream()
|
||||
.map(Node::getValue)
|
||||
.collect(Collectors.toList()));
|
||||
}
|
||||
} else {
|
||||
/* TODO: Indicate that we could not resolve the command here */
|
||||
final List<CommandComponent<C, ?>> components = this.getChain(root).stream().map(Node::getValue).collect(Collectors.toList());
|
||||
final List<CommandComponent<C, ?>> components = this.getChain(root)
|
||||
.stream()
|
||||
.map(Node::getValue)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
} else {
|
||||
/*
|
||||
final String popped = commandQueue.poll();
|
||||
if (popped == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
int low = 0;
|
||||
int high = children.size() - 1;
|
||||
|
||||
while (low <= high) {
|
||||
int mid = (low + high) / 2;
|
||||
|
||||
final Node<CommandComponent<?>> node = children.get(mid);
|
||||
assert node.getValue() != null;
|
||||
|
||||
final int comparison = node.getValue().getName().compareToIgnoreCase(popped);
|
||||
if (comparison < 0) {
|
||||
low = mid + 1;
|
||||
} else if (comparison > 0) {
|
||||
high = mid - 1;
|
||||
} else {
|
||||
if (node.isLeaf()) {
|
||||
return Optional.ofNullable(node.getValue().getOwningCommand());
|
||||
} else {
|
||||
return parseCommand(commandSender, commandQueue, node);
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
final Iterator<Node<CommandComponent<C, ?>>> childIterator = root.getChildren().iterator();
|
||||
if (childIterator.hasNext()) {
|
||||
while (childIterator.hasNext()) {
|
||||
|
|
@ -171,16 +175,86 @@ public class CommandTree<C extends CommandSender> {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* We could not find a match */
|
||||
throw new NoSuchCommandException(commandContext.getCommandSender(),
|
||||
getChain(root).stream().map(Node::getValue).collect(Collectors.toList()),
|
||||
java.util.Objects.requireNonNull(commandQueue.peek()));
|
||||
getChain(root).stream().map(Node::getValue).collect(Collectors.toList()),
|
||||
stringOrEmpty(commandQueue.peek()));
|
||||
}
|
||||
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
public List<String> getSuggestions(@Nonnull final CommandContext<C> context, @Nonnull final Queue<String> commandQueue) {
|
||||
return getSuggestions(context, commandQueue, this.internalTree);
|
||||
}
|
||||
|
||||
public List<String> getSuggestions(@Nonnull final CommandContext<C> commandContext,
|
||||
@Nonnull final Queue<String> commandQueue,
|
||||
@Nonnull final Node<CommandComponent<C, ?>> root) {
|
||||
final List<Node<CommandComponent<C, ?>>> children = root.getChildren();
|
||||
if (children.size() == 1 && !(children.get(0).getValue() instanceof StaticComponent)) {
|
||||
// The value has to be a variable
|
||||
final Node<CommandComponent<C, ?>> child = children.get(0);
|
||||
if (child.getValue() != null) {
|
||||
if (commandQueue.isEmpty()) {
|
||||
if (child.isLeaf()) {
|
||||
/* Child is leaf, and so no suggestions should be sent */
|
||||
return Collections.emptyList();
|
||||
} else {
|
||||
/* Send all suggestions */
|
||||
return child.getValue().getParser().suggestions(commandContext, "");
|
||||
}
|
||||
}
|
||||
final ComponentParseResult<?> result = child.getValue().getParser().parse(commandContext, commandQueue);
|
||||
if (result.getParsedValue().isPresent()) {
|
||||
if (child.isLeaf()) {
|
||||
/* Child is leaf, and so no suggestions should be sent */
|
||||
return Collections.emptyList();
|
||||
}
|
||||
commandContext.store(child.getValue().getName(), result.getParsedValue().get());
|
||||
return this.getSuggestions(commandContext, commandQueue, child);
|
||||
} else if (result.getFailure().isPresent()) {
|
||||
/* TODO: Return error */
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
}
|
||||
/* There are 0 or more static components as children. No variable child components are present */
|
||||
if (children.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
} else {
|
||||
final Iterator<Node<CommandComponent<C, ?>>> childIterator = root.getChildren().iterator();
|
||||
if (childIterator.hasNext()) {
|
||||
while (childIterator.hasNext()) {
|
||||
final Node<CommandComponent<C, ?>> child = childIterator.next();
|
||||
if (child.getValue() != null) {
|
||||
final ComponentParseResult<?> result = child.getValue().getParser().parse(commandContext, commandQueue);
|
||||
if (result.getParsedValue().isPresent()) {
|
||||
return this.getSuggestions(commandContext, commandQueue, child);
|
||||
} else if (result.getFailure().isPresent() && root.children.size() == 1) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
final List<String> suggestions = new LinkedList<>();
|
||||
for (final Node<CommandComponent<C, ?>> component : root.getChildren()) {
|
||||
if (component.getValue() == null) {
|
||||
continue;
|
||||
}
|
||||
suggestions.addAll(
|
||||
component.getValue().getParser().suggestions(commandContext, stringOrEmpty(commandQueue.peek())));
|
||||
}
|
||||
return suggestions;
|
||||
}
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
private String stringOrEmpty(@Nullable final String string) {
|
||||
if (string == null) {
|
||||
return "";
|
||||
}
|
||||
return string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a new command into the command tree
|
||||
*
|
||||
|
|
@ -196,6 +270,7 @@ public class CommandTree<C extends CommandSender> {
|
|||
if (node.children.size() > 0) {
|
||||
node.children.sort(Comparator.comparing(Node::getValue));
|
||||
}
|
||||
tempNode.setParent(node);
|
||||
node = tempNode;
|
||||
}
|
||||
if (node.getValue() != null) {
|
||||
|
|
@ -260,7 +335,7 @@ public class CommandTree<C extends CommandSender> {
|
|||
Node<CommandComponent<C, ?>> tail = end;
|
||||
while (tail != null) {
|
||||
chain.add(tail);
|
||||
tail = end.getParent();
|
||||
tail = tail.getParent();
|
||||
}
|
||||
return Lists.reverse(chain);
|
||||
}
|
||||
|
|
@ -324,15 +399,15 @@ public class CommandTree<C extends CommandSender> {
|
|||
return Objects.hashCode(getChildren(), getValue());
|
||||
}
|
||||
|
||||
public void setParent(@Nullable final Node<T> parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Node<T> getParent() {
|
||||
return this.parent;
|
||||
}
|
||||
|
||||
public void setParent(@Nullable final Node<T> parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -64,11 +64,11 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
|
|||
private Command<C> owningCommand;
|
||||
|
||||
CommandComponent(final boolean required, @Nonnull final String name,
|
||||
@Nonnull final ComponentParser<C, T> parser) {
|
||||
@Nonnull final ComponentParser<C, T> parser) {
|
||||
this.required = required;
|
||||
this.name = Objects.requireNonNull(name, "Name may not be null");
|
||||
if (!NAME_PATTERN.asPredicate().test(name)) {
|
||||
throw new IllegalArgumentException("Name must be alphanumeric");
|
||||
throw new IllegalArgumentException("Name must be alphanumeric");
|
||||
}
|
||||
this.parser = Objects.requireNonNull(parser, "Parser may not be null");
|
||||
}
|
||||
|
|
@ -77,11 +77,12 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
|
|||
* Create a new command component
|
||||
*
|
||||
* @param clazz Argument class
|
||||
* @param <C> Command sender type
|
||||
* @param <T> Argument Type
|
||||
* @param <C> Command sender type
|
||||
* @param <T> Argument Type
|
||||
* @return Component builder
|
||||
*/
|
||||
@Nonnull public static <C extends CommandSender, T> CommandComponent.Builder<C, T> ofType(@Nonnull final Class<T> clazz) {
|
||||
@Nonnull
|
||||
public static <C extends CommandSender, T> CommandComponent.Builder<C, T> ofType(@Nonnull final Class<T> clazz) {
|
||||
return new Builder<>();
|
||||
}
|
||||
|
||||
|
|
@ -99,7 +100,8 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
|
|||
*
|
||||
* @return Component name
|
||||
*/
|
||||
@Nonnull public String getName() {
|
||||
@Nonnull
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
|
|
@ -109,11 +111,14 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
|
|||
*
|
||||
* @return Command parser
|
||||
*/
|
||||
@Nonnull public ComponentParser<C, T> getParser() {
|
||||
@Nonnull
|
||||
public ComponentParser<C, T> getParser() {
|
||||
return this.parser;
|
||||
}
|
||||
|
||||
@Nonnull @Override public String toString() {
|
||||
@Nonnull
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("CommandComponent{name=%s}", this.name);
|
||||
}
|
||||
|
||||
|
|
@ -122,7 +127,8 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
|
|||
*
|
||||
* @return Owning command
|
||||
*/
|
||||
@Nullable public Command<C> getOwningCommand() {
|
||||
@Nullable
|
||||
public Command<C> getOwningCommand() {
|
||||
return this.owningCommand;
|
||||
}
|
||||
|
||||
|
|
@ -188,7 +194,8 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
|
|||
* @param name Alphanumeric component name
|
||||
* @return Builder instance
|
||||
*/
|
||||
@Nonnull public Builder<C, T> named(@Nonnull final String name) {
|
||||
@Nonnull
|
||||
public Builder<C, T> named(@Nonnull final String name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
|
|
@ -202,7 +209,8 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
|
|||
*
|
||||
* @return Builder instance
|
||||
*/
|
||||
@Nonnull public Builder<C, T> asRequired() {
|
||||
@Nonnull
|
||||
public Builder<C, T> asRequired() {
|
||||
this.required = true;
|
||||
return this;
|
||||
}
|
||||
|
|
@ -216,7 +224,8 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
|
|||
*
|
||||
* @return Builder instance
|
||||
*/
|
||||
@Nonnull public Builder<C, T> asOptional() {
|
||||
@Nonnull
|
||||
public Builder<C, T> asOptional() {
|
||||
this.required = false;
|
||||
return this;
|
||||
}
|
||||
|
|
@ -227,7 +236,8 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
|
|||
* @param parser Component parser
|
||||
* @return Builder instance
|
||||
*/
|
||||
@Nonnull public Builder<C, T> withParser(@Nonnull final ComponentParser<C, T> parser) {
|
||||
@Nonnull
|
||||
public Builder<C, T> withParser(@Nonnull final ComponentParser<C, T> parser) {
|
||||
this.parser = Objects.requireNonNull(parser, "Parser may not be null");
|
||||
return this;
|
||||
}
|
||||
|
|
@ -237,7 +247,8 @@ public class CommandComponent<C extends CommandSender, T> implements Comparable<
|
|||
*
|
||||
* @return Constructed component
|
||||
*/
|
||||
@Nonnull public CommandComponent<C, T> build() {
|
||||
@Nonnull
|
||||
public CommandComponent<C, T> build() {
|
||||
return new CommandComponent<>(this.required, this.name, this.parser);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -27,10 +27,13 @@ import javax.annotation.Nonnull;
|
|||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
@FunctionalInterface public interface CommandSyntaxFormatter extends Function<List<CommandComponent<?, ?>>, String> {
|
||||
@FunctionalInterface
|
||||
public interface CommandSyntaxFormatter extends Function<List<CommandComponent<?, ?>>, String> {
|
||||
|
||||
CommandSyntaxFormatter STANDARD_COMMAND_SYNTAX_FORMATTER = new StandardCommandSyntaxFormatter();
|
||||
|
||||
@Override @Nonnull String apply(@Nonnull List<CommandComponent<?, ?>> commandComponents);
|
||||
@Override
|
||||
@Nonnull
|
||||
String apply(@Nonnull List<CommandComponent<?, ?>> commandComponents);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,21 +30,28 @@ import com.intellectualsites.commands.sender.CommandSender;
|
|||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Queue;
|
||||
import java.util.Set;
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
@Nonnull public static <C extends CommandSender> StaticComponent<C> required(@Nonnull final String name, @Nonnull final String ... aliases) {
|
||||
@Nonnull
|
||||
public static <C extends CommandSender> StaticComponent<C> required(@Nonnull final String name,
|
||||
@Nonnull final String... aliases) {
|
||||
return new StaticComponent<>(true, name, aliases);
|
||||
}
|
||||
|
||||
@Nonnull public static <C extends CommandSender> StaticComponent<C> optional(@Nonnull final String name, @Nonnull final String ... aliases) {
|
||||
@Nonnull
|
||||
public static <C extends CommandSender> StaticComponent<C> optional(@Nonnull final String name,
|
||||
@Nonnull final String... aliases) {
|
||||
return new StaticComponent<>(false, name, aliases);
|
||||
}
|
||||
|
||||
|
|
@ -54,12 +61,15 @@ public final class StaticComponent<C extends CommandSender> extends CommandCompo
|
|||
private final String name;
|
||||
private final Set<String> acceptedStrings = new HashSet<>();
|
||||
|
||||
private StaticComponentParser(@Nonnull final String name, @Nonnull final String ... aliases) {
|
||||
private StaticComponentParser(@Nonnull final String name, @Nonnull final String... aliases) {
|
||||
this.acceptedStrings.add(this.name = name);
|
||||
this.acceptedStrings.addAll(Arrays.asList(aliases));
|
||||
}
|
||||
|
||||
@Nonnull @Override public ComponentParseResult<String> parse(@Nonnull final CommandContext<C> commandContext, @Nonnull final Queue<String> inputQueue) {
|
||||
@Nonnull
|
||||
@Override
|
||||
public ComponentParseResult<String> parse(@Nonnull final CommandContext<C> commandContext,
|
||||
@Nonnull final Queue<String> inputQueue) {
|
||||
final String string = inputQueue.peek();
|
||||
if (string == null) {
|
||||
return ComponentParseResult.failure(this.name);
|
||||
|
|
@ -74,6 +84,15 @@ public final class StaticComponent<C extends CommandSender> extends CommandCompo
|
|||
return ComponentParseResult.failure(this.name);
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public List<String> suggestions(@Nonnull final CommandContext<C> commandContext, @Nonnull final String input) {
|
||||
if (this.name.toLowerCase(Locale.ENGLISH).startsWith(input)) {
|
||||
return Collections.singletonList(this.name);
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -70,8 +70,7 @@ public class CommandContext<C extends CommandSender> {
|
|||
public <T> Optional<T> get(@Nonnull final String key) {
|
||||
final Object value = this.internalStorage.get(key);
|
||||
if (value != null) {
|
||||
@SuppressWarnings("ALL")
|
||||
final T castedValue = (T) value;
|
||||
@SuppressWarnings("ALL") final T castedValue = (T) value;
|
||||
return Optional.of(castedValue);
|
||||
} else {
|
||||
return Optional.empty();
|
||||
|
|
|
|||
|
|
@ -44,7 +44,8 @@ public class CommandParseException extends IllegalArgumentException {
|
|||
* @param commandSender Sender who executed the command
|
||||
* @param currentChain Chain leading up to the exception
|
||||
*/
|
||||
protected CommandParseException(@Nonnull final CommandSender commandSender, @Nonnull final List<CommandComponent<?, ?>> currentChain) {
|
||||
protected CommandParseException(@Nonnull final CommandSender commandSender,
|
||||
@Nonnull final List<CommandComponent<?, ?>> currentChain) {
|
||||
this.commandSender = commandSender;
|
||||
this.currentChain = currentChain;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,12 +33,15 @@ public class InvalidSyntaxException extends CommandParseException {
|
|||
|
||||
private final String correctSyntax;
|
||||
|
||||
public InvalidSyntaxException(@Nonnull final String correctSyntax, @Nonnull final CommandSender commandSender, @Nonnull final List<CommandComponent<?, ?>> currentChain) {
|
||||
public InvalidSyntaxException(@Nonnull final String correctSyntax,
|
||||
@Nonnull final CommandSender commandSender,
|
||||
@Nonnull final List<CommandComponent<?, ?>> currentChain) {
|
||||
super(commandSender, currentChain);
|
||||
this.correctSyntax = correctSyntax;
|
||||
}
|
||||
|
||||
@Nonnull public String getCorrectSyntax() {
|
||||
@Nonnull
|
||||
public String getCorrectSyntax() {
|
||||
return this.correctSyntax;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -44,8 +44,9 @@ public class NoSuchCommandException extends CommandParseException {
|
|||
* @param currentChain Chain leading up to the exception
|
||||
* @param command Entered command (following the command chain)
|
||||
*/
|
||||
public NoSuchCommandException(@Nonnull final CommandSender commandSender, @Nonnull final List<CommandComponent<?, ?>> currentChain,
|
||||
@Nonnull final String command) {
|
||||
public NoSuchCommandException(@Nonnull final CommandSender commandSender,
|
||||
@Nonnull final List<CommandComponent<?, ?>> currentChain,
|
||||
@Nonnull final String command) {
|
||||
super(commandSender, currentChain);
|
||||
this.suppliedCommand = command;
|
||||
}
|
||||
|
|
@ -55,7 +56,8 @@ public class NoSuchCommandException extends CommandParseException {
|
|||
*
|
||||
* @return Supplied command
|
||||
*/
|
||||
@Nonnull public String getSuppliedCommand() {
|
||||
@Nonnull
|
||||
public String getSuppliedCommand() {
|
||||
return this.suppliedCommand;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -71,7 +71,8 @@ public abstract class CommandExecutionCoordinator<C extends CommandSender> {
|
|||
*
|
||||
* @return Command tree
|
||||
*/
|
||||
@Nonnull protected CommandTree<C> getCommandTree() {
|
||||
@Nonnull
|
||||
protected CommandTree<C> getCommandTree() {
|
||||
return this.commandTree;
|
||||
}
|
||||
|
||||
|
|
@ -83,7 +84,8 @@ public abstract class CommandExecutionCoordinator<C extends CommandSender> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<CommandResult> coordinateExecution(@Nonnull CommandContext<C> commandContext, @Nonnull Queue<String> input) {
|
||||
public CompletableFuture<CommandResult> coordinateExecution(@Nonnull CommandContext<C> commandContext,
|
||||
@Nonnull Queue<String> input) {
|
||||
final CompletableFuture<CommandResult> completableFuture = new CompletableFuture<>();
|
||||
try {
|
||||
this.getCommandTree().parse(commandContext, input).ifPresent(
|
||||
|
|
|
|||
|
|
@ -34,7 +34,8 @@ import javax.annotation.Nonnull;
|
|||
*
|
||||
* @param <C> Command sender type
|
||||
*/
|
||||
@FunctionalInterface public interface CommandExecutionHandler<C extends CommandSender> {
|
||||
@FunctionalInterface
|
||||
public interface CommandExecutionHandler<C extends CommandSender> {
|
||||
|
||||
/**
|
||||
* Handle command execution
|
||||
|
|
|
|||
|
|
@ -32,7 +32,8 @@ import javax.annotation.Nonnull;
|
|||
* platform the library is used in. This can do nothing, if
|
||||
* the target platform does not have its own concept of commands
|
||||
*/
|
||||
@FunctionalInterface public interface CommandRegistrationHandler {
|
||||
@FunctionalInterface
|
||||
public interface CommandRegistrationHandler {
|
||||
|
||||
/**
|
||||
* Command registration handler that does nothing
|
||||
|
|
@ -44,7 +45,7 @@ import javax.annotation.Nonnull;
|
|||
*
|
||||
* @param command Command to register
|
||||
* @return {@code true} if the command was registered successfully,
|
||||
* else {@code false}
|
||||
* else {@code false}
|
||||
*/
|
||||
boolean registerCommand(@Nonnull final Command command);
|
||||
|
||||
|
|
|
|||
|
|
@ -31,17 +31,21 @@ public abstract class ComponentParseResult<T> {
|
|||
private ComponentParseResult() {
|
||||
}
|
||||
|
||||
@Nonnull public static <T> ComponentParseResult<T> failure(@Nonnull final String failure) {
|
||||
@Nonnull
|
||||
public static <T> ComponentParseResult<T> failure(@Nonnull final String failure) {
|
||||
return new ParseFailure<>(failure);
|
||||
}
|
||||
|
||||
@Nonnull public static <T> ComponentParseResult<T> success(@Nonnull final T value) {
|
||||
@Nonnull
|
||||
public static <T> ComponentParseResult<T> success(@Nonnull final T value) {
|
||||
return new ParseSuccess<>(value);
|
||||
}
|
||||
|
||||
@Nonnull public abstract Optional<T> getParsedValue();
|
||||
@Nonnull
|
||||
public abstract Optional<T> getParsedValue();
|
||||
|
||||
@Nonnull public abstract Optional<String> getFailure();
|
||||
@Nonnull
|
||||
public abstract Optional<String> getFailure();
|
||||
|
||||
|
||||
private static final class ParseSuccess<T> extends ComponentParseResult<T> {
|
||||
|
|
@ -52,11 +56,15 @@ public abstract class ComponentParseResult<T> {
|
|||
this.value = value;
|
||||
}
|
||||
|
||||
@Nonnull @Override public Optional<T> getParsedValue() {
|
||||
@Nonnull
|
||||
@Override
|
||||
public Optional<T> getParsedValue() {
|
||||
return Optional.of(this.value);
|
||||
}
|
||||
|
||||
@Nonnull @Override public Optional<String> getFailure() {
|
||||
@Nonnull
|
||||
@Override
|
||||
public Optional<String> getFailure() {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
|
|
@ -71,11 +79,15 @@ public abstract class ComponentParseResult<T> {
|
|||
this.failure = failure;
|
||||
}
|
||||
|
||||
@Nonnull @Override public Optional<T> getParsedValue() {
|
||||
@Nonnull
|
||||
@Override
|
||||
public Optional<T> getParsedValue() {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
@Nonnull @Override public Optional<String> getFailure() {
|
||||
@Nonnull
|
||||
@Override
|
||||
public Optional<String> getFailure() {
|
||||
return Optional.of(this.failure);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,6 +27,8 @@ import com.intellectualsites.commands.context.CommandContext;
|
|||
import com.intellectualsites.commands.sender.CommandSender;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
|
||||
@FunctionalInterface
|
||||
|
|
@ -42,4 +44,9 @@ public interface ComponentParser<C extends CommandSender, T> {
|
|||
@Nonnull
|
||||
ComponentParseResult<T> parse(@Nonnull CommandContext<C> commandContext, @Nonnull Queue<String> inputQueue);
|
||||
|
||||
@Nonnull
|
||||
default List<String> suggestions(@Nonnull final CommandContext<C> commandContext, @Nonnull final String input) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue