♻️ Reformat + Update .editorconfig

This commit is contained in:
Alexander Söderberg 2020-10-06 22:48:30 +02:00 committed by Alexander Söderberg
parent 8bdec87a74
commit 2aac3980d5
169 changed files with 4261 additions and 2448 deletions

View file

@ -73,11 +73,13 @@ public class Command<C> {
* @param commandPermission Command permission
* @param commandMeta Command meta instance
*/
public Command(final @NonNull Map<@NonNull CommandArgument<C, ?>, @NonNull Description> commandArguments,
final @NonNull CommandExecutionHandler<@NonNull C> commandExecutionHandler,
final @Nullable Class<? extends C> senderType,
final @NonNull CommandPermission commandPermission,
final @NonNull CommandMeta commandMeta) {
public Command(
final @NonNull Map<@NonNull CommandArgument<C, ?>, @NonNull Description> commandArguments,
final @NonNull CommandExecutionHandler<@NonNull C> commandExecutionHandler,
final @Nullable Class<? extends C> senderType,
final @NonNull CommandPermission commandPermission,
final @NonNull CommandMeta commandMeta
) {
this.arguments = Objects.requireNonNull(commandArguments, "Command arguments may not be null");
if (this.arguments.size() == 0) {
throw new IllegalArgumentException("At least one command argument is required");
@ -90,8 +92,10 @@ public class Command<C> {
}
if (foundOptional && argument.isRequired()) {
throw new IllegalArgumentException(
String.format("Command argument '%s' cannot be placed after an optional argument",
argument.getName()));
String.format(
"Command argument '%s' cannot be placed after an optional argument",
argument.getName()
));
} else if (!argument.isRequired()) {
foundOptional = true;
}
@ -110,10 +114,12 @@ public class Command<C> {
* @param senderType Required sender type. May be {@code null}
* @param commandMeta Command meta instance
*/
public Command(final @NonNull Map<@NonNull CommandArgument<C, ?>, @NonNull Description> commandArguments,
final @NonNull CommandExecutionHandler<@NonNull C> commandExecutionHandler,
final @Nullable Class<? extends C> senderType,
final @NonNull CommandMeta commandMeta) {
public Command(
final @NonNull Map<@NonNull CommandArgument<C, ?>, @NonNull Description> commandArguments,
final @NonNull CommandExecutionHandler<@NonNull C> commandExecutionHandler,
final @Nullable Class<? extends C> senderType,
final @NonNull CommandMeta commandMeta
) {
this(commandArguments, commandExecutionHandler, senderType, Permission.empty(), commandMeta);
}
@ -125,10 +131,12 @@ public class Command<C> {
* @param commandPermission Command permission
* @param commandMeta Command meta instance
*/
public Command(final @NonNull Map<@NonNull CommandArgument<C, ?>, @NonNull Description> commandArguments,
final @NonNull CommandExecutionHandler<@NonNull C> commandExecutionHandler,
final @NonNull CommandPermission commandPermission,
final @NonNull CommandMeta commandMeta) {
public Command(
final @NonNull Map<@NonNull CommandArgument<C, ?>, @NonNull Description> commandArguments,
final @NonNull CommandExecutionHandler<@NonNull C> commandExecutionHandler,
final @NonNull CommandPermission commandPermission,
final @NonNull CommandMeta commandMeta
) {
this(commandArguments, commandExecutionHandler, null, commandPermission, commandMeta);
}
@ -143,19 +151,23 @@ public class Command<C> {
* @param <C> Command sender type
* @return Command builder
*/
public static <C> @NonNull Builder<C> newBuilder(final @NonNull String commandName,
final @NonNull CommandMeta commandMeta,
final @NonNull Description description,
final @NonNull String... aliases) {
public static <C> @NonNull Builder<C> newBuilder(
final @NonNull String commandName,
final @NonNull CommandMeta commandMeta,
final @NonNull Description description,
final @NonNull String... aliases
) {
final Map<@NonNull CommandArgument<C, ?>, @NonNull Description> map = new LinkedHashMap<>();
map.put(StaticArgument.of(commandName, aliases), description);
return new Builder<>(null,
commandMeta,
null,
map,
new CommandExecutionHandler.NullCommandExecutionHandler<>(),
Permission.empty(),
Collections.emptyList());
return new Builder<>(
null,
commandMeta,
null,
map,
new CommandExecutionHandler.NullCommandExecutionHandler<>(),
Permission.empty(),
Collections.emptyList()
);
}
/**
@ -168,18 +180,22 @@ public class Command<C> {
* @param <C> Command sender type
* @return Command builder
*/
public static <C> @NonNull Builder<C> newBuilder(final @NonNull String commandName,
final @NonNull CommandMeta commandMeta,
final @NonNull String... aliases) {
public static <C> @NonNull Builder<C> newBuilder(
final @NonNull String commandName,
final @NonNull CommandMeta commandMeta,
final @NonNull String... aliases
) {
final Map<CommandArgument<C, ?>, Description> map = new LinkedHashMap<>();
map.put(StaticArgument.of(commandName, aliases), Description.empty());
return new Builder<>(null,
commandMeta,
null,
map,
new CommandExecutionHandler.NullCommandExecutionHandler<>(),
Permission.empty(),
Collections.emptyList());
return new Builder<>(
null,
commandMeta,
null,
map,
new CommandExecutionHandler.NullCommandExecutionHandler<>(),
Permission.empty(),
Collections.emptyList()
);
}
/**
@ -273,13 +289,15 @@ public class Command<C> {
private final CommandManager<C> commandManager;
private final Collection<CommandFlag<?>> flags;
private Builder(final @Nullable CommandManager<C> commandManager,
final @NonNull CommandMeta commandMeta,
final @Nullable Class<? extends C> senderType,
final @NonNull Map<@NonNull CommandArgument<C, ?>, @NonNull Description> commandArguments,
final @NonNull CommandExecutionHandler<@NonNull C> commandExecutionHandler,
final @NonNull CommandPermission commandPermission,
final @NonNull Collection<CommandFlag<?>> flags) {
private Builder(
final @Nullable CommandManager<C> commandManager,
final @NonNull CommandMeta commandMeta,
final @Nullable Class<? extends C> senderType,
final @NonNull Map<@NonNull CommandArgument<C, ?>, @NonNull Description> commandArguments,
final @NonNull CommandExecutionHandler<@NonNull C> commandExecutionHandler,
final @NonNull CommandPermission commandPermission,
final @NonNull Collection<CommandFlag<?>> flags
) {
this.commandManager = commandManager;
this.senderType = senderType;
this.commandArguments = Objects.requireNonNull(commandArguments, "Arguments may not be null");
@ -298,13 +316,15 @@ public class Command<C> {
*/
public @NonNull Builder<C> meta(final @NonNull String key, final @NonNull String value) {
final CommandMeta commandMeta = SimpleCommandMeta.builder().with(this.commandMeta).with(key, value).build();
return new Builder<>(this.commandManager,
commandMeta,
this.senderType,
this.commandArguments,
this.commandExecutionHandler,
this.commandPermission,
this.flags);
return new Builder<>(
this.commandManager,
commandMeta,
this.senderType,
this.commandArguments,
this.commandExecutionHandler,
this.commandPermission,
this.flags
);
}
/**
@ -316,13 +336,15 @@ public class Command<C> {
* @return New builder instance using the provided command manager
*/
public @NonNull Builder<C> manager(final @Nullable CommandManager<C> commandManager) {
return new Builder<>(commandManager,
this.commandMeta,
this.senderType,
this.commandArguments,
this.commandExecutionHandler,
this.commandPermission,
this.flags);
return new Builder<>(
commandManager,
this.commandMeta,
this.senderType,
this.commandArguments,
this.commandExecutionHandler,
this.commandPermission,
this.flags
);
}
/**
@ -332,8 +354,10 @@ public class Command<C> {
* @param aliases Argument aliases
* @return New builder instance with the modified command chain
*/
public @NonNull Builder<C> literal(final @NonNull String main,
final @NonNull String... aliases) {
public @NonNull Builder<C> literal(
final @NonNull String main,
final @NonNull String... aliases
) {
return this.argument(StaticArgument.of(main, aliases));
}
@ -345,9 +369,11 @@ public class Command<C> {
* @param aliases Argument aliases
* @return New builder instance with the modified command chain
*/
public @NonNull Builder<C> literal(final @NonNull String main,
final @NonNull Description description,
final @NonNull String... aliases) {
public @NonNull Builder<C> literal(
final @NonNull String main,
final @NonNull Description description,
final @NonNull String... aliases
) {
return this.argument(StaticArgument.of(main, aliases), description);
}
@ -382,22 +408,26 @@ public class Command<C> {
* @param <T> Argument type
* @return New builder instance with the command argument inserted into the argument list
*/
public <T> @NonNull Builder<C> argument(final @NonNull CommandArgument<C, T> argument,
final @NonNull Description description) {
public <T> @NonNull Builder<C> argument(
final @NonNull CommandArgument<C, T> argument,
final @NonNull Description description
) {
if (argument.isArgumentRegistered()) {
throw new IllegalArgumentException("The provided argument has already been associated with a command."
+ " Use CommandArgument#copy to create a copy of the argument.");
+ " Use CommandArgument#copy to create a copy of the argument.");
}
argument.setArgumentRegistered();
final Map<CommandArgument<C, ?>, Description> commandArgumentMap = new LinkedHashMap<>(this.commandArguments);
commandArgumentMap.put(argument, description);
return new Builder<>(this.commandManager,
this.commandMeta,
this.senderType,
commandArgumentMap,
this.commandExecutionHandler,
this.commandPermission,
this.flags);
return new Builder<>(
this.commandManager,
this.commandMeta,
this.senderType,
commandArgumentMap,
this.commandExecutionHandler,
this.commandPermission,
this.flags
);
}
/**
@ -409,17 +439,21 @@ public class Command<C> {
* @param <T> Argument type
* @return New builder instance with the command argument inserted into the argument list
*/
public <T> @NonNull Builder<C> argument(final CommandArgument.@NonNull Builder<C, T> builder,
final @NonNull Description description) {
public <T> @NonNull Builder<C> argument(
final CommandArgument.@NonNull Builder<C, T> builder,
final @NonNull Description description
) {
final Map<CommandArgument<C, ?>, Description> commandArgumentMap = new LinkedHashMap<>(this.commandArguments);
commandArgumentMap.put(builder.build(), description);
return new Builder<>(this.commandManager,
this.commandMeta,
this.senderType,
commandArgumentMap,
this.commandExecutionHandler,
this.commandPermission,
this.flags);
return new Builder<>(
this.commandManager,
this.commandMeta,
this.senderType,
commandArgumentMap,
this.commandExecutionHandler,
this.commandPermission,
this.flags
);
}
/**
@ -431,9 +465,11 @@ public class Command<C> {
* @param <T> Argument type
* @return New builder instance with the command argument inserted into the argument list
*/
public <T> @NonNull Builder<C> argument(final @NonNull Class<T> clazz,
final @NonNull String name,
final @NonNull Consumer<CommandArgument.Builder<C, T>> builderConsumer) {
public <T> @NonNull Builder<C> argument(
final @NonNull Class<T> clazz,
final @NonNull String name,
final @NonNull Consumer<CommandArgument.Builder<C, T>> builderConsumer
) {
final CommandArgument.Builder<C, T> builder = CommandArgument.ofType(clazz, name);
if (this.commandManager != null) {
builder.manager(this.commandManager);
@ -461,10 +497,12 @@ public class Command<C> {
* @param <V> Second type
* @return Builder instance with the argument inserted
*/
public <U, V> @NonNull Builder<C> argumentPair(final @NonNull String name,
final @NonNull Pair<@NonNull String, @NonNull String> names,
final @NonNull Pair<@NonNull Class<U>, @NonNull Class<V>> parserPair,
final @NonNull Description description) {
public <U, V> @NonNull Builder<C> argumentPair(
final @NonNull String name,
final @NonNull Pair<@NonNull String, @NonNull String> names,
final @NonNull Pair<@NonNull Class<U>, @NonNull Class<V>> parserPair,
final @NonNull Description description
) {
if (this.commandManager == null) {
throw new IllegalStateException("This cannot be called from a command that has no command manager attached");
}
@ -491,18 +529,21 @@ public class Command<C> {
* @param <O> Output type
* @return Builder instance with the argument inserted
*/
public <U, V, O> @NonNull Builder<C> argumentPair(final @NonNull String name,
final @NonNull TypeToken<O> outputType,
final @NonNull Pair<String, String> names,
final @NonNull Pair<Class<U>, Class<V>> parserPair,
final @NonNull BiFunction<C, Pair<U, V>, O> mapper,
final @NonNull Description description) {
public <U, V, O> @NonNull Builder<C> argumentPair(
final @NonNull String name,
final @NonNull TypeToken<O> outputType,
final @NonNull Pair<String, String> names,
final @NonNull Pair<Class<U>, Class<V>> parserPair,
final @NonNull BiFunction<C, Pair<U, V>, O> mapper,
final @NonNull Description description
) {
if (this.commandManager == null) {
throw new IllegalStateException("This cannot be called from a command that has no command manager attached");
}
return this.argument(
ArgumentPair.of(this.commandManager, name, names, parserPair).withMapper(outputType, mapper),
description);
description
);
}
/**
@ -523,10 +564,12 @@ public class Command<C> {
* @param <W> Third type
* @return Builder instance with the argument inserted
*/
public <U, V, W> @NonNull Builder<C> argumentTriplet(final @NonNull String name,
final @NonNull Triplet<String, String, String> names,
final @NonNull Triplet<Class<U>, Class<V>, Class<W>> parserTriplet,
final @NonNull Description description) {
public <U, V, W> @NonNull Builder<C> argumentTriplet(
final @NonNull String name,
final @NonNull Triplet<String, String, String> names,
final @NonNull Triplet<Class<U>, Class<V>, Class<W>> parserTriplet,
final @NonNull Description description
) {
if (this.commandManager == null) {
throw new IllegalStateException("This cannot be called from a command that has no command manager attached");
}
@ -554,19 +597,22 @@ public class Command<C> {
* @param <O> Output type
* @return Builder instance with the argument inserted
*/
public <U, V, W, O> @NonNull Builder<C> argumentTriplet(final @NonNull String name,
final @NonNull TypeToken<O> outputType,
final @NonNull Triplet<String, String, String> names,
final @NonNull Triplet<Class<U>, Class<V>,
Class<W>> parserTriplet,
final @NonNull BiFunction<C, Triplet<U, V, W>, O> mapper,
final @NonNull Description description) {
public <U, V, W, O> @NonNull Builder<C> argumentTriplet(
final @NonNull String name,
final @NonNull TypeToken<O> outputType,
final @NonNull Triplet<String, String, String> names,
final @NonNull Triplet<Class<U>, Class<V>,
Class<W>> parserTriplet,
final @NonNull BiFunction<C, Triplet<U, V, W>, O> mapper,
final @NonNull Description description
) {
if (this.commandManager == null) {
throw new IllegalStateException("This cannot be called from a command that has no command manager attached");
}
return this.argument(
ArgumentTriplet.of(this.commandManager, name, names, parserTriplet).withMapper(outputType, mapper),
description);
description
);
}
// End of compound helper methods
@ -578,13 +624,15 @@ public class Command<C> {
* @return New builder instance using the command execution handler
*/
public @NonNull Builder<C> handler(final @NonNull CommandExecutionHandler<C> commandExecutionHandler) {
return new Builder<>(this.commandManager,
this.commandMeta,
this.senderType,
this.commandArguments,
commandExecutionHandler,
this.commandPermission,
this.flags);
return new Builder<>(
this.commandManager,
this.commandMeta,
this.senderType,
this.commandArguments,
commandExecutionHandler,
this.commandPermission,
this.flags
);
}
/**
@ -593,14 +641,16 @@ public class Command<C> {
* @param senderType Required sender type
* @return New builder instance using the command execution handler
*/
public @NonNull Builder<C> withSenderType(final @NonNull Class<? extends C> senderType) {
return new Builder<>(this.commandManager,
this.commandMeta,
senderType,
this.commandArguments,
this.commandExecutionHandler,
this.commandPermission,
this.flags);
public @NonNull Builder<C> senderType(final @NonNull Class<? extends C> senderType) {
return new Builder<>(
this.commandManager,
this.commandMeta,
senderType,
this.commandArguments,
this.commandExecutionHandler,
this.commandPermission,
this.flags
);
}
/**
@ -610,13 +660,15 @@ public class Command<C> {
* @return New builder instance using the command permission
*/
public @NonNull Builder<C> withPermission(final @NonNull CommandPermission permission) {
return new Builder<>(this.commandManager,
this.commandMeta,
this.senderType,
this.commandArguments,
this.commandExecutionHandler,
permission,
this.flags);
return new Builder<>(
this.commandManager,
this.commandMeta,
this.senderType,
this.commandArguments,
this.commandExecutionHandler,
permission,
this.flags
);
}
/**
@ -626,13 +678,15 @@ public class Command<C> {
* @return New builder instance using the command permission
*/
public @NonNull Builder<C> withPermission(final @NonNull String permission) {
return new Builder<>(this.commandManager,
this.commandMeta,
this.senderType,
this.commandArguments,
this.commandExecutionHandler,
Permission.of(permission),
this.flags);
return new Builder<>(
this.commandManager,
this.commandMeta,
this.senderType,
this.commandArguments,
this.commandExecutionHandler,
Permission.of(permission),
this.flags
);
}
/**
@ -681,13 +735,15 @@ public class Command<C> {
public @NonNull <T> Builder<C> flag(final @NonNull CommandFlag<T> flag) {
final List<CommandFlag<?>> flags = new ArrayList<>(this.flags);
flags.add(flag);
return new Builder<>(this.commandManager,
this.commandMeta,
this.senderType,
this.commandArguments,
this.commandExecutionHandler,
this.commandPermission,
Collections.unmodifiableList(flags));
return new Builder<>(
this.commandManager,
this.commandMeta,
this.senderType,
this.commandArguments,
this.commandExecutionHandler,
this.commandPermission,
Collections.unmodifiableList(flags)
);
}
/**
@ -713,11 +769,13 @@ public class Command<C> {
final FlagArgument<C> flagArgument = new FlagArgument<>(this.flags);
commandArguments.put(flagArgument, Description.of("Command flags"));
}
return new Command<>(Collections.unmodifiableMap(commandArguments),
this.commandExecutionHandler,
this.senderType,
this.commandPermission,
this.commandMeta);
return new Command<>(
Collections.unmodifiableMap(commandArguments),
this.commandExecutionHandler,
this.senderType,
this.commandPermission,
this.commandMeta
);
}
}

View file

@ -56,10 +56,12 @@ public final class CommandHelpHandler<C> {
for (final Command<C> command : this.commandManager.getCommands()) {
final List<CommandArgument<C, ?>> arguments = command.getArguments();
final String description = command.getCommandMeta().getOrDefault("description", "");
syntaxHints.add(new VerboseHelpEntry<>(command,
this.commandManager.getCommandSyntaxFormatter()
.apply(arguments, null),
description));
syntaxHints.add(new VerboseHelpEntry<>(
command,
this.commandManager.getCommandSyntaxFormatter()
.apply(arguments, null),
description
));
}
syntaxHints.sort(Comparator.comparing(VerboseHelpEntry::getSyntaxString));
return syntaxHints;
@ -75,58 +77,18 @@ public final class CommandHelpHandler<C> {
public @NonNull List<@NonNull String> getLongestSharedChains() {
final List<String> chains = new ArrayList<>();
this.commandManager.getCommandTree().getRootNodes().forEach(node ->
chains.add(Objects.requireNonNull(node.getValue())
.getName() + this.commandManager.getCommandSyntaxFormatter()
.apply(Collections
.emptyList(),
node)));
chains.add(Objects.requireNonNull(node.getValue())
.getName() + this.commandManager
.getCommandSyntaxFormatter()
.apply(
Collections
.emptyList(),
node
)));
chains.sort(String::compareTo);
return chains;
}
public static final class VerboseHelpEntry<C> {
private final Command<C> command;
private final String syntaxString;
private final String description;
private VerboseHelpEntry(final @NonNull Command<C> command,
final @NonNull String syntaxString,
final @NonNull String description) {
this.command = command;
this.syntaxString = syntaxString;
this.description = description;
}
/**
* Get the command
*
* @return Command
*/
public @NonNull Command<C> getCommand() {
return this.command;
}
/**
* Get the syntax string
*
* @return Syntax string
*/
public @NonNull String getSyntaxString() {
return this.syntaxString;
}
/**
* Get the command description
*
* @return Command description
*/
public @NonNull String getDescription() {
return this.description;
}
}
/**
* Query for help
*
@ -144,12 +106,15 @@ public final class CommandHelpHandler<C> {
* @param query Query string
* @return Help topic, will return an empty {@link IndexHelpTopic} if no results were found
*/
public @NonNull HelpTopic<C> queryHelp(final @Nullable C recipient,
final @NonNull String query) {
public @NonNull HelpTopic<C> queryHelp(
final @Nullable C recipient,
final @NonNull String query
) {
final List<VerboseHelpEntry<C>> commands = this.getAllCommands();
commands.removeIf(command -> recipient != null && !this.commandManager.hasPermission(recipient,
command.getCommand()
.getCommandPermission()));
commands.removeIf(command -> recipient != null && !this.commandManager.hasPermission(
recipient,
command.getCommand().getCommandPermission()
));
if (query.replace(" ", "").isEmpty()) {
return new IndexHelpTopic<>(commands);
}
@ -166,7 +131,7 @@ public final class CommandHelpHandler<C> {
for (final VerboseHelpEntry<C> entry : commands) {
final Command<C> command = entry.getCommand();
@SuppressWarnings("unchecked") final StaticArgument<C> staticArgument = (StaticArgument<C>) command.getArguments()
.get(0);
.get(0);
for (final String alias : staticArgument.getAliases()) {
if (alias.toLowerCase(Locale.ENGLISH).startsWith(rootFragment.toLowerCase(Locale.ENGLISH))) {
availableCommands.add(command);
@ -199,10 +164,12 @@ public final class CommandHelpHandler<C> {
for (final Command<C> command : availableCommands) {
final List<CommandArgument<C, ?>> arguments = command.getArguments();
final String description = command.getCommandMeta().getOrDefault("description", "");
syntaxHints.add(new VerboseHelpEntry<>(command,
this.commandManager.getCommandSyntaxFormatter()
.apply(arguments, null),
description));
syntaxHints.add(new VerboseHelpEntry<>(
command,
this.commandManager.getCommandSyntaxFormatter()
.apply(arguments, null),
description
));
}
syntaxHints.sort(Comparator.comparing(VerboseHelpEntry::getSyntaxString));
syntaxHints.removeIf(command -> recipient != null
@ -212,22 +179,23 @@ public final class CommandHelpHandler<C> {
/* Traverse command to find the most specific help topic */
final CommandTree.Node<CommandArgument<C, ?>> node = this.commandManager.getCommandTree()
.getNamedNode(availableCommandLabels.iterator()
.next());
.getNamedNode(availableCommandLabels.iterator()
.next());
final List<CommandArgument<C, ?>> traversedNodes = new LinkedList<>();
CommandTree.Node<CommandArgument<C, ?>> head = node;
int index = 0;
outer: while (head != null) {
outer:
while (head != null) {
++index;
traversedNodes.add(head.getValue());
if (head.getValue() != null && head.getValue().getOwningCommand() != null) {
if (head.isLeaf() || index == queryFragments.length) {
if (recipient == null || this.commandManager.hasPermission(recipient, head.getValue()
.getOwningCommand()
.getCommandPermission())) {
.getOwningCommand()
.getCommandPermission())) {
return new VerboseHelpTopic<>(head.getValue().getOwningCommand());
}
}
@ -239,8 +207,8 @@ public final class CommandHelpHandler<C> {
if (index < queryFragments.length) {
/* We might still be able to match an argument */
for (final CommandTree.Node<CommandArgument<C, ?>> child : head.getChildren()) {
@SuppressWarnings("unchecked")
final StaticArgument<C> childArgument = (StaticArgument<C>) child.getValue();
@SuppressWarnings("unchecked") final StaticArgument<C> childArgument = (StaticArgument<C>) child
.getValue();
if (childArgument == null) {
continue;
}
@ -260,8 +228,10 @@ public final class CommandHelpHandler<C> {
if (recipient == null
|| child.getValue() == null
|| child.getValue().getOwningCommand() == null
|| this.commandManager.hasPermission(recipient,
child.getValue().getOwningCommand().getCommandPermission())) {
|| this.commandManager.hasPermission(
recipient,
child.getValue().getOwningCommand().getCommandPermission()
)) {
traversedNodesSub.add(child.getValue());
childSuggestions.add(this.commandManager.getCommandSyntaxFormatter().apply(traversedNodesSub, child));
}
@ -273,7 +243,6 @@ public final class CommandHelpHandler<C> {
return new IndexHelpTopic<>(Collections.emptyList());
}
/**
* Something that can be returned as the result of a help query
* <p>
@ -287,8 +256,53 @@ public final class CommandHelpHandler<C> {
* @param <C> Command sender type
*/
public interface HelpTopic<C> {
}
public static final class VerboseHelpEntry<C> {
private final Command<C> command;
private final String syntaxString;
private final String description;
private VerboseHelpEntry(
final @NonNull Command<C> command,
final @NonNull String syntaxString,
final @NonNull String description
) {
this.command = command;
this.syntaxString = syntaxString;
this.description = description;
}
/**
* Get the command
*
* @return Command
*/
public @NonNull Command<C> getCommand() {
return this.command;
}
/**
* Get the syntax string
*
* @return Syntax string
*/
public @NonNull String getSyntaxString() {
return this.syntaxString;
}
/**
* Get the command description
*
* @return Command description
*/
public @NonNull String getDescription() {
return this.description;
}
}
/**
* Index of available commands
@ -371,8 +385,10 @@ public final class CommandHelpHandler<C> {
private final String longestPath;
private final List<String> childSuggestions;
private MultiHelpTopic(final @NonNull String longestPath,
final @NonNull List<@NonNull String> childSuggestions) {
private MultiHelpTopic(
final @NonNull String longestPath,
final @NonNull List<@NonNull String> childSuggestions
) {
this.longestPath = longestPath;
this.childSuggestions = childSuggestions;
}

View file

@ -45,13 +45,13 @@ import cloud.commandframework.execution.preprocessor.AcceptingCommandPreprocesso
import cloud.commandframework.execution.preprocessor.CommandPreprocessingContext;
import cloud.commandframework.execution.preprocessor.CommandPreprocessor;
import cloud.commandframework.internal.CommandRegistrationHandler;
import io.leangen.geantyref.TypeToken;
import cloud.commandframework.meta.CommandMeta;
import cloud.commandframework.permission.CommandPermission;
import cloud.commandframework.permission.OrPermission;
import cloud.commandframework.permission.Permission;
import cloud.commandframework.services.ServicePipeline;
import cloud.commandframework.services.State;
import io.leangen.geantyref.TypeToken;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
@ -76,7 +76,8 @@ import java.util.function.Function;
public abstract class CommandManager<C> {
private final Map<Class<? extends Exception>, BiConsumer<C, ? extends Exception>> exceptionHandlers = new HashMap<>();
private final EnumSet<ManagerSettings> managerSettings = EnumSet.of(ManagerSettings.ENFORCE_INTERMEDIARY_PERMISSIONS);
private final EnumSet<ManagerSettings> managerSettings = EnumSet.of(
ManagerSettings.ENFORCE_INTERMEDIARY_PERMISSIONS);
private final CommandContextFactory<C> commandContextFactory = new StandardCommandContextFactory<>();
private final ServicePipeline servicePipeline = ServicePipeline.builder().build();
@ -97,7 +98,8 @@ public abstract class CommandManager<C> {
*/
public CommandManager(
final @NonNull Function<@NonNull CommandTree<C>, @NonNull CommandExecutionCoordinator<C>> commandExecutionCoordinator,
final @NonNull CommandRegistrationHandler commandRegistrationHandler) {
final @NonNull CommandRegistrationHandler commandRegistrationHandler
) {
this.commandTree = CommandTree.newTree(this);
this.commandExecutionCoordinator = commandExecutionCoordinator.apply(commandTree);
this.commandRegistrationHandler = commandRegistrationHandler;
@ -132,8 +134,10 @@ public abstract class CommandManager<C> {
* @param input Input provided by the sender
* @return Command result
*/
public @NonNull CompletableFuture<CommandResult<C>> executeCommand(final @NonNull C commandSender,
final @NonNull String input) {
public @NonNull CompletableFuture<CommandResult<C>> executeCommand(
final @NonNull C commandSender,
final @NonNull String input
) {
final CommandContext<C> context = this.commandContextFactory.create(false, commandSender);
final LinkedList<String> inputQueue = tokenize(input);
try {
@ -157,14 +161,18 @@ public abstract class CommandManager<C> {
* @param input Input provided by the sender
* @return List of suggestions
*/
public @NonNull List<@NonNull String> suggest(final @NonNull C commandSender,
final @NonNull String input) {
public @NonNull List<@NonNull String> suggest(
final @NonNull C commandSender,
final @NonNull String input
) {
final CommandContext<C> context = this.commandContextFactory.create(true, commandSender);
final LinkedList<String> inputQueue = tokenize(input);
if (this.preprocessContext(context, inputQueue) == State.ACCEPTED) {
return this.commandSuggestionProcessor.apply(new CommandPreprocessingContext<>(context, inputQueue),
this.commandTree.getSuggestions(
context, inputQueue));
return this.commandSuggestionProcessor.apply(
new CommandPreprocessingContext<>(context, inputQueue),
this.commandTree.getSuggestions(
context, inputQueue)
);
} else {
return Collections.emptyList();
}
@ -231,8 +239,10 @@ public abstract class CommandManager<C> {
* @param permission Permission node
* @return {@code true} if the sender has the permission, else {@code false}
*/
public boolean hasPermission(final @NonNull C sender,
final @NonNull CommandPermission permission) {
public boolean hasPermission(
final @NonNull C sender,
final @NonNull CommandPermission permission
) {
if (permission.toString().isEmpty()) {
return true;
}
@ -269,10 +279,12 @@ public abstract class CommandManager<C> {
* @param meta Command meta
* @return Builder instance
*/
public Command.@NonNull Builder<C> commandBuilder(final @NonNull String name,
final @NonNull Collection<String> aliases,
final @NonNull Description description,
final @NonNull CommandMeta meta) {
public Command.@NonNull Builder<C> commandBuilder(
final @NonNull String name,
final @NonNull Collection<String> aliases,
final @NonNull Description description,
final @NonNull CommandMeta meta
) {
return Command.newBuilder(name, meta, description, aliases.toArray(new String[0]));
}
@ -284,9 +296,11 @@ public abstract class CommandManager<C> {
* @param meta Command meta
* @return Builder instance
*/
public Command.@NonNull Builder<C> commandBuilder(final @NonNull String name,
final @NonNull Collection<String> aliases,
final @NonNull CommandMeta meta) {
public Command.@NonNull Builder<C> commandBuilder(
final @NonNull String name,
final @NonNull Collection<String> aliases,
final @NonNull CommandMeta meta
) {
return Command.newBuilder(name, meta, Description.empty(), aliases.toArray(new String[0]));
}
@ -299,10 +313,12 @@ public abstract class CommandManager<C> {
* @param aliases Command aliases
* @return Builder instance
*/
public Command.@NonNull Builder<C> commandBuilder(final @NonNull String name,
final @NonNull CommandMeta meta,
final @NonNull Description description,
final @NonNull String... aliases) {
public Command.@NonNull Builder<C> commandBuilder(
final @NonNull String name,
final @NonNull CommandMeta meta,
final @NonNull Description description,
final @NonNull String... aliases
) {
return Command.newBuilder(name, meta, description, aliases);
}
@ -314,9 +330,11 @@ public abstract class CommandManager<C> {
* @param aliases Command aliases
* @return Builder instance
*/
public Command.@NonNull Builder<C> commandBuilder(final @NonNull String name,
final @NonNull CommandMeta meta,
final @NonNull String... aliases) {
public Command.@NonNull Builder<C> commandBuilder(
final @NonNull String name,
final @NonNull CommandMeta meta,
final @NonNull String... aliases
) {
return Command.newBuilder(name, meta, Description.empty(), aliases);
}
@ -330,23 +348,27 @@ public abstract class CommandManager<C> {
* @throws UnsupportedOperationException If the command manager does not support default command meta creation
* @see #createDefaultCommandMeta() Default command meta creation
*/
public Command.@NonNull Builder<C> commandBuilder(final @NonNull String name,
final @NonNull Description description,
final @NonNull String... aliases) {
public Command.@NonNull Builder<C> commandBuilder(
final @NonNull String name,
final @NonNull Description description,
final @NonNull String... aliases
) {
return Command.<C>newBuilder(name, this.createDefaultCommandMeta(), description, aliases).manager(this);
}
/**
* Create a new command builder using a default command meta instance.
*
* @param name Command name
* @param aliases Command aliases
* @param name Command name
* @param aliases Command aliases
* @return Builder instance
* @throws UnsupportedOperationException If the command manager does not support default command meta creation
* @see #createDefaultCommandMeta() Default command meta creation
*/
public Command.@NonNull Builder<C> commandBuilder(final @NonNull String name,
final @NonNull String... aliases) {
public Command.@NonNull Builder<C> commandBuilder(
final @NonNull String name,
final @NonNull String... aliases
) {
return Command.<C>newBuilder(name, this.createDefaultCommandMeta(), Description.empty(), aliases).manager(this);
}
@ -358,8 +380,10 @@ public abstract class CommandManager<C> {
* @param <T> Generic argument name
* @return Argument builder
*/
public <T> CommandArgument.@NonNull Builder<C, T> argumentBuilder(final @NonNull Class<T> type,
final @NonNull String name) {
public <T> CommandArgument.@NonNull Builder<C, T> argumentBuilder(
final @NonNull Class<T> type,
final @NonNull String name
) {
return CommandArgument.<C, T>ofType(type, name).manager(this);
}
@ -401,7 +425,8 @@ public abstract class CommandManager<C> {
public void registerCommandPreProcessor(final @NonNull CommandPreprocessor<C> processor) {
this.servicePipeline.registerServiceImplementation(new TypeToken<CommandPreprocessor<C>>() {
}, processor,
Collections.emptyList());
Collections.emptyList()
);
}
/**
@ -414,7 +439,8 @@ public abstract class CommandManager<C> {
public void registerCommandPostProcessor(final @NonNull CommandPostprocessor<C> processor) {
this.servicePipeline.registerServiceImplementation(new TypeToken<CommandPostprocessor<C>>() {
}, processor,
Collections.emptyList());
Collections.emptyList()
);
}
/**
@ -425,15 +451,17 @@ public abstract class CommandManager<C> {
* @return {@link State#ACCEPTED} if the command should be parsed and executed, else {@link State#REJECTED}
* @see #registerCommandPreProcessor(CommandPreprocessor) Register a command preprocessor
*/
public State preprocessContext(final @NonNull CommandContext<C> context,
final @NonNull LinkedList<@NonNull String> inputQueue) {
public State preprocessContext(
final @NonNull CommandContext<C> context,
final @NonNull LinkedList<@NonNull String> inputQueue
) {
this.servicePipeline.pump(new CommandPreprocessingContext<>(context, inputQueue))
.through(new TypeToken<CommandPreprocessor<C>>() {
})
.getResult();
.through(new TypeToken<CommandPreprocessor<C>>() {
})
.getResult();
return context.<String>getOptional(AcceptingCommandPreprocessor.PROCESSED_INDICATOR_KEY).orElse("").isEmpty()
? State.REJECTED
: State.ACCEPTED;
? State.REJECTED
: State.ACCEPTED;
}
/**
@ -444,15 +472,17 @@ public abstract class CommandManager<C> {
* @return {@link State#ACCEPTED} if the command should be parsed and executed, else {@link State#REJECTED}
* @see #registerCommandPostProcessor(CommandPostprocessor) Register a command postprocessor
*/
public State postprocessContext(final @NonNull CommandContext<C> context,
final @NonNull Command<C> command) {
public State postprocessContext(
final @NonNull CommandContext<C> context,
final @NonNull Command<C> command
) {
this.servicePipeline.pump(new CommandPostprocessingContext<>(context, command))
.through(new TypeToken<CommandPostprocessor<C>>() {
})
.getResult();
.through(new TypeToken<CommandPostprocessor<C>>() {
})
.getResult();
return context.<String>getOptional(AcceptingCommandPostprocessor.PROCESSED_INDICATOR_KEY).orElse("").isEmpty()
? State.REJECTED
: State.ACCEPTED;
? State.REJECTED
: State.ACCEPTED;
}
/**
@ -520,8 +550,10 @@ public abstract class CommandManager<C> {
* @param handler Exception handler
* @param <E> Exception type
*/
public final <E extends Exception> void registerExceptionHandler(final @NonNull Class<E> clazz,
final @NonNull BiConsumer<@NonNull C, @NonNull E> handler) {
public final <E extends Exception> void registerExceptionHandler(
final @NonNull Class<E> clazz,
final @NonNull BiConsumer<@NonNull C, @NonNull E> handler
) {
this.exceptionHandlers.put(clazz, handler);
}
@ -536,10 +568,12 @@ public abstract class CommandManager<C> {
* handler stored for the exception type
* @param <E> Exception type
*/
public final <E extends Exception> void handleException(final @NonNull C sender,
final @NonNull Class<E> clazz,
final @NonNull E exception,
final @NonNull BiConsumer<C, E> defaultHandler) {
public final <E extends Exception> void handleException(
final @NonNull C sender,
final @NonNull Class<E> clazz,
final @NonNull E exception,
final @NonNull BiConsumer<C, E> defaultHandler
) {
Optional.ofNullable(this.getExceptionHandler(clazz)).orElse(defaultHandler).accept(sender, exception);
}
@ -557,7 +591,7 @@ public abstract class CommandManager<C> {
* of command help menus, etc.
*
* @return Command help handler. A new instance will be created
* each time this method is called.
* each time this method is called.
*/
public final @NonNull CommandHelpHandler<C> getCommandHelpHandler() {
return new CommandHelpHandler<>(this);
@ -580,8 +614,10 @@ public abstract class CommandManager<C> {
* @param value Value
*/
@SuppressWarnings("unused")
public void setSetting(final @NonNull ManagerSettings setting,
final boolean value) {
public void setSetting(
final @NonNull ManagerSettings setting,
final boolean value
) {
if (value) {
this.managerSettings.add(setting);
} else {

View file

@ -112,45 +112,58 @@ public final class CommandTree<C> {
* @param args Input
* @return Parsed command, if one could be found
*/
public @NonNull Pair<@Nullable Command<C>, @Nullable Exception> parse(final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> args) {
final Pair<@Nullable Command<C>, @Nullable Exception> pair = this.parseCommand(new ArrayList<>(),
commandContext,
args,
this.internalTree);
public @NonNull Pair<@Nullable Command<C>, @Nullable Exception> parse(
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> args
) {
final Pair<@Nullable Command<C>, @Nullable Exception> pair = this.parseCommand(
new ArrayList<>(),
commandContext,
args,
this.internalTree
);
if (pair.getFirst() != null) {
final Command<C> command = pair.getFirst();
if (command.getSenderType().isPresent() && !command.getSenderType().get()
.isAssignableFrom(commandContext.getSender().getClass())) {
return Pair.of(null, new InvalidCommandSenderException(commandContext.getSender(),
command.getSenderType().get(),
Collections.emptyList()));
.isAssignableFrom(commandContext
.getSender()
.getClass())) {
return Pair.of(null, new InvalidCommandSenderException(
commandContext.getSender(),
command.getSenderType().get(),
Collections.emptyList()
));
}
}
return pair;
}
private @NonNull Pair<@Nullable Command<C>, @Nullable Exception>
parseCommand(final @NonNull List<@NonNull CommandArgument<C, ?>> parsedArguments,
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> commandQueue,
final @NonNull Node<@Nullable CommandArgument<C, ?>> root) {
private @NonNull Pair<@Nullable Command<C>, @Nullable Exception> parseCommand(
final @NonNull List<@NonNull CommandArgument<C, ?>> parsedArguments,
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> commandQueue,
final @NonNull Node<@Nullable CommandArgument<C, ?>> root
) {
CommandPermission permission = this.isPermitted(commandContext.getSender(), root);
if (permission != null) {
return Pair.of(null, new NoPermissionException(permission,
commandContext.getSender(),
this.getChain(root)
.stream()
.filter(node -> node.getValue() != null)
.map(Node::getValue)
.collect(Collectors.toList())));
return Pair.of(null, new NoPermissionException(
permission,
commandContext.getSender(),
this.getChain(root)
.stream()
.filter(node -> node.getValue() != null)
.map(Node::getValue)
.collect(Collectors.toList())
));
}
final Pair<@Nullable Command<C>, @Nullable Exception> parsedChild = this.attemptParseUnambiguousChild(parsedArguments,
commandContext,
root,
commandQueue);
final Pair<@Nullable Command<C>, @Nullable Exception> parsedChild = this.attemptParseUnambiguousChild(
parsedArguments,
commandContext,
root,
commandQueue
);
if (parsedChild.getFirst() != null || parsedChild.getSecond() != null) {
return parsedChild;
}
@ -165,23 +178,25 @@ public final class CommandTree<C> {
/* Too many arguments. We have a unique path, so we can send the entire context */
return Pair.of(null, new InvalidSyntaxException(
this.commandManager.getCommandSyntaxFormatter()
.apply(parsedArguments, root),
.apply(parsedArguments, root),
commandContext.getSender(), this.getChain(root)
.stream()
.filter(node -> node.getValue() != null)
.map(Node::getValue)
.collect(Collectors.toList())));
.stream()
.filter(node -> node.getValue() != null)
.map(Node::getValue)
.collect(Collectors.toList())
));
}
} else {
/* Too many arguments. We have a unique path, so we can send the entire context */
return Pair.of(null, new InvalidSyntaxException(
this.commandManager.getCommandSyntaxFormatter()
.apply(parsedArguments, root),
.apply(parsedArguments, root),
commandContext.getSender(), this.getChain(root)
.stream()
.filter(node -> node.getValue() != null)
.map(Node::getValue)
.collect(Collectors.toList())));
.stream()
.filter(node -> node.getValue() != null)
.map(Node::getValue)
.collect(Collectors.toList())
));
}
} else {
final Iterator<Node<CommandArgument<C, ?>>> childIterator = root.getChildren().iterator();
@ -208,33 +223,38 @@ public final class CommandTree<C> {
return Pair.of(null, new NoSuchCommandException(
commandContext.getSender(),
getChain(root).stream().map(Node::getValue).collect(Collectors.toList()),
stringOrEmpty(commandQueue.peek())));
stringOrEmpty(commandQueue.peek())
));
}
/* If we couldn't match a child, check if there's a command attached and execute it */
if (root.getValue() != null && root.getValue().getOwningCommand() != null && commandQueue.isEmpty()) {
final Command<C> command = root.getValue().getOwningCommand();
if (!this.getCommandManager().hasPermission(commandContext.getSender(),
command.getCommandPermission())) {
if (!this.getCommandManager().hasPermission(
commandContext.getSender(),
command.getCommandPermission()
)) {
return Pair.of(null, new NoPermissionException(
command.getCommandPermission(),
commandContext.getSender(),
this.getChain(root)
.stream()
.filter(node -> node.getValue() != null)
.map(Node::getValue)
.collect(Collectors.toList())));
.stream()
.filter(node -> node.getValue() != null)
.map(Node::getValue)
.collect(Collectors.toList())
));
}
return Pair.of(root.getValue().getOwningCommand(), null);
}
/* We know that there's no command and we also cannot match any of the children */
return Pair.of(null, new InvalidSyntaxException(
this.commandManager.getCommandSyntaxFormatter()
.apply(parsedArguments, root),
.apply(parsedArguments, root),
commandContext.getSender(), this.getChain(root)
.stream()
.filter(node -> node.getValue() != null)
.map(Node::getValue)
.collect(Collectors.toList())));
.stream()
.filter(node -> node.getValue() != null)
.map(Node::getValue)
.collect(Collectors.toList())
));
}
}
@ -242,7 +262,8 @@ public final class CommandTree<C> {
final @NonNull List<@NonNull CommandArgument<C, ?>> parsedArguments,
final @NonNull CommandContext<C> commandContext,
final @NonNull Node<@Nullable CommandArgument<C, ?>> root,
final @NonNull Queue<String> commandQueue) {
final @NonNull Queue<String> commandQueue
) {
CommandPermission permission;
final List<Node<CommandArgument<C, ?>>> children = root.getChildren();
if (children.size() == 1 && !(children.get(0).getValue() instanceof StaticArgument)) {
@ -254,11 +275,11 @@ public final class CommandTree<C> {
permission,
commandContext.getSender(),
this.getChain(child)
.stream()
.filter(node -> node.getValue() != null)
.map(Node::getValue)
.collect(
Collectors.toList())));
.stream()
.filter(node -> node.getValue() != null)
.map(Node::getValue)
.collect(Collectors.toList())
));
}
if (child.getValue() != null) {
if (commandQueue.isEmpty()) {
@ -270,58 +291,65 @@ public final class CommandTree<C> {
/* The child is not a leaf, but may have an intermediary executor, attempt to use it */
if (root.getValue() != null && root.getValue().getOwningCommand() != null) {
final Command<C> command = root.getValue().getOwningCommand();
if (!this.getCommandManager().hasPermission(commandContext.getSender(),
command.getCommandPermission())) {
if (!this.getCommandManager().hasPermission(
commandContext.getSender(),
command.getCommandPermission()
)) {
return Pair.of(null, new NoPermissionException(
command.getCommandPermission(),
commandContext.getSender(),
this.getChain(root)
.stream()
.filter(node -> node.getValue() != null)
.map(Node::getValue)
.collect(Collectors.toList())));
.stream()
.filter(node -> node.getValue() != null)
.map(Node::getValue)
.collect(Collectors.toList())
));
}
return Pair.of(command, null);
}
/* Not enough arguments */
return Pair.of(null, new InvalidSyntaxException(
this.commandManager.getCommandSyntaxFormatter()
.apply(Objects.requireNonNull(
child.getValue()
.getOwningCommand())
.getArguments(), child),
.apply(Objects.requireNonNull(
child.getValue()
.getOwningCommand())
.getArguments(), child),
commandContext.getSender(), this.getChain(root)
.stream()
.filter(node -> node.getValue() != null)
.map(Node::getValue)
.collect(
Collectors.toList())));
.stream()
.filter(node -> node.getValue() != null)
.map(Node::getValue)
.collect(Collectors.toList())
));
} else {
/* The child is not a leaf, but may have an intermediary executor, attempt to use it */
if (root.getValue() != null && root.getValue().getOwningCommand() != null) {
final Command<C> command = root.getValue().getOwningCommand();
if (!this.getCommandManager().hasPermission(commandContext.getSender(),
command.getCommandPermission())) {
if (!this.getCommandManager().hasPermission(
commandContext.getSender(),
command.getCommandPermission()
)) {
return Pair.of(null, new NoPermissionException(
command.getCommandPermission(),
commandContext.getSender(),
this.getChain(root)
.stream()
.filter(node -> node.getValue() != null)
.map(Node::getValue)
.collect(Collectors.toList())));
.stream()
.filter(node -> node.getValue() != null)
.map(Node::getValue)
.collect(Collectors.toList())
));
}
return Pair.of(command, null);
}
/* Child does not have a command and so we cannot proceed */
return Pair.of(null, new InvalidSyntaxException(
this.commandManager.getCommandSyntaxFormatter()
.apply(parsedArguments, root),
.apply(parsedArguments, root),
commandContext.getSender(), this.getChain(root)
.stream()
.filter(node -> node.getValue() != null)
.map(Node::getValue)
.collect(Collectors.toList())));
.stream()
.filter(node -> node.getValue() != null)
.map(Node::getValue)
.collect(Collectors.toList())
));
}
}
@ -341,13 +369,13 @@ public final class CommandTree<C> {
/* Too many arguments. We have a unique path, so we can send the entire context */
return Pair.of(null, new InvalidSyntaxException(
this.commandManager.getCommandSyntaxFormatter()
.apply(parsedArguments, child),
.apply(parsedArguments, child),
commandContext.getSender(), this.getChain(root)
.stream()
.filter(node -> node.getValue() != null)
.map(Node::getValue)
.collect(
Collectors.toList())));
.stream()
.filter(node -> node.getValue() != null)
.map(Node::getValue)
.collect(Collectors.toList())
));
}
} else {
parsedArguments.add(child.getValue());
@ -357,10 +385,11 @@ public final class CommandTree<C> {
return Pair.of(null, new ArgumentParseException(
result.getFailure().get(), commandContext.getSender(),
this.getChain(child)
.stream()
.filter(node -> node.getValue() != null)
.map(Node::getValue)
.collect(Collectors.toList())));
.stream()
.filter(node -> node.getValue() != null)
.map(Node::getValue)
.collect(Collectors.toList())
));
}
}
}
@ -374,14 +403,18 @@ public final class CommandTree<C> {
* @param commandQueue Input queue
* @return String suggestions. These should be filtered based on {@link String#startsWith(String)}
*/
public @NonNull List<@NonNull String> getSuggestions(final @NonNull CommandContext<C> context,
final @NonNull Queue<@NonNull String> commandQueue) {
public @NonNull List<@NonNull String> getSuggestions(
final @NonNull CommandContext<C> context,
final @NonNull Queue<@NonNull String> commandQueue
) {
return getSuggestions(context, commandQueue, this.internalTree);
}
private @NonNull List<@NonNull String> getSuggestions(final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> commandQueue,
final @NonNull Node<@Nullable CommandArgument<C, ?>> root) {
private @NonNull List<@NonNull String> getSuggestions(
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> commandQueue,
final @NonNull Node<@Nullable CommandArgument<C, ?>> root
) {
/* If the sender isn't allowed to access the root node, no suggestions are needed */
if (this.isPermitted(commandContext.getSender(), root) != null) {
@ -395,8 +428,8 @@ public final class CommandTree<C> {
// START: Compound arguments
/* When we get in here, we need to treat compound arguments a little differently */
if (child.getValue() instanceof CompoundArgument) {
@SuppressWarnings("unchecked")
final CompoundArgument<?, C, ?> compoundArgument = (CompoundArgument<?, C, ?>) child.getValue();
@SuppressWarnings("unchecked") final CompoundArgument<?, C, ?> compoundArgument = (CompoundArgument<?, C, ?>) child
.getValue();
/* See how many arguments it requires */
final int requiredArguments = compoundArgument.getParserTuple().getSize();
/* Figure out whether we even need to care about this */
@ -453,7 +486,10 @@ public final class CommandTree<C> {
while (childIterator.hasNext()) {
final Node<CommandArgument<C, ?>> child = childIterator.next();
if (child.getValue() != null) {
final ArgumentParseResult<?> result = child.getValue().getParser().parse(commandContext, commandQueue);
final ArgumentParseResult<?> result = child.getValue().getParser().parse(
commandContext,
commandQueue
);
if (result.getParsedValue().isPresent()) {
return this.getSuggestions(commandContext, commandQueue, child);
}
@ -466,7 +502,7 @@ public final class CommandTree<C> {
continue;
}
suggestions.addAll(argument.getValue().getSuggestionsProvider()
.apply(commandContext, stringOrEmpty(commandQueue.peek())));
.apply(commandContext, stringOrEmpty(commandQueue.peek())));
}
return suggestions;
}
@ -517,19 +553,27 @@ public final class CommandTree<C> {
}
}
private @Nullable CommandPermission isPermitted(final @NonNull C sender,
final @NonNull Node<@Nullable CommandArgument<C, ?>> node) {
private @Nullable CommandPermission isPermitted(
final @NonNull C sender,
final @NonNull Node<@Nullable CommandArgument<C, ?>> node
) {
final CommandPermission permission = (CommandPermission) node.nodeMeta.get("permission");
if (permission != null) {
return this.commandManager.hasPermission(sender, permission) ? null : permission;
}
if (node.isLeaf()) {
return this.commandManager.hasPermission(sender,
Objects.requireNonNull(
Objects.requireNonNull(node.value, "node.value").getOwningCommand(),
"owning command").getCommandPermission())
? null : Objects.requireNonNull(node.value.getOwningCommand(), "owning command")
.getCommandPermission();
return this.commandManager.hasPermission(
sender,
Objects.requireNonNull(
Objects.requireNonNull(
node.value,
"node.value"
).getOwningCommand(),
"owning command"
).getCommandPermission()
)
? null : Objects.requireNonNull(node.value.getOwningCommand(), "owning command")
.getCommandPermission();
}
/*
if any of the children would permit the execution, then the sender has a valid
@ -584,7 +628,8 @@ public final class CommandTree<C> {
chain = chain.subList(1, chain.size());
// Go through all nodes from the tail upwards until a collision occurs
for (final Node<CommandArgument<C, ?>> commandArgumentNode : chain) {
final CommandPermission existingPermission = (CommandPermission) commandArgumentNode.nodeMeta.get("permission");
final CommandPermission existingPermission = (CommandPermission) commandArgumentNode.nodeMeta
.get("permission");
CommandPermission permission;
if (existingPermission != null) {
@ -594,9 +639,13 @@ public final class CommandTree<C> {
}
/* Now also check if there's a command handler attached to an upper level node */
if (commandArgumentNode.getValue() != null && commandArgumentNode.getValue().getOwningCommand() != null) {
if (commandArgumentNode.getValue() != null && commandArgumentNode
.getValue()
.getOwningCommand() != null) {
final Command<C> command = commandArgumentNode.getValue().getOwningCommand();
if (this.getCommandManager().getSetting(CommandManager.ManagerSettings.ENFORCE_INTERMEDIARY_PERMISSIONS)) {
if (this
.getCommandManager()
.getSetting(CommandManager.ManagerSettings.ENFORCE_INTERMEDIARY_PERMISSIONS)) {
permission = command.getCommandPermission();
} else {
permission = OrPermission.of(Arrays.asList(permission, command.getCommandPermission()));
@ -608,26 +657,30 @@ public final class CommandTree<C> {
});
}
private void checkAmbiguity(final @NonNull Node<@Nullable CommandArgument<C, ?>> node) throws AmbiguousNodeException {
private void checkAmbiguity(final @NonNull Node<@Nullable CommandArgument<C, ?>> node) throws
AmbiguousNodeException {
if (node.isLeaf()) {
return;
}
final int size = node.children.size();
for (final Node<CommandArgument<C, ?>> child : node.children) {
if (child.getValue() != null && !child.getValue().isRequired() && size > 1) {
throw new AmbiguousNodeException(node.getValue(),
child.getValue(),
node.getChildren()
.stream()
.filter(n -> n.getValue() != null)
.map(Node::getValue).collect(Collectors.toList()));
throw new AmbiguousNodeException(
node.getValue(),
child.getValue(),
node.getChildren()
.stream()
.filter(n -> n.getValue() != null)
.map(Node::getValue).collect(Collectors.toList())
);
}
}
node.children.forEach(this::checkAmbiguity);
}
private @NonNull List<@NonNull Node<@Nullable CommandArgument<C, ?>>>
getLeavesRaw(final @NonNull Node<@Nullable CommandArgument<C, ?>> node) {
private @NonNull List<@NonNull Node<@Nullable CommandArgument<C, ?>>> getLeavesRaw(
final @NonNull Node<@Nullable CommandArgument<C, ?>> node
) {
final List<Node<CommandArgument<C, ?>>> leaves = new LinkedList<>();
if (node.isLeaf()) {
if (node.getValue() != null) {
@ -639,7 +692,9 @@ public final class CommandTree<C> {
return leaves;
}
private @NonNull List<@NonNull CommandArgument<C, ?>> getLeaves(final @NonNull Node<@NonNull CommandArgument<C, ?>> node) {
private @NonNull List<@NonNull CommandArgument<C, ?>> getLeaves(
final @NonNull Node<@NonNull CommandArgument<C, ?>> node
) {
final List<CommandArgument<C, ?>> leaves = new LinkedList<>();
if (node.isLeaf()) {
if (node.getValue() != null) {
@ -651,8 +706,9 @@ public final class CommandTree<C> {
return leaves;
}
private @NonNull List<@NonNull Node<@Nullable CommandArgument<C, ?>>>
getChain(final @Nullable Node<@Nullable CommandArgument<C, ?>> end) {
private @NonNull List<@NonNull Node<@Nullable CommandArgument<C, ?>>> getChain(
final @Nullable Node<@Nullable CommandArgument<C, ?>> end
) {
final List<Node<CommandArgument<C, ?>>> chain = new LinkedList<>();
Node<CommandArgument<C, ?>> tail = end;
while (tail != null) {
@ -812,6 +868,7 @@ public final class CommandTree<C> {
public String toString() {
return "Node{value=" + value + '}';
}
}
}

View file

@ -67,7 +67,7 @@ public final class Description {
* @return Command description
*/
public @NonNull String getDescription() {
return this.description;
return this.description;
}
/**

View file

@ -34,4 +34,5 @@ import java.lang.annotation.Target;
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface Greedy {
}

View file

@ -23,13 +23,13 @@
//
package cloud.commandframework.arguments;
import cloud.commandframework.arguments.parser.ArgumentParser;
import io.leangen.geantyref.TypeToken;
import cloud.commandframework.Command;
import cloud.commandframework.CommandManager;
import cloud.commandframework.arguments.parser.ArgumentParseResult;
import cloud.commandframework.arguments.parser.ArgumentParser;
import cloud.commandframework.arguments.parser.ParserParameters;
import cloud.commandframework.context.CommandContext;
import io.leangen.geantyref.TypeToken;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
@ -99,12 +99,14 @@ public class CommandArgument<C, T> implements Comparable<CommandArgument<?, ?>>
* @param valueType Type produced by the parser
* @param suggestionsProvider Suggestions provider
*/
public CommandArgument(final boolean required,
final @NonNull String name,
final @NonNull ArgumentParser<C, T> parser,
final @NonNull String defaultValue,
final @NonNull TypeToken<T> valueType,
final @Nullable BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider) {
public CommandArgument(
final boolean required,
final @NonNull String name,
final @NonNull ArgumentParser<C, T> parser,
final @NonNull String defaultValue,
final @NonNull TypeToken<T> valueType,
final @Nullable BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider
) {
this.required = required;
this.name = Objects.requireNonNull(name, "Name may not be null");
if (!NAME_PATTERN.asPredicate().test(name)) {
@ -114,8 +116,8 @@ public class CommandArgument<C, T> implements Comparable<CommandArgument<?, ?>>
this.defaultValue = defaultValue;
this.valueType = valueType;
this.suggestionsProvider = suggestionsProvider == null
? buildDefaultSuggestionsProvider(this)
: suggestionsProvider;
? buildDefaultSuggestionsProvider(this)
: suggestionsProvider;
}
/**
@ -128,13 +130,15 @@ public class CommandArgument<C, T> implements Comparable<CommandArgument<?, ?>>
* @param valueType Type produced by the parser
* @param suggestionsProvider Suggestions provider
*/
public CommandArgument(final boolean required,
final @NonNull String name,
final @NonNull ArgumentParser<C, T> parser,
final @NonNull String defaultValue,
final @NonNull Class<T> valueType,
final @Nullable BiFunction<@NonNull CommandContext<C>,
@NonNull String, @NonNull List<@NonNull String>> suggestionsProvider) {
public CommandArgument(
final boolean required,
final @NonNull String name,
final @NonNull ArgumentParser<C, T> parser,
final @NonNull String defaultValue,
final @NonNull Class<T> valueType,
final @Nullable BiFunction<@NonNull CommandContext<C>,
@NonNull String, @NonNull List<@NonNull String>> suggestionsProvider
) {
this(required, name, parser, defaultValue, TypeToken.get(valueType), suggestionsProvider);
}
@ -146,10 +150,12 @@ public class CommandArgument<C, T> implements Comparable<CommandArgument<?, ?>>
* @param parser The argument parser
* @param valueType Type produced by the parser
*/
public CommandArgument(final boolean required,
final @NonNull String name,
final @NonNull ArgumentParser<C, T> parser,
final @NonNull Class<T> valueType) {
public CommandArgument(
final boolean required,
final @NonNull String name,
final @NonNull ArgumentParser<C, T> parser,
final @NonNull Class<T> valueType
) {
this(required, name, parser, "", valueType, null);
}
@ -167,8 +173,10 @@ public class CommandArgument<C, T> implements Comparable<CommandArgument<?, ?>>
* @param <T> Argument Type. Used to make the compiler happy.
* @return Argument builder
*/
public static <C, T> CommandArgument.@NonNull Builder<C, T> ofType(final @NonNull TypeToken<T> clazz,
final @NonNull String name) {
public static <C, T> CommandArgument.@NonNull Builder<C, T> ofType(
final @NonNull TypeToken<T> clazz,
final @NonNull String name
) {
return new Builder<>(clazz, name);
}
@ -181,8 +189,10 @@ public class CommandArgument<C, T> implements Comparable<CommandArgument<?, ?>>
* @param <T> Argument Type. Used to make the compiler happy.
* @return Argument builder
*/
public static <C, T> CommandArgument.@NonNull Builder<@NonNull C, @NonNull T> ofType(final @NonNull Class<T> clazz,
final @NonNull String name) {
public static <C, T> CommandArgument.@NonNull Builder<@NonNull C, @NonNull T> ofType(
final @NonNull Class<T> clazz,
final @NonNull String name
) {
return new Builder<>(TypeToken.get(clazz), name);
}
@ -366,14 +376,18 @@ public class CommandArgument<C, T> implements Comparable<CommandArgument<?, ?>>
private String defaultValue = "";
private BiFunction<@NonNull CommandContext<C>, @NonNull String, @NonNull List<String>> suggestionsProvider;
protected Builder(final @NonNull TypeToken<T> valueType,
final @NonNull String name) {
protected Builder(
final @NonNull TypeToken<T> valueType,
final @NonNull String name
) {
this.valueType = valueType;
this.name = name;
}
protected Builder(final @NonNull Class<T> valueType,
final @NonNull String name) {
protected Builder(
final @NonNull Class<T> valueType,
final @NonNull String name
) {
this(TypeToken.get(valueType), name);
}
@ -452,7 +466,8 @@ public class CommandArgument<C, T> implements Comparable<CommandArgument<?, ?>>
*/
public @NonNull Builder<@NonNull C, @NonNull T> withSuggestionsProvider(
final @NonNull BiFunction<@NonNull CommandContext<C>,
@NonNull String, @NonNull List<String>> suggestionsProvider) {
@NonNull String, @NonNull List<String>> suggestionsProvider
) {
this.suggestionsProvider = suggestionsProvider;
return this;
}
@ -465,7 +480,7 @@ public class CommandArgument<C, T> implements Comparable<CommandArgument<?, ?>>
public @NonNull CommandArgument<@NonNull C, @NonNull T> build() {
if (this.parser == null && this.manager != null) {
this.parser = this.manager.getParserRegistry().createParser(valueType, ParserParameters.empty())
.orElse(null);
.orElse(null);
}
if (this.parser == null) {
this.parser = (c, i) -> ArgumentParseResult
@ -475,7 +490,8 @@ public class CommandArgument<C, T> implements Comparable<CommandArgument<?, ?>>
this.suggestionsProvider = new DelegatingSuggestionsProvider<>(this.name, this.parser);
}
return new CommandArgument<>(this.required, this.name, this.parser,
this.defaultValue, this.valueType, this.suggestionsProvider);
this.defaultValue, this.valueType, this.suggestionsProvider
);
}
protected final @NonNull String getName() {

View file

@ -44,7 +44,9 @@ public interface CommandSyntaxFormatter<C> {
* @param node Trailing node
* @return Syntax string
*/
@NonNull String apply(@NonNull List<@NonNull CommandArgument<C, ?>> commandArguments,
CommandTree.@Nullable Node<@Nullable CommandArgument<C, ?>> node);
@NonNull String apply(
@NonNull List<@NonNull CommandArgument<C, ?>> commandArguments,
CommandTree.@Nullable Node<@Nullable CommandArgument<C, ?>> node
);
}

View file

@ -49,7 +49,8 @@ final class DelegatingSuggestionsProvider<C> implements BiFunction<@NonNull Comm
@Override
public String toString() {
return String.format("DelegatingSuggestionsProvider{name='%s',parser='%s'}", this.argumentName,
this.parser.getClass().getCanonicalName());
this.parser.getClass().getCanonicalName()
);
}
}

View file

@ -46,8 +46,10 @@ import java.util.List;
public class StandardCommandSyntaxFormatter<C> implements CommandSyntaxFormatter<C> {
@Override
public final @NonNull String apply(final @NonNull List<@NonNull CommandArgument<C, ?>> commandArguments,
final CommandTree.@Nullable Node<@Nullable CommandArgument<C, ?>> node) {
public final @NonNull String apply(
final @NonNull List<@NonNull CommandArgument<C, ?>> commandArguments,
final CommandTree.@Nullable Node<@Nullable CommandArgument<C, ?>> node
) {
final StringBuilder stringBuilder = new StringBuilder();
final Iterator<CommandArgument<C, ?>> iterator = commandArguments.iterator();
while (iterator.hasNext()) {
@ -73,8 +75,9 @@ public class StandardCommandSyntaxFormatter<C> implements CommandSyntaxFormatter
if (commandArgument instanceof FlagArgument) {
final StringBuilder flagBuilder = new StringBuilder();
@SuppressWarnings("unchecked")
final Iterator<CommandFlag<?>> flagIterator = ((FlagArgument<C>) commandArgument).getFlags().iterator();
@SuppressWarnings("unchecked") final Iterator<CommandFlag<?>> flagIterator = ((FlagArgument<C>) commandArgument)
.getFlags()
.iterator();
while (flagIterator.hasNext()) {
final CommandFlag<?> flag = flagIterator.next();
flagBuilder.append("--").append(flag.getName());
@ -140,8 +143,9 @@ public class StandardCommandSyntaxFormatter<C> implements CommandSyntaxFormatter
stringBuilder.append(suffix);
} else if (argument instanceof FlagArgument) {
final StringBuilder flagBuilder = new StringBuilder();
@SuppressWarnings("unchecked")
final Iterator<CommandFlag<?>> flagIterator = ((FlagArgument<C>) argument).getFlags().iterator();
@SuppressWarnings("unchecked") final Iterator<CommandFlag<?>> flagIterator = ((FlagArgument<C>) argument)
.getFlags()
.iterator();
while (flagIterator.hasNext()) {
final CommandFlag<?> flag = flagIterator.next();
flagBuilder.append("--").append(flag.getName());
@ -153,14 +157,14 @@ public class StandardCommandSyntaxFormatter<C> implements CommandSyntaxFormatter
}
}
stringBuilder.append(" ")
.append(prefix)
.append(flagBuilder)
.append(suffix);
.append(prefix)
.append(flagBuilder)
.append(suffix);
} else {
stringBuilder.append(" ")
.append(prefix)
.append(argument.getName())
.append(suffix);
.append(prefix)
.append(argument.getName())
.append(suffix);
}
tail = tail.getChildren().get(0);
}

View file

@ -56,8 +56,10 @@ public final class StaticArgument<C> extends CommandArgument<C, String> {
* @param <C> Command sender type
* @return Constructed argument
*/
public static <C> @NonNull StaticArgument<C> of(final @NonNull String name,
final @NonNull String... aliases) {
public static <C> @NonNull StaticArgument<C> of(
final @NonNull String name,
final @NonNull String... aliases
) {
return new StaticArgument<>(true, name, aliases);
}
@ -104,8 +106,10 @@ public final class StaticArgument<C> extends CommandArgument<C, String> {
}
@Override
public @NonNull ArgumentParseResult<String> parse(final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue) {
public @NonNull ArgumentParseResult<String> parse(
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue
) {
final String string = inputQueue.peek();
if (string == null) {
return ArgumentParseResult.failure(new NullPointerException("No input provided"));
@ -118,8 +122,10 @@ public final class StaticArgument<C> extends CommandArgument<C, String> {
}
@Override
public @NonNull List<@NonNull String> suggestions(final @NonNull CommandContext<C> commandContext,
final @NonNull String input) {
public @NonNull List<@NonNull String> suggestions(
final @NonNull CommandContext<C> commandContext,
final @NonNull String input
) {
return Collections.singletonList(this.name);
}

View file

@ -54,13 +54,15 @@ public class ArgumentPair<C, U, V, O> extends CompoundArgument<Pair<U, V>, C, O>
* @param mapper Mapper that maps the sub-arguments to the output type
* @param valueType The output type
*/
protected ArgumentPair(final boolean required,
final @NonNull String name,
final @NonNull Pair<@NonNull String, @NonNull String> names,
final @NonNull Pair<@NonNull Class<U>, @NonNull Class<V>> types,
final @NonNull Pair<@NonNull ArgumentParser<C, U>, @NonNull ArgumentParser<C, V>> parserPair,
final @NonNull BiFunction<@NonNull C, @NonNull Pair<@NonNull U, @NonNull V>, @NonNull O> mapper,
final @NonNull TypeToken<O> valueType) {
protected ArgumentPair(
final boolean required,
final @NonNull String name,
final @NonNull Pair<@NonNull String, @NonNull String> names,
final @NonNull Pair<@NonNull Class<U>, @NonNull Class<V>> types,
final @NonNull Pair<@NonNull ArgumentParser<C, U>, @NonNull ArgumentParser<C, V>> parserPair,
final @NonNull BiFunction<@NonNull C, @NonNull Pair<@NonNull U, @NonNull V>, @NonNull O> mapper,
final @NonNull TypeToken<O> valueType
) {
super(required, name, names, parserPair, types, mapper, o -> Pair.of((U) o[0], (V) o[1]), valueType);
}
@ -78,21 +80,27 @@ public class ArgumentPair<C, U, V, O> extends CompoundArgument<Pair<U, V>, C, O>
* @param <V> Second parsed type
* @return Intermediary builder
*/
public static <C, U, V> @NonNull ArgumentPairIntermediaryBuilder<C, U, V> of(final @NonNull CommandManager<C> manager,
final @NonNull String name,
final @NonNull Pair<@NonNull String,
@NonNull String> names,
final @NonNull Pair<@NonNull Class<U>,
@NonNull Class<V>> types) {
public static <C, U, V> @NonNull ArgumentPairIntermediaryBuilder<C, U, V> of(
final @NonNull CommandManager<C> manager,
final @NonNull String name,
final @NonNull Pair<@NonNull String,
@NonNull String> names,
final @NonNull Pair<@NonNull Class<U>,
@NonNull Class<V>> types
) {
final ParserRegistry<C> parserRegistry = manager.getParserRegistry();
final ArgumentParser<C, U> firstParser = parserRegistry.createParser(TypeToken.get(types.getFirst()),
ParserParameters.empty()).orElseThrow(() ->
new IllegalArgumentException(
"Could not create parser for primary type"));
final ArgumentParser<C, V> secondaryParser = parserRegistry.createParser(TypeToken.get(types.getSecond()),
ParserParameters.empty()).orElseThrow(() ->
new IllegalArgumentException(
"Could not create parser for secondary type"));
final ArgumentParser<C, U> firstParser = parserRegistry.createParser(
TypeToken.get(types.getFirst()),
ParserParameters.empty()
).orElseThrow(() ->
new IllegalArgumentException(
"Could not create parser for primary type"));
final ArgumentParser<C, V> secondaryParser = parserRegistry.createParser(
TypeToken.get(types.getSecond()),
ParserParameters.empty()
).orElseThrow(() ->
new IllegalArgumentException(
"Could not create parser for secondary type"));
return new ArgumentPairIntermediaryBuilder<>(true, name, names, Pair.of(firstParser, secondaryParser), types);
}
@ -105,12 +113,14 @@ public class ArgumentPair<C, U, V, O> extends CompoundArgument<Pair<U, V>, C, O>
private final Pair<String, String> names;
private final Pair<Class<U>, Class<V>> types;
private ArgumentPairIntermediaryBuilder(final boolean required,
final @NonNull String name,
final @NonNull Pair<@NonNull String, @NonNull String> names,
final @NonNull Pair<@NonNull ArgumentParser<@NonNull C, @NonNull U>,
@NonNull ArgumentParser<@NonNull C, @NonNull V>> parserPair,
final @NonNull Pair<@NonNull Class<U>, @NonNull Class<V>> types) {
private ArgumentPairIntermediaryBuilder(
final boolean required,
final @NonNull String name,
final @NonNull Pair<@NonNull String, @NonNull String> names,
final @NonNull Pair<@NonNull ArgumentParser<@NonNull C, @NonNull U>,
@NonNull ArgumentParser<@NonNull C, @NonNull V>> parserPair,
final @NonNull Pair<@NonNull Class<U>, @NonNull Class<V>> types
) {
this.required = required;
this.name = name;
this.names = names;
@ -124,14 +134,16 @@ public class ArgumentPair<C, U, V, O> extends CompoundArgument<Pair<U, V>, C, O>
* @return Argument pair
*/
public @NonNull ArgumentPair<@NonNull C, @NonNull U, @NonNull V, @NonNull Pair<@NonNull U, @NonNull V>> simple() {
return new ArgumentPair<C, U, V, Pair<U, V>>(this.required,
this.name,
this.names,
this.types,
this.parserPair,
(sender, pair) -> pair,
new TypeToken<Pair<U, V>>() {
});
return new ArgumentPair<C, U, V, Pair<U, V>>(
this.required,
this.name,
this.names,
this.types,
this.parserPair,
(sender, pair) -> pair,
new TypeToken<Pair<U, V>>() {
}
);
}
/**
@ -142,9 +154,11 @@ public class ArgumentPair<C, U, V, O> extends CompoundArgument<Pair<U, V>, C, O>
* @param <O> Output type
* @return Created pair
*/
public <O> @NonNull ArgumentPair<C, U, V, O> withMapper(final @NonNull TypeToken<O> clazz,
final @NonNull BiFunction<@NonNull C, @NonNull Pair<@NonNull U,
@NonNull V>, @NonNull O> mapper) {
public <O> @NonNull ArgumentPair<C, U, V, O> withMapper(
final @NonNull TypeToken<O> clazz,
final @NonNull BiFunction<@NonNull C, @NonNull Pair<@NonNull U,
@NonNull V>, @NonNull O> mapper
) {
return new ArgumentPair<C, U, V, O>(this.required, this.name, this.names, this.types, this.parserPair, mapper, clazz);
}
@ -158,7 +172,8 @@ public class ArgumentPair<C, U, V, O> extends CompoundArgument<Pair<U, V>, C, O>
*/
public <O> @NonNull ArgumentPair<@NonNull C, @NonNull U, @NonNull V, @NonNull O> withMapper(
final @NonNull Class<O> clazz,
final @NonNull BiFunction<@NonNull C, @NonNull Pair<@NonNull U, @NonNull V>, @NonNull O> mapper) {
final @NonNull BiFunction<@NonNull C, @NonNull Pair<@NonNull U, @NonNull V>, @NonNull O> mapper
) {
return this.withMapper(TypeToken.get(clazz), mapper);
}

View file

@ -55,15 +55,17 @@ public class ArgumentTriplet<C, U, V, W, O> extends CompoundArgument<Triplet<U,
* @param mapper Mapper that maps the sub-arguments to the output type
* @param valueType The output type
*/
protected ArgumentTriplet(final boolean required,
final @NonNull String name,
final @NonNull Triplet<@NonNull String, @NonNull String, @NonNull String> names,
final @NonNull Triplet<@NonNull Class<U>, @NonNull Class<V>, @NonNull Class<W>> types,
final @NonNull Triplet<@NonNull ArgumentParser<C, U>, @NonNull ArgumentParser<C, V>,
@NonNull ArgumentParser<C, W>> parserTriplet,
final @NonNull BiFunction<@NonNull C,
@NonNull Triplet<U, @NonNull V, @NonNull W>, @NonNull O> mapper,
final @NonNull TypeToken<O> valueType) {
protected ArgumentTriplet(
final boolean required,
final @NonNull String name,
final @NonNull Triplet<@NonNull String, @NonNull String, @NonNull String> names,
final @NonNull Triplet<@NonNull Class<U>, @NonNull Class<V>, @NonNull Class<W>> types,
final @NonNull Triplet<@NonNull ArgumentParser<C, U>, @NonNull ArgumentParser<C, V>,
@NonNull ArgumentParser<C, W>> parserTriplet,
final @NonNull BiFunction<@NonNull C,
@NonNull Triplet<U, @NonNull V, @NonNull W>, @NonNull O> mapper,
final @NonNull TypeToken<O> valueType
) {
super(required, name, names, parserTriplet, types, mapper, o -> Triplet.of((U) o[0], (V) o[1], (W) o[2]), valueType);
}
@ -83,25 +85,34 @@ public class ArgumentTriplet<C, U, V, W, O> extends CompoundArgument<Triplet<U,
* @return Intermediary builder
*/
public static <C, U, V, W> @NonNull ArgumentTripletIntermediaryBuilder<@NonNull C, @NonNull U, @NonNull V, @NonNull W>
of(final @NonNull CommandManager<C> manager,
final @NonNull String name,
final @NonNull Triplet<@NonNull String, @NonNull String, @NonNull String> names,
final @NonNull Triplet<@NonNull Class<U>, @NonNull Class<V>, @NonNull Class<W>> types) {
of(
final @NonNull CommandManager<C> manager,
final @NonNull String name,
final @NonNull Triplet<@NonNull String, @NonNull String, @NonNull String> names,
final @NonNull Triplet<@NonNull Class<U>, @NonNull Class<V>, @NonNull Class<W>> types
) {
final ParserRegistry<C> parserRegistry = manager.getParserRegistry();
final ArgumentParser<C, U> firstParser = parserRegistry.createParser(TypeToken.get(types.getFirst()),
ParserParameters.empty()).orElseThrow(() ->
new IllegalArgumentException(
"Could not create parser for primary type"));
final ArgumentParser<C, V> secondaryParser = parserRegistry.createParser(TypeToken.get(types.getSecond()),
ParserParameters.empty()).orElseThrow(() ->
new IllegalArgumentException(
"Could not create parser for secondary type"));
final ArgumentParser<C, W> tertiaryParser = parserRegistry.createParser(TypeToken.get(types.getThird()),
ParserParameters.empty()).orElseThrow(() ->
new IllegalArgumentException(
"Could not create parser for tertiary type"));
final ArgumentParser<C, U> firstParser = parserRegistry.createParser(
TypeToken.get(types.getFirst()),
ParserParameters.empty()
).orElseThrow(() ->
new IllegalArgumentException(
"Could not create parser for primary type"));
final ArgumentParser<C, V> secondaryParser = parserRegistry.createParser(
TypeToken.get(types.getSecond()),
ParserParameters.empty()
).orElseThrow(() ->
new IllegalArgumentException(
"Could not create parser for secondary type"));
final ArgumentParser<C, W> tertiaryParser = parserRegistry.createParser(
TypeToken.get(types.getThird()),
ParserParameters.empty()
).orElseThrow(() ->
new IllegalArgumentException(
"Could not create parser for tertiary type"));
return new ArgumentTripletIntermediaryBuilder<>(true, name, names,
Triplet.of(firstParser, secondaryParser, tertiaryParser), types);
Triplet.of(firstParser, secondaryParser, tertiaryParser), types
);
}
@SuppressWarnings("ALL")
@ -113,15 +124,17 @@ public class ArgumentTriplet<C, U, V, W, O> extends CompoundArgument<Triplet<U,
private final Triplet<String, String, String> names;
private final Triplet<Class<U>, Class<V>, Class<W>> types;
private ArgumentTripletIntermediaryBuilder(final boolean required,
final @NonNull String name,
final @NonNull Triplet<@NonNull String, @NonNull String,
@NonNull String> names,
final @NonNull Triplet<@NonNull ArgumentParser<C, U>,
@NonNull ArgumentParser<C, V>,
@NonNull ArgumentParser<C, W>> parserTriplet,
final @NonNull Triplet<@NonNull Class<U>,
@NonNull Class<V>, @NonNull Class<W>> types) {
private ArgumentTripletIntermediaryBuilder(
final boolean required,
final @NonNull String name,
final @NonNull Triplet<@NonNull String, @NonNull String,
@NonNull String> names,
final @NonNull Triplet<@NonNull ArgumentParser<C, U>,
@NonNull ArgumentParser<C, V>,
@NonNull ArgumentParser<C, W>> parserTriplet,
final @NonNull Triplet<@NonNull Class<U>,
@NonNull Class<V>, @NonNull Class<W>> types
) {
this.required = required;
this.name = name;
this.names = names;
@ -136,14 +149,16 @@ public class ArgumentTriplet<C, U, V, W, O> extends CompoundArgument<Triplet<U,
*/
public @NonNull ArgumentTriplet<@NonNull C, @NonNull U, @NonNull V,
@NonNull W, Triplet<U, V, W>> simple() {
return new ArgumentTriplet<>(this.required,
this.name,
this.names,
this.types,
this.parserTriplet,
(sender, triplet) -> triplet,
new TypeToken<Triplet<U, V, W>>() {
});
return new ArgumentTriplet<>(
this.required,
this.name,
this.names,
this.types,
this.parserTriplet,
(sender, triplet) -> triplet,
new TypeToken<Triplet<U, V, W>>() {
}
);
}
/**
@ -155,9 +170,11 @@ public class ArgumentTriplet<C, U, V, W, O> extends CompoundArgument<Triplet<U,
* @return Created triplet
*/
public <O> @NonNull ArgumentTriplet<@NonNull C, @NonNull U, @NonNull V,
@NonNull W, @NonNull O> withMapper(final @NonNull TypeToken<O> clazz,
final @NonNull BiFunction<@NonNull C, @NonNull Triplet<@NonNull U,
@NonNull V, @NonNull W>, @NonNull O> mapper) {
@NonNull W, @NonNull O> withMapper(
final @NonNull TypeToken<O> clazz,
final @NonNull BiFunction<@NonNull C, @NonNull Triplet<@NonNull U,
@NonNull V, @NonNull W>, @NonNull O> mapper
) {
return new ArgumentTriplet<>(this.required, this.name, this.names, this.types, this.parserTriplet, mapper, clazz);
}
@ -169,12 +186,15 @@ public class ArgumentTriplet<C, U, V, W, O> extends CompoundArgument<Triplet<U,
* @param <O> Output type
* @return Created triplet
*/
public <O> @NonNull ArgumentTriplet<C, U, V, W, O> withMapper(final @NonNull Class<O> clazz,
final @NonNull BiFunction<@NonNull C, @NonNull Triplet<
@NonNull U, @NonNull V, @NonNull W>,
@NonNull O> mapper) {
public <O> @NonNull ArgumentTriplet<C, U, V, W, O> withMapper(
final @NonNull Class<O> clazz,
final @NonNull BiFunction<@NonNull C, @NonNull Triplet<
@NonNull U, @NonNull V, @NonNull W>,
@NonNull O> mapper
) {
return new ArgumentTriplet<>(this.required, this.name, this.names, this.types,
this.parserTriplet, mapper, TypeToken.get(clazz));
this.parserTriplet, mapper, TypeToken.get(clazz)
);
}
}

View file

@ -61,20 +61,24 @@ public class CompoundArgument<T extends Tuple, C, O> extends CommandArgument<C,
* @param tupleFactory Function to use when creating tuple
* @param valueType The output type
*/
public CompoundArgument(final boolean required,
final @NonNull String name,
final @NonNull Tuple names,
final @NonNull Tuple parserTuple,
final @NonNull Tuple types,
final @NonNull BiFunction<@NonNull C, @NonNull T, @NonNull O> mapper,
final @NonNull Function<@NonNull Object[], @NonNull T> tupleFactory,
final @NonNull TypeToken<O> valueType) {
super(required,
name,
new CompoundParser<>(parserTuple, mapper, tupleFactory),
"",
valueType,
null);
public CompoundArgument(
final boolean required,
final @NonNull String name,
final @NonNull Tuple names,
final @NonNull Tuple parserTuple,
final @NonNull Tuple types,
final @NonNull BiFunction<@NonNull C, @NonNull T, @NonNull O> mapper,
final @NonNull Function<@NonNull Object[], @NonNull T> tupleFactory,
final @NonNull TypeToken<O> valueType
) {
super(
required,
name,
new CompoundParser<>(parserTuple, mapper, tupleFactory),
"",
valueType,
null
);
this.parserTuple = parserTuple;
this.names = names;
this.types = types;
@ -114,17 +118,21 @@ public class CompoundArgument<T extends Tuple, C, O> extends CommandArgument<C,
private final BiFunction<C, T, O> mapper;
private final Function<Object[], T> tupleFactory;
private CompoundParser(final @NonNull Tuple parserTuple,
final @NonNull BiFunction<@NonNull C, @NonNull T, @NonNull O> mapper,
final @NonNull Function<@NonNull Object[], @NonNull T> tupleFactory) {
private CompoundParser(
final @NonNull Tuple parserTuple,
final @NonNull BiFunction<@NonNull C, @NonNull T, @NonNull O> mapper,
final @NonNull Function<@NonNull Object[], @NonNull T> tupleFactory
) {
this.parsers = parserTuple.toArray();
this.mapper = mapper;
this.tupleFactory = tupleFactory;
}
@Override
public @NonNull ArgumentParseResult<O> parse(final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue) {
public @NonNull ArgumentParseResult<O> parse(
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue
) {
final Object[] output = new Object[this.parsers.length];
for (int i = 0; i < this.parsers.length; i++) {
@SuppressWarnings("unchecked") final ArgumentParser<C, ?> parser = (ArgumentParser<C, ?>) this.parsers[i];
@ -141,16 +149,20 @@ public class CompoundArgument<T extends Tuple, C, O> extends CommandArgument<C,
* Now check if the mapper threw any exceptions
*/
try {
return ArgumentParseResult.success(this.mapper.apply(commandContext.getSender(),
this.tupleFactory.apply(output)));
return ArgumentParseResult.success(this.mapper.apply(
commandContext.getSender(),
this.tupleFactory.apply(output)
));
} catch (final Exception e) {
return ArgumentParseResult.failure(e);
}
}
@Override
public @NonNull List<@NonNull String> suggestions(final @NonNull CommandContext<C> commandContext,
final @NonNull String input) {
public @NonNull List<@NonNull String> suggestions(
final @NonNull CommandContext<C> commandContext,
final @NonNull String input
) {
/*
This method will be called n times, each time for each of the internal types.
The problem is that we need to then know which of the parsers to forward the
@ -163,6 +175,7 @@ public class CompoundArgument<T extends Tuple, C, O> extends CommandArgument<C,
//noinspection all
return ((ArgumentParser<C, ?>) this.parsers[argument]).suggestions(commandContext, input);
}
}
}

View file

@ -66,10 +66,12 @@ public final class FlagArgument<C> extends CommandArgument<C, Object> {
* @param flags Flags
*/
public FlagArgument(final Collection<CommandFlag<?>> flags) {
super(false,
FLAG_ARGUMENT_NAME,
new FlagArgumentParser<>(flags.toArray(new CommandFlag<?>[0])),
Object.class);
super(
false,
FLAG_ARGUMENT_NAME,
new FlagArgumentParser<>(flags.toArray(new CommandFlag<?>[0])),
Object.class
);
this.flags = flags;
}
@ -92,8 +94,10 @@ public final class FlagArgument<C> extends CommandArgument<C, Object> {
}
@Override
public @NonNull ArgumentParseResult<@NonNull Object> parse(final @NonNull CommandContext<@NonNull C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue) {
public @NonNull ArgumentParseResult<@NonNull Object> parse(
final @NonNull CommandContext<@NonNull C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue
) {
/*
This argument must necessarily be the last so we can just consume all remaining input. This argument type
is similar to a greedy string in that sense. But, we need to keep all flag logic contained to the parser
@ -144,13 +148,15 @@ public final class FlagArgument<C> extends CommandArgument<C, Object> {
if (currentFlag == null) {
return ArgumentParseResult.failure(
new IllegalArgumentException(String.format("No flag started. Don't"
+ " know what to do with '%s'", string)));
+ " know what to do with '%s'", string)));
} else {
final ArgumentParseResult<?> result =
((CommandArgument) currentFlag.getCommandArgument())
.getParser()
.parse(commandContext,
new LinkedList<>(Collections.singletonList(string)));
.parse(
commandContext,
new LinkedList<>(Collections.singletonList(string))
);
if (result.getFailure().isPresent()) {
return ArgumentParseResult.failure(result.getFailure().get());
} else {
@ -167,8 +173,10 @@ public final class FlagArgument<C> extends CommandArgument<C, Object> {
}
@Override
public @NonNull List<@NonNull String> suggestions(final @NonNull CommandContext<C> commandContext,
final @NonNull String input) {
public @NonNull List<@NonNull String> suggestions(
final @NonNull CommandContext<C> commandContext,
final @NonNull String input
) {
/* Check if we have a last flag stored */
final String lastArg = commandContext.getOrDefault(FLAG_META, "");
if (lastArg.isEmpty() || !lastArg.startsWith("-")) {

View file

@ -48,10 +48,12 @@ public final class CommandFlag<T> {
private final @Nullable CommandArgument<?, T> commandArgument;
private CommandFlag(final @NonNull String name,
final @NonNull String @NonNull [] aliases,
final @NonNull Description description,
final @Nullable CommandArgument<?, T> commandArgument) {
private CommandFlag(
final @NonNull String name,
final @NonNull String @NonNull [] aliases,
final @NonNull Description description,
final @Nullable CommandArgument<?, T> commandArgument
) {
this.name = name;
this.aliases = aliases;
this.description = description;
@ -135,10 +137,12 @@ public final class CommandFlag<T> {
private final Description description;
private final CommandArgument<?, T> commandArgument;
private Builder(final @NonNull String name,
final @NonNull String[] aliases,
final @NonNull Description description,
final @Nullable CommandArgument<?, T> commandArgument) {
private Builder(
final @NonNull String name,
final @NonNull String[] aliases,
final @NonNull Description description,
final @Nullable CommandArgument<?, T> commandArgument
) {
this.name = name;
this.aliases = aliases;
this.description = description;

View file

@ -70,8 +70,10 @@ public final class FlagContext {
* @param value Flag value
* @param <T> Value type
*/
public <T> void addValueFlag(final @NonNull CommandFlag<T> flag,
final @NonNull T value) {
public <T> void addValueFlag(
final @NonNull CommandFlag<T> flag,
final @NonNull T value
) {
this.flagValues.put(flag.getName(), value);
}
@ -81,7 +83,7 @@ public final class FlagContext {
*
* @param flag Flag name
* @return {@code true} if the flag is presence and the flag is a presence flag,
* else {@code false}
* else {@code false}
*/
public boolean isPresent(final @NonNull String flag) {
final Object value = this.flagValues.get(flag);
@ -96,8 +98,10 @@ public final class FlagContext {
* @param <T> Value type
* @return Stored value, or the supplied default value
*/
public <T> @Nullable T getValue(final @NonNull String name,
final @Nullable T defaultValue) {
public <T> @Nullable T getValue(
final @NonNull String name,
final @Nullable T defaultValue
) {
final Object value = this.flagValues.get(name);
if (value == null) {
return defaultValue;

View file

@ -118,6 +118,7 @@ public abstract class ArgumentParseResult<T> {
public @NonNull Optional<Throwable> getFailure() {
return Optional.of(this.failure);
}
}
}

View file

@ -46,8 +46,10 @@ public interface ArgumentParser<C, T> {
* @param inputQueue The queue of arguments
* @return Parsed command result
*/
@NonNull ArgumentParseResult<@NonNull T> parse(@NonNull CommandContext<@NonNull C> commandContext,
@NonNull Queue<@NonNull String> inputQueue);
@NonNull ArgumentParseResult<@NonNull T> parse(
@NonNull CommandContext<@NonNull C> commandContext,
@NonNull Queue<@NonNull String> inputQueue
);
/**
* Get a list of suggested arguments that would be correctly parsed by this parser
@ -56,8 +58,10 @@ public interface ArgumentParser<C, T> {
* @param input Input string
* @return List of suggestions
*/
default @NonNull List<@NonNull String> suggestions(final @NonNull CommandContext<C> commandContext,
final @NonNull String input) {
default @NonNull List<@NonNull String> suggestions(
final @NonNull CommandContext<C> commandContext,
final @NonNull String input
) {
return Collections.emptyList();
}

View file

@ -44,8 +44,10 @@ public class ParserParameter<T> {
* @param key Parameter key
* @param expectedType Type that is expected to be mapped to this parameter
*/
public ParserParameter(final @NonNull String key,
final @NonNull TypeToken<T> expectedType) {
public ParserParameter(
final @NonNull String key,
final @NonNull TypeToken<T> expectedType
) {
this.key = key;
this.expectedType = expectedType;
}

View file

@ -53,8 +53,10 @@ public final class ParserParameters {
* @param <T> Value type
* @return Constructed instance
*/
public static <T> @NonNull ParserParameters single(final @NonNull ParserParameter<T> parameter,
final @NonNull T value) {
public static <T> @NonNull ParserParameters single(
final @NonNull ParserParameter<T> parameter,
final @NonNull T value
) {
final ParserParameters parameters = new ParserParameters();
parameters.store(parameter, value);
return parameters;
@ -78,8 +80,10 @@ public final class ParserParameters {
* @param value Object
* @param <T> Parameter type
*/
public <T> void store(final @NonNull ParserParameter<T> parameter,
final @NonNull T value) {
public <T> void store(
final @NonNull ParserParameter<T> parameter,
final @NonNull T value
) {
this.internalMap.put(parameter, value);
}
@ -92,8 +96,10 @@ public final class ParserParameters {
* @return Parameter value
*/
@SuppressWarnings("unchecked")
public <T> @NonNull T get(final @NonNull ParserParameter<T> parameter,
final @NonNull T defaultValue) {
public <T> @NonNull T get(
final @NonNull ParserParameter<T> parameter,
final @NonNull T defaultValue
) {
return (T) this.internalMap.getOrDefault(parameter, defaultValue);
}

View file

@ -49,8 +49,10 @@ public interface ParserRegistry<C> {
* to configure the parser, many of which are documented in {@link StandardParameters}
* @param <T> Generic type specifying what is produced by the parser
*/
<T> void registerParserSupplier(@NonNull TypeToken<T> type,
@NonNull Function<@NonNull ParserParameters, @NonNull ArgumentParser<C, ?>> supplier);
<T> void registerParserSupplier(
@NonNull TypeToken<T> type,
@NonNull Function<@NonNull ParserParameters, @NonNull ArgumentParser<C, ?>> supplier
);
/**
* Register a named parser supplier
@ -59,8 +61,10 @@ public interface ParserRegistry<C> {
* @param supplier The function that generates the parser. The map supplied my contain parameters used
* to configure the parser, many of which are documented in {@link StandardParameters}
*/
void registerNamedParserSupplier(@NonNull String name,
@NonNull Function<@NonNull ParserParameters, @NonNull ArgumentParser<C, ?>> supplier);
void registerNamedParserSupplier(
@NonNull String name,
@NonNull Function<@NonNull ParserParameters, @NonNull ArgumentParser<C, ?>> supplier
);
/**
* Register a mapper that maps annotation instances to a map of parameter-object pairs
@ -71,9 +75,11 @@ public interface ParserRegistry<C> {
* @param <A> Annotation type
* @param <T> Type of the object that the parser is retrieved for
*/
<A extends Annotation, T> void registerAnnotationMapper(@NonNull Class<A> annotation,
@NonNull BiFunction<@NonNull A, @NonNull TypeToken<?>,
@NonNull ParserParameters> mapper);
<A extends Annotation, T> void registerAnnotationMapper(
@NonNull Class<A> annotation,
@NonNull BiFunction<@NonNull A, @NonNull TypeToken<?>,
@NonNull ParserParameters> mapper
);
/**
* Parse annotations into {@link ParserParameters}
@ -82,8 +88,10 @@ public interface ParserRegistry<C> {
* @param annotations The annotations to be parsed
* @return Parsed parameters
*/
@NonNull ParserParameters parseAnnotations(@NonNull TypeToken<?> parsingType,
@NonNull Collection<? extends Annotation> annotations);
@NonNull ParserParameters parseAnnotations(
@NonNull TypeToken<?> parsingType,
@NonNull Collection<? extends Annotation> annotations
);
/**
* Attempt to create a {@link ArgumentParser} for a specified type, using
@ -94,8 +102,10 @@ public interface ParserRegistry<C> {
* @param <T> Generic type
* @return Parser, if one can be created
*/
<T> @NonNull Optional<ArgumentParser<C, T>> createParser(@NonNull TypeToken<T> type,
@NonNull ParserParameters parserParameters);
<T> @NonNull Optional<ArgumentParser<C, T>> createParser(
@NonNull TypeToken<T> type,
@NonNull ParserParameters parserParameters
);
/**
* Attempt to create a {@link ArgumentParser} for a specified type, using
@ -106,7 +116,9 @@ public interface ParserRegistry<C> {
* @param <T> Generic type
* @return Parser, if one can be created
*/
<T> @NonNull Optional<ArgumentParser<C, T>> createParser(@NonNull String name,
@NonNull ParserParameters parserParameters);
<T> @NonNull Optional<ArgumentParser<C, T>> createParser(
@NonNull String name,
@NonNull ParserParameters parserParameters
);
}

View file

@ -63,8 +63,10 @@ public final class StandardParameters {
private StandardParameters() {
}
private static <T> @NonNull ParserParameter<T> create(final @NonNull String key,
final @NonNull TypeToken<T> expectedType) {
private static <T> @NonNull ParserParameter<T> create(
final @NonNull String key,
final @NonNull TypeToken<T> expectedType
) {
return new ParserParameter<>(key, expectedType);
}

View file

@ -23,9 +23,8 @@
//
package cloud.commandframework.arguments.parser;
import cloud.commandframework.annotations.specifier.Greedy;
import cloud.commandframework.arguments.standard.UUIDArgument;
import cloud.commandframework.annotations.specifier.Completions;
import cloud.commandframework.annotations.specifier.Greedy;
import cloud.commandframework.annotations.specifier.Range;
import cloud.commandframework.arguments.standard.BooleanArgument;
import cloud.commandframework.arguments.standard.ByteArgument;
@ -36,6 +35,7 @@ import cloud.commandframework.arguments.standard.FloatArgument;
import cloud.commandframework.arguments.standard.IntegerArgument;
import cloud.commandframework.arguments.standard.ShortArgument;
import cloud.commandframework.arguments.standard.StringArgument;
import cloud.commandframework.arguments.standard.UUIDArgument;
import io.leangen.geantyref.GenericTypeReflector;
import io.leangen.geantyref.TypeToken;
import org.checkerframework.checker.nullness.qual.NonNull;
@ -86,60 +86,83 @@ public final class StandardParserRegistry<C> implements ParserRegistry<C> {
/* Register standard types */
this.registerParserSupplier(TypeToken.get(Byte.class), options ->
new ByteArgument.ByteParser<C>((byte) options.get(StandardParameters.RANGE_MIN, Byte.MIN_VALUE),
(byte) options.get(StandardParameters.RANGE_MAX, Byte.MAX_VALUE)));
new ByteArgument.ByteParser<C>(
(byte) options.get(StandardParameters.RANGE_MIN, Byte.MIN_VALUE),
(byte) options.get(StandardParameters.RANGE_MAX, Byte.MAX_VALUE)
));
this.registerParserSupplier(TypeToken.get(Short.class), options ->
new ShortArgument.ShortParser<C>((short) options.get(StandardParameters.RANGE_MIN, Short.MIN_VALUE),
(short) options.get(StandardParameters.RANGE_MAX, Short.MAX_VALUE)));
new ShortArgument.ShortParser<C>(
(short) options.get(StandardParameters.RANGE_MIN, Short.MIN_VALUE),
(short) options.get(StandardParameters.RANGE_MAX, Short.MAX_VALUE)
));
this.registerParserSupplier(TypeToken.get(Integer.class), options ->
new IntegerArgument.IntegerParser<C>((int) options.get(StandardParameters.RANGE_MIN, Integer.MIN_VALUE),
(int) options.get(StandardParameters.RANGE_MAX, Integer.MAX_VALUE)));
new IntegerArgument.IntegerParser<C>(
(int) options.get(StandardParameters.RANGE_MIN, Integer.MIN_VALUE),
(int) options.get(StandardParameters.RANGE_MAX, Integer.MAX_VALUE)
));
this.registerParserSupplier(TypeToken.get(Float.class), options ->
new FloatArgument.FloatParser<C>((float) options.get(StandardParameters.RANGE_MIN, Float.MIN_VALUE),
(float) options.get(StandardParameters.RANGE_MAX, Float.MAX_VALUE)));
new FloatArgument.FloatParser<C>(
(float) options.get(StandardParameters.RANGE_MIN, Float.MIN_VALUE),
(float) options.get(StandardParameters.RANGE_MAX, Float.MAX_VALUE)
));
this.registerParserSupplier(TypeToken.get(Double.class), options ->
new DoubleArgument.DoubleParser<C>((double) options.get(StandardParameters.RANGE_MIN, Double.MIN_VALUE),
(double) options.get(StandardParameters.RANGE_MAX, Double.MAX_VALUE)));
new DoubleArgument.DoubleParser<C>(
(double) options.get(StandardParameters.RANGE_MIN, Double.MIN_VALUE),
(double) options.get(StandardParameters.RANGE_MAX, Double.MAX_VALUE)
));
this.registerParserSupplier(TypeToken.get(Character.class), options -> new CharArgument.CharacterParser<C>());
/* Make this one less awful */
this.registerParserSupplier(TypeToken.get(String.class), options -> {
final boolean greedy = options.get(StandardParameters.GREEDY, false);
final StringArgument.StringMode stringMode = greedy
? StringArgument.StringMode.GREEDY
: StringArgument.StringMode.SINGLE;
? StringArgument.StringMode.GREEDY
: StringArgument.StringMode.SINGLE;
return new StringArgument.StringParser<C>(
stringMode,
(context, s) -> Arrays.asList(options.get(StandardParameters.COMPLETIONS, new String[0])));
(context, s) -> Arrays.asList(options.get(StandardParameters.COMPLETIONS, new String[0]))
);
});
/* Add options to this */
this.registerParserSupplier(TypeToken.get(Boolean.class), options -> new BooleanArgument.BooleanParser<>(false));
this.registerParserSupplier(TypeToken.get(UUID.class), options -> new UUIDArgument.UUIDParser<>());
}
private static boolean isPrimitive(final @NonNull TypeToken<?> type) {
return GenericTypeReflector.erase(type.getType()).isPrimitive();
}
@Override
public <T> void registerParserSupplier(final @NonNull TypeToken<T> type,
final @NonNull Function<@NonNull ParserParameters,
@NonNull ArgumentParser<C, ?>> supplier) {
public <T> void registerParserSupplier(
final @NonNull TypeToken<T> type,
final @NonNull Function<@NonNull ParserParameters,
@NonNull ArgumentParser<C, ?>> supplier
) {
this.parserSuppliers.put(type, supplier);
}
@Override
public void registerNamedParserSupplier(final @NonNull String name,
final @NonNull Function<@NonNull ParserParameters,
@NonNull ArgumentParser<C, ?>> supplier) {
public void registerNamedParserSupplier(
final @NonNull String name,
final @NonNull Function<@NonNull ParserParameters,
@NonNull ArgumentParser<C, ?>> supplier
) {
this.namedParsers.put(name, supplier);
}
@Override
public <A extends Annotation, T> void registerAnnotationMapper(final @NonNull Class<A> annotation,
final @NonNull BiFunction<@NonNull A, @NonNull TypeToken<?>,
@NonNull ParserParameters> mapper) {
public <A extends Annotation, T> void registerAnnotationMapper(
final @NonNull Class<A> annotation,
final @NonNull BiFunction<@NonNull A, @NonNull TypeToken<?>,
@NonNull ParserParameters> mapper
) {
this.annotationMappers.put(annotation, mapper);
}
@Override
public @NonNull ParserParameters parseAnnotations(final @NonNull TypeToken<?> parsingType,
final @NonNull Collection<@NonNull ? extends Annotation> annotations) {
public @NonNull ParserParameters parseAnnotations(
final @NonNull TypeToken<?> parsingType,
final @NonNull Collection<@NonNull ? extends Annotation> annotations
) {
final ParserParameters parserParameters = new ParserParameters();
annotations.forEach(annotation -> {
// noinspection all
@ -155,8 +178,10 @@ public final class StandardParserRegistry<C> implements ParserRegistry<C> {
}
@Override
public <T> @NonNull Optional<ArgumentParser<C, T>> createParser(final @NonNull TypeToken<T> type,
final @NonNull ParserParameters parserParameters) {
public <T> @NonNull Optional<ArgumentParser<C, T>> createParser(
final @NonNull TypeToken<T> type,
final @NonNull ParserParameters parserParameters
) {
final TypeToken<?> actualType;
if (GenericTypeReflector.erase(type.getType()).isPrimitive()) {
actualType = TypeToken.get(PRIMITIVE_MAPPINGS.get(GenericTypeReflector.erase(type.getType())));
@ -180,8 +205,10 @@ public final class StandardParserRegistry<C> implements ParserRegistry<C> {
}
@Override
public <T> @NonNull Optional<ArgumentParser<C, T>> createParser(final @NonNull String name,
final @NonNull ParserParameters parserParameters) {
public <T> @NonNull Optional<ArgumentParser<C, T>> createParser(
final @NonNull String name,
final @NonNull ParserParameters parserParameters
) {
final Function<ParserParameters, ArgumentParser<C, ?>> producer = this.namedParsers.get(name);
if (producer == null) {
return Optional.empty();
@ -191,12 +218,6 @@ public final class StandardParserRegistry<C> implements ParserRegistry<C> {
return Optional.of(parser);
}
private static boolean isPrimitive(final @NonNull TypeToken<?> type) {
return GenericTypeReflector.erase(type.getType()).isPrimitive();
}
private static final class RangeMapper<T> implements BiFunction<@NonNull Range, @NonNull TypeToken<?>,
@NonNull ParserParameters> {

View file

@ -37,14 +37,17 @@ import java.util.function.BiFunction;
@SuppressWarnings("unused")
public final class BooleanArgument<C> extends CommandArgument<C, Boolean> {
private final boolean liberal;
private BooleanArgument(final boolean required,
final @NonNull String name,
final boolean liberal,
final @NonNull String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider) {
private BooleanArgument(
final boolean required,
final @NonNull String name,
final boolean liberal,
final @NonNull String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider
) {
super(required, name, new BooleanParser<>(liberal), defaultValue, Boolean.class, suggestionsProvider);
this.liberal = liberal;
}
@ -90,8 +93,10 @@ public final class BooleanArgument<C> extends CommandArgument<C, Boolean> {
* @param <C> Command sender type
* @return Created argument
*/
public static <C> @NonNull CommandArgument<C, Boolean> optional(final @NonNull String name,
final boolean defaultBoolean) {
public static <C> @NonNull CommandArgument<C, Boolean> optional(
final @NonNull String name,
final boolean defaultBoolean
) {
return BooleanArgument.<C>newBuilder(name).asOptionalWithDefault(Boolean.toString(defaultBoolean)).build();
}
@ -131,7 +136,8 @@ public final class BooleanArgument<C> extends CommandArgument<C, Boolean> {
@Override
public @NonNull BooleanArgument<C> build() {
return new BooleanArgument<>(this.isRequired(), this.getName(), this.liberal,
this.getDefaultValue(), this.getSuggestionsProvider());
this.getDefaultValue(), this.getSuggestionsProvider()
);
}
}
@ -154,8 +160,10 @@ public final class BooleanArgument<C> extends CommandArgument<C, Boolean> {
}
@Override
public @NonNull ArgumentParseResult<Boolean> parse(final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue) {
public @NonNull ArgumentParseResult<Boolean> parse(
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue
) {
final String input = inputQueue.peek();
if (input == null) {
return ArgumentParseResult.failure(new NullPointerException("No input was provided"));
@ -188,8 +196,10 @@ public final class BooleanArgument<C> extends CommandArgument<C, Boolean> {
}
@Override
public @NonNull List<@NonNull String> suggestions(final @NonNull CommandContext<C> commandContext,
final @NonNull String input) {
public @NonNull List<@NonNull String> suggestions(
final @NonNull CommandContext<C> commandContext,
final @NonNull String input
) {
if (!liberal) {
return Arrays.asList("TRUE", "FALSE");
}
@ -201,6 +211,7 @@ public final class BooleanArgument<C> extends CommandArgument<C, Boolean> {
public boolean isContextFree() {
return true;
}
}
@ -218,8 +229,10 @@ public final class BooleanArgument<C> extends CommandArgument<C, Boolean> {
* @param input String input
* @param liberal Liberal value
*/
public BooleanParseException(final @NonNull String input,
final boolean liberal) {
public BooleanParseException(
final @NonNull String input,
final boolean liberal
) {
this.input = input;
this.liberal = liberal;
}
@ -246,5 +259,7 @@ public final class BooleanArgument<C> extends CommandArgument<C, Boolean> {
public String getMessage() {
return String.format("Could not parse boolean from '%s'.", input);
}
}
}

View file

@ -41,13 +41,15 @@ public final class ByteArgument<C> extends CommandArgument<C, Byte> {
private final byte min;
private final byte max;
private ByteArgument(final boolean required,
final @NonNull String name,
final byte min,
final byte max,
final @NonNull String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider) {
private ByteArgument(
final boolean required,
final @NonNull String name,
final byte min,
final byte max,
final @NonNull String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider
) {
super(required, name, new ByteParser<>(min, max), defaultValue, Byte.class, suggestionsProvider);
this.min = min;
this.max = max;
@ -94,8 +96,10 @@ public final class ByteArgument<C> extends CommandArgument<C, Byte> {
* @param <C> Command sender type
* @return Created argument
*/
public static <C> @NonNull CommandArgument<C, Byte> optional(final @NonNull String name,
final byte defaultNum) {
public static <C> @NonNull CommandArgument<C, Byte> optional(
final @NonNull String name,
final byte defaultNum
) {
return ByteArgument.<C>newBuilder(name).asOptionalWithDefault(Byte.toString(defaultNum)).build();
}
@ -156,7 +160,8 @@ public final class ByteArgument<C> extends CommandArgument<C, Byte> {
@Override
public @NonNull ByteArgument<C> build() {
return new ByteArgument<>(this.isRequired(), this.getName(), this.min, this.max,
this.getDefaultValue(), this.getSuggestionsProvider());
this.getDefaultValue(), this.getSuggestionsProvider()
);
}
}
@ -180,7 +185,8 @@ public final class ByteArgument<C> extends CommandArgument<C, Byte> {
@Override
public @NonNull ArgumentParseResult<Byte> parse(
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue) {
final @NonNull Queue<@NonNull String> inputQueue
) {
final String input = inputQueue.peek();
if (input == null) {
return ArgumentParseResult.failure(new NullPointerException("No input was provided"));
@ -189,16 +195,19 @@ public final class ByteArgument<C> extends CommandArgument<C, Byte> {
final byte value = Byte.parseByte(input);
if (value < this.min || value > this.max) {
return ArgumentParseResult.failure(
new ByteParseException(input,
this.min,
this.max));
new ByteParseException(
input,
this.min,
this.max
));
}
inputQueue.remove();
return ArgumentParseResult.success(value);
} catch (final Exception e) {
return ArgumentParseResult.failure(
new ByteParseException(input, this.min,
this.max));
this.max
));
}
}
@ -208,8 +217,10 @@ public final class ByteArgument<C> extends CommandArgument<C, Byte> {
}
@Override
public @NonNull List<@NonNull String> suggestions(final @NonNull CommandContext<C> commandContext,
final @NonNull String input) {
public @NonNull List<@NonNull String> suggestions(
final @NonNull CommandContext<C> commandContext,
final @NonNull String input
) {
return IntegerArgument.IntegerParser.getSuggestions(this.min, this.max, input);
}

View file

@ -37,11 +37,13 @@ import java.util.function.BiFunction;
@SuppressWarnings("unused")
public final class CharArgument<C> extends CommandArgument<C, Character> {
private CharArgument(final boolean required,
final @NonNull String name,
final @NonNull String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>,
@NonNull String, @NonNull List<@NonNull String>> suggestionsProvider) {
private CharArgument(
final boolean required,
final @NonNull String name,
final @NonNull String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>,
@NonNull String, @NonNull List<@NonNull String>> suggestionsProvider
) {
super(required, name, new CharacterParser<>(), defaultValue, Character.class, suggestionsProvider);
}
@ -86,8 +88,10 @@ public final class CharArgument<C> extends CommandArgument<C, Character> {
* @param <C> Command sender type
* @return Created argument
*/
public static <C> @NonNull CommandArgument<C, Character> optional(final @NonNull String name,
final @NonNull String defaultNum) {
public static <C> @NonNull CommandArgument<C, Character> optional(
final @NonNull String name,
final @NonNull String defaultNum
) {
return CharArgument.<C>newBuilder(name).asOptionalWithDefault(defaultNum).build();
}
@ -106,7 +110,8 @@ public final class CharArgument<C> extends CommandArgument<C, Character> {
@Override
public @NonNull CharArgument<C> build() {
return new CharArgument<>(this.isRequired(), this.getName(),
this.getDefaultValue(), this.getSuggestionsProvider());
this.getDefaultValue(), this.getSuggestionsProvider()
);
}
}
@ -115,8 +120,10 @@ public final class CharArgument<C> extends CommandArgument<C, Character> {
public static final class CharacterParser<C> implements ArgumentParser<C, Character> {
@Override
public @NonNull ArgumentParseResult<Character> parse(final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue) {
public @NonNull ArgumentParseResult<Character> parse(
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue
) {
final String input = inputQueue.peek();
if (input == null) {
return ArgumentParseResult.failure(new NullPointerException("No input was provided"));
@ -166,5 +173,7 @@ public final class CharArgument<C> extends CommandArgument<C, Character> {
public String getMessage() {
return String.format("'%s' is not a valid character.", input);
}
}
}

View file

@ -41,13 +41,15 @@ public final class DoubleArgument<C> extends CommandArgument<C, Double> {
private final double min;
private final double max;
private DoubleArgument(final boolean required,
final @NonNull String name,
final double min,
final double max,
final String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider) {
private DoubleArgument(
final boolean required,
final @NonNull String name,
final double min,
final double max,
final String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider
) {
super(required, name, new DoubleParser<>(min, max), defaultValue, Double.class, suggestionsProvider);
this.min = min;
this.max = max;
@ -94,8 +96,10 @@ public final class DoubleArgument<C> extends CommandArgument<C, Double> {
* @param <C> Command sender type
* @return Created argument
*/
public static <C> @NonNull CommandArgument<C, Double> optional(final @NonNull String name,
final double defaultNum) {
public static <C> @NonNull CommandArgument<C, Double> optional(
final @NonNull String name,
final double defaultNum
) {
return DoubleArgument.<C>newBuilder(name).asOptionalWithDefault(Double.toString(defaultNum)).build();
}
@ -156,7 +160,8 @@ public final class DoubleArgument<C> extends CommandArgument<C, Double> {
@Override
public @NonNull DoubleArgument<C> build() {
return new DoubleArgument<>(this.isRequired(), this.getName(), this.min, this.max,
this.getDefaultValue(), this.getSuggestionsProvider());
this.getDefaultValue(), this.getSuggestionsProvider()
);
}
}
@ -180,7 +185,8 @@ public final class DoubleArgument<C> extends CommandArgument<C, Double> {
@Override
public @NonNull ArgumentParseResult<Double> parse(
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue) {
final @NonNull Queue<@NonNull String> inputQueue
) {
final String input = inputQueue.peek();
if (input == null) {
return ArgumentParseResult.failure(new NullPointerException("No input was provided"));

View file

@ -45,12 +45,14 @@ import java.util.stream.Collectors;
@SuppressWarnings("unused")
public class EnumArgument<C, E extends Enum<E>> extends CommandArgument<C, E> {
protected EnumArgument(final @NonNull Class<E> enumClass,
final boolean required,
final @NonNull String name,
final @NonNull String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider) {
protected EnumArgument(
final @NonNull Class<E> enumClass,
final boolean required,
final @NonNull String name,
final @NonNull String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider
) {
super(required, name, new EnumParser<>(enumClass), defaultValue, enumClass, suggestionsProvider);
}
@ -64,8 +66,9 @@ public class EnumArgument<C, E extends Enum<E>> extends CommandArgument<C, E> {
* @return Created builder
*/
public static <C, E extends Enum<E>> EnumArgument.@NonNull Builder<C, E> newBuilder(
@NonNull final Class<E> enumClass,
final @NonNull String name) {
@NonNull final Class<E> enumClass,
final @NonNull String name
) {
return new EnumArgument.Builder<>(name, enumClass);
}
@ -80,7 +83,8 @@ public class EnumArgument<C, E extends Enum<E>> extends CommandArgument<C, E> {
*/
public static <C, E extends Enum<E>> @NonNull CommandArgument<C, E> of(
final @NonNull Class<E> enumClass,
final @NonNull String name) {
final @NonNull String name
) {
return EnumArgument.<C, E>newBuilder(enumClass, name).asRequired().build();
}
@ -95,7 +99,8 @@ public class EnumArgument<C, E extends Enum<E>> extends CommandArgument<C, E> {
*/
public static <C, E extends Enum<E>> @NonNull CommandArgument<C, E> optional(
final @NonNull Class<E> enumClass,
final @NonNull String name) {
final @NonNull String name
) {
return EnumArgument.<C, E>newBuilder(enumClass, name).asOptional().build();
}
@ -112,7 +117,8 @@ public class EnumArgument<C, E extends Enum<E>> extends CommandArgument<C, E> {
public static <C, E extends Enum<E>> @NonNull CommandArgument<C, E> optional(
final @NonNull Class<E> enumClass,
final @NonNull String name,
final @NonNull E defaultValue) {
final @NonNull E defaultValue
) {
return EnumArgument.<C, E>newBuilder(enumClass, name).asOptionalWithDefault(defaultValue.name().toLowerCase()).build();
}
@ -129,8 +135,10 @@ public class EnumArgument<C, E extends Enum<E>> extends CommandArgument<C, E> {
@Override
public @NonNull CommandArgument<C, E> build() {
return new EnumArgument<>(this.enumClass, this.isRequired(), this.getName(),
this.getDefaultValue(), this.getSuggestionsProvider());
this.getDefaultValue(), this.getSuggestionsProvider()
);
}
}
@ -150,8 +158,10 @@ public class EnumArgument<C, E extends Enum<E>> extends CommandArgument<C, E> {
}
@Override
public @NonNull ArgumentParseResult<E> parse(final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue) {
public @NonNull ArgumentParseResult<E> parse(
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue
) {
final String input = inputQueue.peek();
if (input == null) {
return ArgumentParseResult.failure(new NullPointerException("No input was provided"));
@ -168,8 +178,10 @@ public class EnumArgument<C, E extends Enum<E>> extends CommandArgument<C, E> {
}
@Override
public @NonNull List<@NonNull String> suggestions(final @NonNull CommandContext<C> commandContext,
final @NonNull String input) {
public @NonNull List<@NonNull String> suggestions(
final @NonNull CommandContext<C> commandContext,
final @NonNull String input
) {
return EnumSet.allOf(this.enumClass).stream().map(e -> e.name().toLowerCase()).collect(Collectors.toList());
}
@ -177,6 +189,7 @@ public class EnumArgument<C, E extends Enum<E>> extends CommandArgument<C, E> {
public boolean isContextFree() {
return true;
}
}
@ -191,8 +204,10 @@ public class EnumArgument<C, E extends Enum<E>> extends CommandArgument<C, E> {
* @param input Input
* @param enumClass Enum class
*/
public EnumParseException(final @NonNull String input,
final @NonNull Class<? extends Enum<?>> enumClass) {
public EnumParseException(
final @NonNull String input,
final @NonNull Class<? extends Enum<?>> enumClass
) {
this.input = input;
this.enumClass = enumClass;
}
@ -201,8 +216,8 @@ public class EnumArgument<C, E extends Enum<E>> extends CommandArgument<C, E> {
private static @NonNull String join(final @NonNull Class<? extends Enum> clazz) {
final EnumSet<?> enumSet = EnumSet.allOf(clazz);
return enumSet.stream()
.map(e -> e.toString().toLowerCase())
.collect(Collectors.joining(", "));
.map(e -> e.toString().toLowerCase())
.collect(Collectors.joining(", "));
}
/**

View file

@ -41,13 +41,15 @@ public final class FloatArgument<C> extends CommandArgument<C, Float> {
private final float min;
private final float max;
private FloatArgument(final boolean required,
final @NonNull String name,
final float min,
final float max,
final @NonNull String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>,
@NonNull String, @NonNull List<@NonNull String>> suggestionsProvider) {
private FloatArgument(
final boolean required,
final @NonNull String name,
final float min,
final float max,
final @NonNull String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>,
@NonNull String, @NonNull List<@NonNull String>> suggestionsProvider
) {
super(required, name, new FloatParser<>(min, max), defaultValue, Float.class, suggestionsProvider);
this.min = min;
this.max = max;
@ -94,8 +96,10 @@ public final class FloatArgument<C> extends CommandArgument<C, Float> {
* @param <C> Command sender type
* @return Created argument
*/
public static <C> @NonNull CommandArgument<C, Float> optional(final @NonNull String name,
final float defaultNum) {
public static <C> @NonNull CommandArgument<C, Float> optional(
final @NonNull String name,
final float defaultNum
) {
return FloatArgument.<C>newBuilder(name).asOptionalWithDefault(Float.toString(defaultNum)).build();
}
@ -156,7 +160,8 @@ public final class FloatArgument<C> extends CommandArgument<C, Float> {
@Override
public @NonNull FloatArgument<C> build() {
return new FloatArgument<>(this.isRequired(), this.getName(), this.min, this.max,
this.getDefaultValue(), this.getSuggestionsProvider());
this.getDefaultValue(), this.getSuggestionsProvider()
);
}
}
@ -180,7 +185,8 @@ public final class FloatArgument<C> extends CommandArgument<C, Float> {
@Override
public @NonNull ArgumentParseResult<Float> parse(
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<String> inputQueue) {
final @NonNull Queue<String> inputQueue
) {
final String input = inputQueue.peek();
if (input == null) {
return ArgumentParseResult.failure(new NullPointerException("No input was provided"));

View file

@ -48,13 +48,15 @@ public final class IntegerArgument<C> extends CommandArgument<C, Integer> {
private final int min;
private final int max;
private IntegerArgument(final boolean required,
final @NonNull String name,
final int min,
final int max,
final String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider) {
private IntegerArgument(
final boolean required,
final @NonNull String name,
final int min,
final int max,
final String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider
) {
super(required, name, new IntegerParser<>(min, max), defaultValue, Integer.class, suggestionsProvider);
this.min = min;
this.max = max;
@ -101,8 +103,10 @@ public final class IntegerArgument<C> extends CommandArgument<C, Integer> {
* @param <C> Command sender type
* @return Created argument
*/
public static <C> @NonNull CommandArgument<C, Integer> optional(final @NonNull String name,
final int defaultNum) {
public static <C> @NonNull CommandArgument<C, Integer> optional(
final @NonNull String name,
final int defaultNum
) {
return IntegerArgument.<C>newBuilder(name).asOptionalWithDefault(Integer.toString(defaultNum)).build();
}
@ -163,7 +167,8 @@ public final class IntegerArgument<C> extends CommandArgument<C, Integer> {
@Override
public @NonNull IntegerArgument<C> build() {
return new IntegerArgument<>(this.isRequired(), this.getName(), this.min, this.max,
this.getDefaultValue(), this.getSuggestionsProvider());
this.getDefaultValue(), this.getSuggestionsProvider()
);
}
}
@ -209,7 +214,8 @@ public final class IntegerArgument<C> extends CommandArgument<C, Integer> {
@Override
public @NonNull ArgumentParseResult<Integer> parse(
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue) {
final @NonNull Queue<@NonNull String> inputQueue
) {
final String input = inputQueue.peek();
if (input == null) {
return ArgumentParseResult.failure(new NullPointerException("No input was provided"));
@ -250,8 +256,10 @@ public final class IntegerArgument<C> extends CommandArgument<C, Integer> {
}
@Override
public @NonNull List<@NonNull String> suggestions(final @NonNull CommandContext<C> commandContext,
final @NonNull String input) {
public @NonNull List<@NonNull String> suggestions(
final @NonNull CommandContext<C> commandContext,
final @NonNull String input
) {
return getSuggestions(this.min, this.max, input);
}

View file

@ -41,13 +41,15 @@ public final class LongArgument<C> extends CommandArgument<C, Long> {
private final long min;
private final long max;
private LongArgument(final boolean required,
final @NonNull String name,
final long min,
final long max,
final String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider) {
private LongArgument(
final boolean required,
final @NonNull String name,
final long min,
final long max,
final String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider
) {
super(required, name, new LongParser<>(min, max), defaultValue, Long.class, suggestionsProvider);
this.min = min;
this.max = max;
@ -94,8 +96,10 @@ public final class LongArgument<C> extends CommandArgument<C, Long> {
* @param <C> Command sender type
* @return Created argument
*/
public static <C> @NonNull CommandArgument<C, Long> optional(final @NonNull String name,
final long defaultNum) {
public static <C> @NonNull CommandArgument<C, Long> optional(
final @NonNull String name,
final long defaultNum
) {
return LongArgument.<C>newBuilder(name).asOptionalWithDefault(Long.toString(defaultNum)).build();
}
@ -156,7 +160,8 @@ public final class LongArgument<C> extends CommandArgument<C, Long> {
@Override
public @NonNull LongArgument<C> build() {
return new LongArgument<>(this.isRequired(), this.getName(), this.min,
this.max, this.getDefaultValue(), this.getSuggestionsProvider());
this.max, this.getDefaultValue(), this.getSuggestionsProvider()
);
}
}
@ -174,7 +179,8 @@ public final class LongArgument<C> extends CommandArgument<C, Long> {
@Override
public @NonNull ArgumentParseResult<Long> parse(
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue) {
final @NonNull Queue<@NonNull String> inputQueue
) {
final String input = inputQueue.peek();
if (input == null) {
return ArgumentParseResult.failure(new NullPointerException("No input was provided"));
@ -197,8 +203,10 @@ public final class LongArgument<C> extends CommandArgument<C, Long> {
}
@Override
public @NonNull List<@NonNull String> suggestions(final @NonNull CommandContext<C> commandContext,
final @NonNull String input) {
public @NonNull List<@NonNull String> suggestions(
final @NonNull CommandContext<C> commandContext,
final @NonNull String input
) {
return IntegerArgument.IntegerParser.getSuggestions(this.min, this.max, input);
}

View file

@ -41,13 +41,15 @@ public final class ShortArgument<C> extends CommandArgument<C, Short> {
private final short min;
private final short max;
private ShortArgument(final boolean required,
final @NonNull String name,
final short min,
final short max,
final String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<String>> suggestionsProvider) {
private ShortArgument(
final boolean required,
final @NonNull String name,
final short min,
final short max,
final String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<String>> suggestionsProvider
) {
super(required, name, new ShortParser<>(min, max), defaultValue, Short.class, suggestionsProvider);
this.min = min;
this.max = max;
@ -94,8 +96,10 @@ public final class ShortArgument<C> extends CommandArgument<C, Short> {
* @param <C> Command sender type
* @return Created argument
*/
public static <C> @NonNull CommandArgument<C, Short> optional(final @NonNull String name,
final short defaultNum) {
public static <C> @NonNull CommandArgument<C, Short> optional(
final @NonNull String name,
final short defaultNum
) {
return ShortArgument.<C>newBuilder(name).asOptionalWithDefault(Short.toString(defaultNum)).build();
}
@ -156,7 +160,8 @@ public final class ShortArgument<C> extends CommandArgument<C, Short> {
@Override
public @NonNull ShortArgument<C> build() {
return new ShortArgument<>(this.isRequired(), this.getName(), this.min, this.max,
this.getDefaultValue(), this.getSuggestionsProvider());
this.getDefaultValue(), this.getSuggestionsProvider()
);
}
}
@ -180,7 +185,8 @@ public final class ShortArgument<C> extends CommandArgument<C, Short> {
@Override
public @NonNull ArgumentParseResult<Short> parse(
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue) {
final @NonNull Queue<@NonNull String> inputQueue
) {
final String input = inputQueue.peek();
if (input == null) {
return ArgumentParseResult.failure(new NullPointerException("No input was provided"));
@ -203,8 +209,10 @@ public final class ShortArgument<C> extends CommandArgument<C, Short> {
}
@Override
public @NonNull List<@NonNull String> suggestions(final @NonNull CommandContext<C> commandContext,
final @NonNull String input) {
public @NonNull List<@NonNull String> suggestions(
final @NonNull CommandContext<C> commandContext,
final @NonNull String input
) {
return IntegerArgument.IntegerParser.getSuggestions(this.min, this.max, input);
}

View file

@ -40,14 +40,17 @@ public final class StringArgument<C> extends CommandArgument<C, String> {
private final StringMode stringMode;
private StringArgument(final boolean required,
final @NonNull String name,
final @NonNull StringMode stringMode,
final @NonNull String defaultValue,
final @NonNull BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider) {
private StringArgument(
final boolean required,
final @NonNull String name,
final @NonNull StringMode stringMode,
final @NonNull String defaultValue,
final @NonNull BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider
) {
super(required, name, new StringParser<>(stringMode, suggestionsProvider),
defaultValue, String.class, suggestionsProvider);
defaultValue, String.class, suggestionsProvider
);
this.stringMode = stringMode;
}
@ -81,8 +84,10 @@ public final class StringArgument<C> extends CommandArgument<C, String> {
* @param <C> Command sender type
* @return Created argument
*/
public static <C> @NonNull CommandArgument<C, String> of(final @NonNull String name,
final @NonNull StringMode stringMode) {
public static <C> @NonNull CommandArgument<C, String> of(
final @NonNull String name,
final @NonNull StringMode stringMode
) {
return StringArgument.<C>newBuilder(name).withMode(stringMode).asRequired().build();
}
@ -105,8 +110,10 @@ public final class StringArgument<C> extends CommandArgument<C, String> {
* @param <C> Command sender type
* @return Created argument
*/
public static <C> @NonNull CommandArgument<C, String> optional(final @NonNull String name,
final @NonNull StringMode stringMode) {
public static <C> @NonNull CommandArgument<C, String> optional(
final @NonNull String name,
final @NonNull StringMode stringMode
) {
return StringArgument.<C>newBuilder(name).withMode(stringMode).asOptional().build();
}
@ -118,8 +125,10 @@ public final class StringArgument<C> extends CommandArgument<C, String> {
* @param <C> Command sender type
* @return Created argument
*/
public static <C> @NonNull CommandArgument<C, String> optional(final @NonNull String name,
final @NonNull String defaultString) {
public static <C> @NonNull CommandArgument<C, String> optional(
final @NonNull String name,
final @NonNull String defaultString
) {
return StringArgument.<C>newBuilder(name).asOptionalWithDefault(defaultString).build();
}
@ -190,8 +199,10 @@ public final class StringArgument<C> extends CommandArgument<C, String> {
* @param suggestionsProvider Suggestions provider
* @return Builder instance
*/
public @NonNull Builder<C> withSuggestionsProvider(final @NonNull BiFunction<@NonNull CommandContext<C>,
@NonNull String, @NonNull List<@NonNull String>> suggestionsProvider) {
public @NonNull Builder<C> withSuggestionsProvider(
final @NonNull BiFunction<@NonNull CommandContext<C>,
@NonNull String, @NonNull List<@NonNull String>> suggestionsProvider
) {
this.suggestionsProvider = suggestionsProvider;
return this;
}
@ -204,7 +215,8 @@ public final class StringArgument<C> extends CommandArgument<C, String> {
@Override
public @NonNull StringArgument<C> build() {
return new StringArgument<>(this.isRequired(), this.getName(), this.stringMode,
this.getDefaultValue(), this.suggestionsProvider);
this.getDefaultValue(), this.suggestionsProvider
);
}
}
@ -221,16 +233,20 @@ public final class StringArgument<C> extends CommandArgument<C, String> {
* @param stringMode String parsing mode
* @param suggestionsProvider Suggestions provider
*/
public StringParser(final @NonNull StringMode stringMode,
final @NonNull BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider) {
public StringParser(
final @NonNull StringMode stringMode,
final @NonNull BiFunction<@NonNull CommandContext<C>, @NonNull String,
@NonNull List<@NonNull String>> suggestionsProvider
) {
this.stringMode = stringMode;
this.suggestionsProvider = suggestionsProvider;
}
@Override
public @NonNull ArgumentParseResult<String> parse(final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue) {
public @NonNull ArgumentParseResult<String> parse(
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue
) {
final String input = inputQueue.peek();
if (input == null) {
return ArgumentParseResult.failure(new NullPointerException("No input was provided"));
@ -294,8 +310,10 @@ public final class StringArgument<C> extends CommandArgument<C, String> {
}
@Override
public @NonNull List<@NonNull String> suggestions(final @NonNull CommandContext<C> commandContext,
final @NonNull String input) {
public @NonNull List<@NonNull String> suggestions(
final @NonNull CommandContext<C> commandContext,
final @NonNull String input
) {
return this.suggestionsProvider.apply(commandContext, input);
}
@ -312,6 +330,7 @@ public final class StringArgument<C> extends CommandArgument<C, String> {
public @NonNull StringMode getStringMode() {
return this.stringMode;
}
}

View file

@ -38,11 +38,13 @@ import java.util.function.BiFunction;
@SuppressWarnings("unused")
public final class UUIDArgument<C> extends CommandArgument<C, UUID> {
private UUIDArgument(final boolean required,
final @NonNull String name,
final @NonNull String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>,
@NonNull String, @NonNull List<@NonNull String>> suggestionsProvider) {
private UUIDArgument(
final boolean required,
final @NonNull String name,
final @NonNull String defaultValue,
final @Nullable BiFunction<@NonNull CommandContext<C>,
@NonNull String, @NonNull List<@NonNull String>> suggestionsProvider
) {
super(required, name, new UUIDParser<>(), defaultValue, UUID.class, suggestionsProvider);
}
@ -87,8 +89,10 @@ public final class UUIDArgument<C> extends CommandArgument<C, UUID> {
* @param <C> Command sender type
* @return Created component
*/
public static <C> @NonNull CommandArgument<C, UUID> optional(final @NonNull String name,
final @NonNull UUID defaultUUID) {
public static <C> @NonNull CommandArgument<C, UUID> optional(
final @NonNull String name,
final @NonNull UUID defaultUUID
) {
return UUIDArgument.<C>newBuilder(name).asOptionalWithDefault(defaultUUID.toString()).build();
}
@ -117,7 +121,8 @@ public final class UUIDArgument<C> extends CommandArgument<C, UUID> {
@Override
public @NonNull ArgumentParseResult<UUID> parse(
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> inputQueue) {
final @NonNull Queue<@NonNull String> inputQueue
) {
final String input = inputQueue.peek();
if (input == null) {
return ArgumentParseResult.failure(new NullPointerException("No input was provided"));
@ -136,6 +141,7 @@ public final class UUIDArgument<C> extends CommandArgument<C, UUID> {
public boolean isContextFree() {
return true;
}
}
@ -156,5 +162,7 @@ public final class UUIDArgument<C> extends CommandArgument<C, UUID> {
public String getMessage() {
return String.format("Could not parse UUID from '%s'.", input);
}
}
}

View file

@ -61,8 +61,10 @@ public final class CommandContext<C> {
* @param suggestions Whether or not the context is created for command suggestions
* @param commandSender Sender of the command
*/
public CommandContext(final boolean suggestions,
final @NonNull C commandSender) {
public CommandContext(
final boolean suggestions,
final @NonNull C commandSender
) {
this.commandSender = commandSender;
this.suggestions = suggestions;
}
@ -181,8 +183,10 @@ public final class CommandContext<C> {
* @param <T> Argument type
* @return Stored value, or supplied default value
*/
public <T> @Nullable T getOrDefault(final @NonNull CommandArgument<C, T> argument,
final @Nullable T defaultValue) {
public <T> @Nullable T getOrDefault(
final @NonNull CommandArgument<C, T> argument,
final @Nullable T defaultValue
) {
return this.<T>getOptional(argument.getName()).orElse(defaultValue);
}
@ -194,8 +198,10 @@ public final class CommandContext<C> {
* @param <T> Argument type
* @return Argument, or supplied default value
*/
public <T> @Nullable T getOrDefault(final @NonNull String key,
final @Nullable T defaultValue) {
public <T> @Nullable T getOrDefault(
final @NonNull String key,
final @Nullable T defaultValue
) {
return this.<T>getOptional(key).orElse(defaultValue);
}

View file

@ -36,7 +36,7 @@ public interface CommandContextFactory<C> {
* Create a new command context
*
* @param suggestions Whether or not the sender is requesting suggestions
* @param sender Command sender
* @param sender Command sender
* @return Command context
*/
@NonNull CommandContext<C> create(boolean suggestions, @NonNull C sender);

View file

@ -51,9 +51,11 @@ public final class AmbiguousNodeException extends IllegalStateException {
* @param ambiguousNode Node that caused exception
* @param children All children of the parent
*/
public AmbiguousNodeException(final @Nullable CommandArgument<?, ?> parentNode,
final @NonNull CommandArgument<?, ?> ambiguousNode,
final @NonNull List<@NonNull CommandArgument<?, ?>> children) {
public AmbiguousNodeException(
final @Nullable CommandArgument<?, ?> parentNode,
final @NonNull CommandArgument<?, ?> ambiguousNode,
final @NonNull List<@NonNull CommandArgument<?, ?>> children
) {
this.parentNode = parentNode;
this.ambiguousNode = ambiguousNode;
this.children = children;
@ -101,7 +103,7 @@ public final class AmbiguousNodeException extends IllegalStateException {
}
}
return stringBuilder.append(")")
.toString();
.toString();
}
}

View file

@ -39,9 +39,11 @@ public class ArgumentParseException extends CommandParseException {
* @param commandSender Command sender
* @param currentChain Chain leading up to the exception
*/
public ArgumentParseException(final @NonNull Throwable throwable,
final @NonNull Object commandSender,
final @NonNull List<@NonNull CommandArgument<?, ?>> currentChain) {
public ArgumentParseException(
final @NonNull Throwable throwable,
final @NonNull Object commandSender,
final @NonNull List<@NonNull CommandArgument<?, ?>> currentChain
) {
super(commandSender, currentChain);
this.cause = throwable;
}

View file

@ -44,8 +44,10 @@ public class CommandParseException extends IllegalArgumentException {
* @param commandSender Sender who executed the command
* @param currentChain Chain leading up to the exception
*/
protected CommandParseException(final @NonNull Object commandSender,
final @NonNull List<CommandArgument<?, ?>> currentChain) {
protected CommandParseException(
final @NonNull Object commandSender,
final @NonNull List<CommandArgument<?, ?>> currentChain
) {
this.commandSender = commandSender;
this.currentChain = currentChain;
}

View file

@ -42,9 +42,11 @@ public final class InvalidCommandSenderException extends CommandParseException {
* @param requiredSender The sender type that is required
* @param currentChain Chain leading up to the exception
*/
public InvalidCommandSenderException(final @NonNull Object commandSender,
final @NonNull Class<?> requiredSender,
final @NonNull List<@NonNull CommandArgument<?, ?>> currentChain) {
public InvalidCommandSenderException(
final @NonNull Object commandSender,
final @NonNull Class<?> requiredSender,
final @NonNull List<@NonNull CommandArgument<?, ?>> currentChain
) {
super(commandSender, currentChain);
this.requiredSender = requiredSender;
}
@ -60,8 +62,11 @@ public final class InvalidCommandSenderException extends CommandParseException {
@Override
public String getMessage() {
return String.format("%s is not allowed to execute that command. Must be of type %s",
getCommandSender().getClass().getSimpleName(),
requiredSender.getSimpleName());
return String.format(
"%s is not allowed to execute that command. Must be of type %s",
getCommandSender().getClass().getSimpleName(),
requiredSender.getSimpleName()
);
}
}

View file

@ -43,9 +43,11 @@ public class InvalidSyntaxException extends CommandParseException {
* @param commandSender Sender that sent the command
* @param currentChain Chain leading up to issue
*/
public InvalidSyntaxException(final @NonNull String correctSyntax,
final @NonNull Object commandSender,
final @NonNull List<@NonNull CommandArgument<?, ?>> currentChain) {
public InvalidSyntaxException(
final @NonNull String correctSyntax,
final @NonNull Object commandSender,
final @NonNull List<@NonNull CommandArgument<?, ?>> currentChain
) {
super(commandSender, currentChain);
this.correctSyntax = correctSyntax;
}

View file

@ -46,9 +46,11 @@ public class NoPermissionException extends CommandParseException {
* @param commandSender Command sender
* @param currentChain Chain leading up to the exception
*/
public NoPermissionException(final @NonNull CommandPermission missingPermission,
final @NonNull Object commandSender,
final @NonNull List<@NonNull CommandArgument<?, ?>> currentChain) {
public NoPermissionException(
final @NonNull CommandPermission missingPermission,
final @NonNull Object commandSender,
final @NonNull List<@NonNull CommandArgument<?, ?>> currentChain
) {
super(commandSender, currentChain);
this.missingPermission = missingPermission;
}

View file

@ -44,9 +44,11 @@ public final class NoSuchCommandException extends CommandParseException {
* @param currentChain Chain leading up to the exception
* @param command Entered command (following the command chain)
*/
public NoSuchCommandException(final @NonNull Object commandSender,
final @NonNull List<CommandArgument<?, ?>> currentChain,
final @NonNull String command) {
public NoSuchCommandException(
final @NonNull Object commandSender,
final @NonNull List<CommandArgument<?, ?>> currentChain,
final @NonNull String command
) {
super(commandSender, currentChain);
this.suppliedCommand = command;
}

View file

@ -38,9 +38,11 @@ public abstract class NumberParseException extends IllegalArgumentException {
* @param min Maximum value
* @param max Minimum value
*/
public NumberParseException(final @NonNull String input,
final @NonNull Number min,
final @NonNull Number max) {
public NumberParseException(
final @NonNull String input,
final @NonNull Number min,
final @NonNull Number max
) {
this.input = input;
this.min = min;
this.max = max;

View file

@ -50,9 +50,11 @@ public final class AsynchronousCommandExecutionCoordinator<C> extends CommandExe
private final Executor executor;
private final boolean synchronizeParsing;
private AsynchronousCommandExecutionCoordinator(final @Nullable Executor executor,
final boolean synchronizeParsing,
final @NonNull CommandTree<C> commandTree) {
private AsynchronousCommandExecutionCoordinator(
final @Nullable Executor executor,
final boolean synchronizeParsing,
final @NonNull CommandTree<C> commandTree
) {
super(commandTree);
this.executor = executor == null ? ForkJoinPool.commonPool() : executor;
this.synchronizeParsing = synchronizeParsing;
@ -70,8 +72,10 @@ public final class AsynchronousCommandExecutionCoordinator<C> extends CommandExe
}
@Override
public @NonNull CompletableFuture<CommandResult<C>> coordinateExecution(final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> input) {
public @NonNull CompletableFuture<CommandResult<C>> coordinateExecution(
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> input
) {
final Consumer<Command<C>> commandConsumer = command -> {
if (this.commandManager.postprocessContext(commandContext, command) == State.ACCEPTED) {

View file

@ -74,8 +74,10 @@ public abstract class CommandExecutionCoordinator<C> {
* @param input Command input
* @return Future that completes with the result
*/
public abstract @NonNull CompletableFuture<CommandResult<C>> coordinateExecution(@NonNull CommandContext<C> commandContext,
@NonNull Queue<@NonNull String> input);
public abstract @NonNull CompletableFuture<CommandResult<C>> coordinateExecution(
@NonNull CommandContext<C> commandContext,
@NonNull Queue<@NonNull String> input
);
/**
* Get the command tree
@ -100,8 +102,10 @@ public abstract class CommandExecutionCoordinator<C> {
}
@Override
public CompletableFuture<CommandResult<C>> coordinateExecution(final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> input) {
public CompletableFuture<CommandResult<C>> coordinateExecution(
final @NonNull CommandContext<C> commandContext,
final @NonNull Queue<@NonNull String> input
) {
final CompletableFuture<CommandResult<C>> completableFuture = new CompletableFuture<>();
try {
final @NonNull Pair<@Nullable Command<C>, @Nullable Exception> pair =

View file

@ -37,8 +37,10 @@ import java.util.List;
public final class FilteringCommandSuggestionProcessor<C> implements CommandSuggestionProcessor<C> {
@Override
public @NonNull List<@NonNull String> apply(final @NonNull CommandPreprocessingContext<C> context,
final @NonNull List<@NonNull String> strings) {
public @NonNull List<@NonNull String> apply(
final @NonNull CommandPreprocessingContext<C> context,
final @NonNull List<@NonNull String> strings
) {
final String input;
if (context.getInputQueue().isEmpty()) {
input = "";

View file

@ -45,8 +45,10 @@ public final class CommandPostprocessingContext<C> {
* @param commandContext Command context
* @param command Command instance
*/
public CommandPostprocessingContext(final @NonNull CommandContext<@NonNull C> commandContext,
final @NonNull Command<@NonNull C> command) {
public CommandPostprocessingContext(
final @NonNull CommandContext<@NonNull C> commandContext,
final @NonNull Command<@NonNull C> command
) {
this.commandContext = commandContext;
this.command = command;
}

View file

@ -31,4 +31,5 @@ import cloud.commandframework.services.types.ConsumerService;
* @param <C> Command sender type
*/
public interface CommandPostprocessor<C> extends ConsumerService<CommandPostprocessingContext<C>> {
}

View file

@ -45,8 +45,10 @@ public final class CommandPreprocessingContext<C> {
* @param commandContext Command context
* @param inputQueue Command input as supplied by sender
*/
public CommandPreprocessingContext(final @NonNull CommandContext<C> commandContext,
final @NonNull LinkedList<@NonNull String> inputQueue) {
public CommandPreprocessingContext(
final @NonNull CommandContext<C> commandContext,
final @NonNull LinkedList<@NonNull String> inputQueue
) {
this.commandContext = commandContext;
this.inputQueue = inputQueue;
}

View file

@ -35,4 +35,5 @@ import cloud.commandframework.services.types.ConsumerService;
* @param <C> Command sender type
*/
public interface CommandPreprocessor<C> extends ConsumerService<CommandPreprocessingContext<C>> {
}

View file

@ -72,10 +72,12 @@ public class CommandConfirmationManager<C> {
* @param notifier Notifier that gets called when a command gets added to the queue
* @param errorNotifier Notifier that gets called when someone tries to confirm a command with nothing in the queue
*/
public CommandConfirmationManager(final long timeout,
final @NonNull TimeUnit timeoutTimeUnit,
final @NonNull Consumer<@NonNull CommandPostprocessingContext<C>> notifier,
final @NonNull Consumer<@NonNull C> errorNotifier) {
public CommandConfirmationManager(
final long timeout,
final @NonNull TimeUnit timeoutTimeUnit,
final @NonNull Consumer<@NonNull CommandPostprocessingContext<C>> notifier,
final @NonNull Consumer<@NonNull C> errorNotifier
) {
this.notifier = notifier;
this.errorNotifier = errorNotifier;
this.pendingCommands = new LinkedHashMap<C, Pair<CommandPostprocessingContext<C>, Long>>() {
@ -141,8 +143,8 @@ public class CommandConfirmationManager<C> {
if (pending.isPresent()) {
final CommandPostprocessingContext<C> postprocessingContext = pending.get();
postprocessingContext.getCommand()
.getCommandExecutionHandler()
.execute(postprocessingContext.getCommandContext());
.getCommandExecutionHandler()
.execute(postprocessingContext.getCommandContext());
} else {
this.errorNotifier.accept(context.getSender());
}
@ -155,9 +157,9 @@ public class CommandConfirmationManager<C> {
@Override
public void accept(final @NonNull CommandPostprocessingContext<C> context) {
if (!context.getCommand()
.getCommandMeta()
.getOrDefault(CONFIRMATION_REQUIRED_META, "false")
.equals("true")) {
.getCommandMeta()
.getOrDefault(CONFIRMATION_REQUIRED_META, "false")
.equals("true")) {
return;
}
/* Add it to the "queue" */

View file

@ -48,7 +48,7 @@ public interface CommandRegistrationHandler {
*
* @param command Command to register
* @return {@code true} if the command was registered successfully,
* else {@code false}
* else {@code false}
*/
boolean registerCommand(@NonNull Command<?> command);

View file

@ -121,8 +121,10 @@ public class SimpleCommandMeta extends CommandMeta {
* @param value Value
* @return Builder instance
*/
public @NonNull Builder with(final @NonNull String key,
final @NonNull String value) {
public @NonNull Builder with(
final @NonNull String key,
final @NonNull String value
) {
this.map.put(key, value);
return this;
}

View file

@ -38,8 +38,10 @@ public class Pair<U, V> implements Tuple {
private final U first;
private final V second;
protected Pair(final U first,
final V second) {
protected Pair(
final U first,
final V second
) {
this.first = first;
this.second = second;
}
@ -53,8 +55,10 @@ public class Pair<U, V> implements Tuple {
* @param <V> Second type
* @return Created pair
*/
public static <U, V> @NonNull Pair<U, V> of(final U first,
final V second) {
public static <U, V> @NonNull Pair<U, V> of(
final U first,
final V second
) {
return new Pair<>(first, second);
}

View file

@ -42,10 +42,12 @@ public class Quartet<U, V, W, X> implements Tuple {
private final W third;
private final X fourth;
protected Quartet(final @NonNull U first,
final @NonNull V second,
final @NonNull W third,
final @NonNull X fourth) {
protected Quartet(
final @NonNull U first,
final @NonNull V second,
final @NonNull W third,
final @NonNull X fourth
) {
this.first = first;
this.second = second;
this.third = third;
@ -65,10 +67,12 @@ public class Quartet<U, V, W, X> implements Tuple {
* @param <X> Fourth type
* @return Created quartet
*/
public static <U, V, W, X> @NonNull Quartet<@NonNull U, @NonNull V, @NonNull W, @NonNull X> of(final @NonNull U first,
final @NonNull V second,
final @NonNull W third,
final @NonNull X fourth) {
public static <U, V, W, X> @NonNull Quartet<@NonNull U, @NonNull V, @NonNull W, @NonNull X> of(
final @NonNull U first,
final @NonNull V second,
final @NonNull W third,
final @NonNull X fourth
) {
return new Quartet<>(first, second, third, fourth);
}

View file

@ -44,11 +44,13 @@ public class Quintet<U, V, W, X, Y> implements Tuple {
private final X fourth;
private final Y fifth;
protected Quintet(final @NonNull U first,
final @NonNull V second,
final @NonNull W third,
final @NonNull X fourth,
final @NonNull Y fifth) {
protected Quintet(
final @NonNull U first,
final @NonNull V second,
final @NonNull W third,
final @NonNull X fourth,
final @NonNull Y fifth
) {
this.first = first;
this.second = second;
this.third = third;
@ -76,7 +78,8 @@ public class Quintet<U, V, W, X, Y> implements Tuple {
final @NonNull V second,
final @NonNull W third,
final @NonNull X fourth,
final @NonNull Y fifth) {
final @NonNull Y fifth
) {
return new Quintet<>(first, second, third, fourth, fifth);
}

View file

@ -46,12 +46,14 @@ public class Sextet<U, V, W, X, Y, Z> implements Tuple {
private final Y fifth;
private final Z sixth;
protected Sextet(final @NonNull U first,
final @NonNull V second,
final @NonNull W third,
final @NonNull X fourth,
final @NonNull Y fifth,
final @NonNull Z sixth) {
protected Sextet(
final @NonNull U first,
final @NonNull V second,
final @NonNull W third,
final @NonNull X fourth,
final @NonNull Y fifth,
final @NonNull Z sixth
) {
this.first = first;
this.second = second;
this.third = third;
@ -78,12 +80,13 @@ public class Sextet<U, V, W, X, Y, Z> implements Tuple {
* @return Created sextet
*/
public static <U, V, W, X, Y, Z> @NonNull Sextet<@NonNull U, @NonNull V, @NonNull W, @NonNull X, @NonNull Y, @NonNull Z> of(
final @NonNull U first,
final @NonNull V second,
final @NonNull W third,
final @NonNull X fourth,
final @NonNull Y fifth,
final @NonNull Z sixth) {
final @NonNull U first,
final @NonNull V second,
final @NonNull W third,
final @NonNull X fourth,
final @NonNull Y fifth,
final @NonNull Z sixth
) {
return new Sextet<>(first, second, third, fourth, fifth, sixth);
}
@ -166,7 +169,8 @@ public class Sextet<U, V, W, X, Y, Z> implements Tuple {
@Override
public final String toString() {
return String.format("(%s, %s, %s, %s, %s, %s)", this.first, this.second, this.third,
this.fourth, this.fifth, this.sixth);
this.fourth, this.fifth, this.sixth
);
}
@Override

View file

@ -40,9 +40,11 @@ public class Triplet<U, V, W> implements Tuple {
private final V second;
private final W third;
protected Triplet(final @NonNull U first,
final @NonNull V second,
final @NonNull W third) {
protected Triplet(
final @NonNull U first,
final @NonNull V second,
final @NonNull W third
) {
this.first = first;
this.second = second;
this.third = third;
@ -59,9 +61,11 @@ public class Triplet<U, V, W> implements Tuple {
* @param <W> Third type
* @return Created triplet
*/
public static <U, V, W> @NonNull Triplet<@NonNull U, @NonNull V, @NonNull W> of(final @NonNull U first,
final @NonNull V second,
final @NonNull W third) {
public static <U, V, W> @NonNull Triplet<@NonNull U, @NonNull V, @NonNull W> of(
final @NonNull U first,
final @NonNull V second,
final @NonNull W third
) {
return new Triplet<>(first, second, third);
}

View file

@ -50,10 +50,11 @@ class CommandHelpHandlerTest {
argument(IntegerArgument.of("int"), Description.of("A number")).build());
manager.command(manager.commandBuilder("vec")
.meta("description", "Takes in a vector")
.argumentPair("vec", Pair.of("x", "y"),
Pair.of(Double.class, Double.class), Description.of("Vector"))
.build());
.meta("description", "Takes in a vector")
.argumentPair("vec", Pair.of("x", "y"),
Pair.of(Double.class, Double.class), Description.of("Vector")
)
.build());
}
@Test
@ -88,8 +89,10 @@ class CommandHelpHandlerTest {
this.printTopic("vec", query4);
}
private void printTopic(final String query,
final CommandHelpHandler.HelpTopic<TestCommandSender> helpTopic) {
private void printTopic(
final String query,
final CommandHelpHandler.HelpTopic<TestCommandSender> helpTopic
) {
System.out.printf("Showing results for query: \"/%s\"\n", query);
if (helpTopic instanceof CommandHelpHandler.IndexHelpTopic) {
this.printIndexHelpTopic((CommandHelpHandler.IndexHelpTopic<TestCommandSender>) helpTopic);
@ -135,7 +138,7 @@ class CommandHelpHandlerTest {
private void printVerboseHelpTopic(final CommandHelpHandler.VerboseHelpTopic<TestCommandSender> helpTopic) {
System.out.printf("└── Command: /%s\n", manager.getCommandSyntaxFormatter()
.apply(helpTopic.getCommand().getArguments(), null));
.apply(helpTopic.getCommand().getArguments(), null));
System.out.printf(" ├── Description: %s\n", helpTopic.getDescription());
System.out.println(" └── Args: ");
final Iterator<CommandArgument<TestCommandSender, ?>> iterator = helpTopic.getCommand().getArguments().iterator();

View file

@ -23,8 +23,8 @@
//
package cloud.commandframework;
import cloud.commandframework.execution.CommandResult;
import cloud.commandframework.context.CommandContext;
import cloud.commandframework.execution.CommandResult;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
@ -73,7 +73,8 @@ final class CommandPerformanceTest {
double averageTime = elapsedTime / (double) amount;
System.out.printf("Average literal parse time: %fns (%f ms) | %d samples & %d iterations\n",
averageTime, averageTime / 10e6, 101, 100000);
averageTime, averageTime / 10e6, 101, 100000
);
}
@Test
@ -84,7 +85,7 @@ final class CommandPerformanceTest {
final Options options = new OptionsBuilder()
.include(ExecutionBenchmark.class.getSimpleName())
.build();
final Collection<RunResult> results = new Runner(options).run();
final Collection<RunResult> results = new Runner(options).run();
Assertions.assertFalse(results.isEmpty());
}

View file

@ -57,8 +57,10 @@ class CommandPermissionTest {
}
@Override
public boolean hasPermission(final TestCommandSender sender,
final String permission) {
public boolean hasPermission(
final TestCommandSender sender,
final String permission
) {
return acceptOne && permission.equalsIgnoreCase("test.permission.four");
}
@ -66,6 +68,7 @@ class CommandPermissionTest {
public CommandMeta createDefaultCommandMeta() {
return SimpleCommandMeta.empty();
}
}
}

View file

@ -33,21 +33,21 @@ import org.junit.jupiter.api.Test;
public class CommandPostProcessorTest {
private static final boolean[] state = new boolean[]{false};
private static CommandManager<TestCommandSender> manager;
private static final boolean[] state = new boolean[] {false};
@BeforeAll
static void newTree() {
manager = new TestCommandManager();
manager.command(manager.commandBuilder("test", SimpleCommandMeta.empty())
.handler(c -> state[0] = true)
.build());
.handler(c -> state[0] = true)
.build());
manager.registerCommandPostProcessor(new SamplePostprocessor());
}
@Test
void testPreprocessing() {
manager.executeCommand(new TestCommandSender(),"test").join();
manager.executeCommand(new TestCommandSender(), "test").join();
Assertions.assertEquals(false, state[0]);
}

View file

@ -40,24 +40,26 @@ public class CommandPreProcessorTest {
static void newTree() {
manager = new TestCommandManager();
manager.command(manager.commandBuilder("test", SimpleCommandMeta.empty())
.argument(EnumArgument.of(SampleEnum.class, "enum"))
.handler(
commandContext -> System.out.printf("enum = %s | integer = %d\n",
commandContext.<SampleEnum>getOptional(
"enum").orElse(
SampleEnum.VALUE1),
commandContext.<Integer>getOptional(
"int").orElseThrow(
() -> new NullPointerException(
"int"))))
.build());
.argument(EnumArgument.of(SampleEnum.class, "enum"))
.handler(
commandContext -> System.out.printf(
"enum = %s | integer = %d\n",
commandContext.<SampleEnum>getOptional(
"enum").orElse(
SampleEnum.VALUE1),
commandContext.<Integer>getOptional(
"int").orElseThrow(
() -> new NullPointerException(
"int"))
))
.build());
manager.registerCommandPreProcessor(new SamplePreprocessor());
}
@Test
void testPreprocessing() {
Assertions.assertEquals(10, manager.executeCommand(new TestCommandSender(), "10 test value1")
.join().getCommandContext().<Integer>getOptional("int").orElse(0));
.join().getCommandContext().<Integer>getOptional("int").orElse(0));
manager.executeCommand(new TestCommandSender(), "aa test value1").join();
}

View file

@ -45,38 +45,40 @@ public class CommandSuggestionsTest {
manager.command(manager.commandBuilder("test").literal("one").build());
manager.command(manager.commandBuilder("test").literal("two").build());
manager.command(manager.commandBuilder("test")
.literal("var")
.argument(StringArgument.<TestCommandSender>newBuilder("str")
.withSuggestionsProvider((c, s) -> Arrays.asList("one", "two")))
.argument(EnumArgument.of(TestEnum.class, "enum")));
.literal("var")
.argument(StringArgument.<TestCommandSender>newBuilder("str")
.withSuggestionsProvider((c, s) -> Arrays.asList("one", "two")))
.argument(EnumArgument.of(TestEnum.class, "enum")));
manager.command(manager.commandBuilder("test")
.literal("comb")
.argument(StringArgument.<TestCommandSender>newBuilder("str")
.withSuggestionsProvider((c, s) -> Arrays.asList("one", "two")))
.argument(IntegerArgument.<TestCommandSender>newBuilder("num")
.withMin(1).withMax(95).asOptional()));
.literal("comb")
.argument(StringArgument.<TestCommandSender>newBuilder("str")
.withSuggestionsProvider((c, s) -> Arrays.asList("one", "two")))
.argument(IntegerArgument.<TestCommandSender>newBuilder("num")
.withMin(1).withMax(95).asOptional()));
manager.command(manager.commandBuilder("test")
.literal("alt")
.argument(IntegerArgument.<TestCommandSender>newBuilder("num")
.withSuggestionsProvider((c, s) -> Arrays.asList("3", "33", "333"))));
.literal("alt")
.argument(IntegerArgument.<TestCommandSender>newBuilder("num")
.withSuggestionsProvider((c, s) -> Arrays.asList("3", "33", "333"))));
manager.command(manager.commandBuilder("com")
.argumentPair("com", Pair.of("x", "y"), Pair.of(Integer.class, TestEnum.class),
Description.empty())
.argument(IntegerArgument.of("int")));
.argumentPair("com", Pair.of("x", "y"), Pair.of(Integer.class, TestEnum.class),
Description.empty()
)
.argument(IntegerArgument.of("int")));
manager.command(manager.commandBuilder("com2")
.argumentPair("com", Pair.of("x", "enum"),
Pair.of(Integer.class, TestEnum.class), Description.empty()));
.argumentPair("com", Pair.of("x", "enum"),
Pair.of(Integer.class, TestEnum.class), Description.empty()
));
manager.command(manager.commandBuilder("flags")
.argument(IntegerArgument.of("num"))
.flag(manager.flagBuilder("enum")
.withArgument(EnumArgument.of(TestEnum.class, "enum"))
.build())
.flag(manager.flagBuilder("static")
.build())
.build());
.argument(IntegerArgument.of("num"))
.flag(manager.flagBuilder("enum")
.withArgument(EnumArgument.of(TestEnum.class, "enum"))
.build())
.flag(manager.flagBuilder("static")
.build())
.build());
}
@Test

View file

@ -40,7 +40,7 @@ class CommandTest {
void ensureOrdering() {
Assertions.assertThrows(IllegalArgumentException.class, () ->
Command.newBuilder("test", SimpleCommandMeta.empty()).argument(StringArgument.optional("something"))
.argument(StaticArgument.of("somethingelse")).build());
.argument(StaticArgument.of("somethingelse")).build());
}
}

View file

@ -51,102 +51,113 @@ class CommandTreeTest {
/* Build general test commands */
manager.command(manager.commandBuilder("test", SimpleCommandMeta.empty())
.literal("one").build())
.command(manager.commandBuilder("test", SimpleCommandMeta.empty())
.literal("two").withPermission("no").build())
.command(manager.commandBuilder("test", Collections.singleton("other"),
SimpleCommandMeta.empty())
.literal("opt", "öpt")
.argument(IntegerArgument
.optional("num", EXPECTED_INPUT_NUMBER))
.build())
.command(manager.commandBuilder("req").withSenderType(SpecificCommandSender.class).build());
.literal("one").build())
.command(manager.commandBuilder("test", SimpleCommandMeta.empty())
.literal("two").withPermission("no").build())
.command(manager.commandBuilder("test", Collections.singleton("other"),
SimpleCommandMeta.empty()
)
.literal("opt", "öpt")
.argument(IntegerArgument
.optional("num", EXPECTED_INPUT_NUMBER))
.build())
.command(manager.commandBuilder("req").senderType(SpecificCommandSender.class).build());
/* Build command to test command proxying */
final Command<TestCommandSender> toProxy = manager.commandBuilder("test")
.literal("unproxied")
.argument(StringArgument.of("string"))
.argument(IntegerArgument.of("int"))
.literal("anotherliteral")
.handler(c -> {
})
.build();
.literal("unproxied")
.argument(StringArgument.of("string"))
.argument(IntegerArgument.of("int"))
.literal("anotherliteral")
.handler(c -> {
})
.build();
manager.command(toProxy);
manager.command(manager.commandBuilder("proxy").proxies(toProxy).build());
/* Build command for testing intermediary and final executors */
manager.command(manager.commandBuilder("command")
.withPermission("command.inner")
.literal("inner")
.handler(c -> System.out.println("Using inner command")));
.withPermission("command.inner")
.literal("inner")
.handler(c -> System.out.println("Using inner command")));
manager.command(manager.commandBuilder("command")
.withPermission("command.outer")
.handler(c -> System.out.println("Using outer command")));
.withPermission("command.outer")
.handler(c -> System.out.println("Using outer command")));
/* Build command for testing compound types */
manager.command(manager.commandBuilder("pos")
.argument(ArgumentPair.of(manager, "pos", Pair.of("x", "y"),
Pair.of(Integer.class, Integer.class))
.simple())
.handler(c -> {
final Pair<Integer, Integer> pair = c.get("pos");
System.out.printf("X: %d | Y: %d\n", pair.getFirst(), pair.getSecond());
}));
.argument(ArgumentPair.of(manager, "pos", Pair.of("x", "y"),
Pair.of(Integer.class, Integer.class)
)
.simple())
.handler(c -> {
final Pair<Integer, Integer> pair = c.get("pos");
System.out.printf("X: %d | Y: %d\n", pair.getFirst(), pair.getSecond());
}));
manager.command(manager.commandBuilder("vec")
.argument(ArgumentPair.of(manager, "vec", Pair.of("x", "y"),
Pair.of(Double.class, Double.class))
.withMapper(Vector2.class,
(sender, pair) -> new Vector2(pair.getFirst(), pair.getSecond()))
)
.handler(c -> {
final Vector2 vector2 = c.get("vec");
System.out.printf("X: %f | Y: %f\n", vector2.getX(), vector2.getY());
}));
.argument(ArgumentPair.of(manager, "vec", Pair.of("x", "y"),
Pair.of(Double.class, Double.class)
)
.withMapper(
Vector2.class,
(sender, pair) -> new Vector2(pair.getFirst(), pair.getSecond())
)
)
.handler(c -> {
final Vector2 vector2 = c.get("vec");
System.out.printf("X: %f | Y: %f\n", vector2.getX(), vector2.getY());
}));
/* Build command for testing flags */
manager.command(manager.commandBuilder("flags")
.flag(manager.flagBuilder("test")
.withAliases("t")
.build())
.flag(manager.flagBuilder("test2")
.build())
.flag(manager.flagBuilder("num")
.withArgument(IntegerArgument.of("num")).build())
.handler(c -> {
System.out.println("Flag present? " + c.flags().isPresent("test"));
System.out.println("Numerical flag: " + c.flags().getValue("num", -10));
})
.build());
.flag(manager.flagBuilder("test")
.withAliases("t")
.build())
.flag(manager.flagBuilder("test2")
.build())
.flag(manager.flagBuilder("num")
.withArgument(IntegerArgument.of("num")).build())
.handler(c -> {
System.out.println("Flag present? " + c.flags().isPresent("test"));
System.out.println("Numerical flag: " + c.flags().getValue("num", -10));
})
.build());
}
@Test
void parse() {
final Pair<Command<TestCommandSender>, Exception> command = manager.getCommandTree()
.parse(new CommandContext<>(
new TestCommandSender()),
new LinkedList<>(
Arrays.asList("test",
"one")));
.parse(
new CommandContext<>(
new TestCommandSender()),
new LinkedList<>(
Arrays.asList(
"test",
"one"
))
);
Assertions.assertNotNull(command.getFirst());
Assertions.assertEquals(NoPermissionException.class, manager.getCommandTree()
.parse(new CommandContext<>(
new TestCommandSender()),
new LinkedList<>(
Arrays.asList("test", "two")))
.getSecond().getClass());
.parse(
new CommandContext<>(
new TestCommandSender()),
new LinkedList<>(
Arrays.asList("test", "two"))
)
.getSecond().getClass());
manager.getCommandTree()
.parse(new CommandContext<>(new TestCommandSender()), new LinkedList<>(Arrays.asList("test", "opt")))
.getFirst().getCommandExecutionHandler().execute(new CommandContext<>(new TestCommandSender()));
.parse(new CommandContext<>(new TestCommandSender()), new LinkedList<>(Arrays.asList("test", "opt")))
.getFirst().getCommandExecutionHandler().execute(new CommandContext<>(new TestCommandSender()));
manager.getCommandTree()
.parse(new CommandContext<>(new TestCommandSender()), new LinkedList<>(Arrays.asList("test", "opt", "12")))
.getFirst().getCommandExecutionHandler().execute(new CommandContext<>(new TestCommandSender()));
.parse(new CommandContext<>(new TestCommandSender()), new LinkedList<>(Arrays.asList("test", "opt", "12")))
.getFirst().getCommandExecutionHandler().execute(new CommandContext<>(new TestCommandSender()));
}
@Test
void testAlias() {
manager.getCommandTree()
.parse(new CommandContext<>(new TestCommandSender()), new LinkedList<>(Arrays.asList("other", "öpt", "12")))
.getFirst().getCommandExecutionHandler().execute(new CommandContext<>(new TestCommandSender()));
.parse(new CommandContext<>(new TestCommandSender()), new LinkedList<>(Arrays.asList("other", "öpt", "12")))
.getFirst().getCommandExecutionHandler().execute(new CommandContext<>(new TestCommandSender()));
}
@Test
@ -166,11 +177,11 @@ class CommandTreeTest {
void testDefaultParser() {
manager.command(
manager.commandBuilder("default")
.argument(manager.argumentBuilder(Integer.class, "int"))
.handler(context -> {
final int number = context.get("int");
System.out.printf("Supplied number is: %d\n", number);
})
.argument(manager.argumentBuilder(Integer.class, "int"))
.handler(context -> {
final int number = context.get("int");
System.out.printf("Supplied number is: %d\n", number);
})
);
manager.executeCommand(new TestCommandSender(), "default 5").join();
}
@ -224,6 +235,7 @@ class CommandTreeTest {
public static final class SpecificCommandSender extends TestCommandSender {
}

View file

@ -68,11 +68,13 @@ public class ParserRegistryTest {
final ParserParameters parserParameters = parserRegistry.parseAnnotations(parsedType, Collections.singleton(range));
Assertions.assertTrue(parserParameters.has(StandardParameters.RANGE_MIN));
Assertions.assertTrue(parserParameters.has(StandardParameters.RANGE_MAX));
final ArgumentParser<TestCommandSender, ?> parser = parserRegistry.createParser(parsedType,
parserParameters)
.orElseThrow(
() -> new NullPointerException(
"No parser found"));
final ArgumentParser<TestCommandSender, ?> parser = parserRegistry.createParser(
parsedType,
parserParameters
)
.orElseThrow(
() -> new NullPointerException(
"No parser found"));
Assertions.assertTrue(parser instanceof IntegerArgument.IntegerParser);
@SuppressWarnings("unchecked") final IntegerArgument.IntegerParser<TestCommandSender> integerParser =
(IntegerArgument.IntegerParser<TestCommandSender>) parser;
@ -81,11 +83,11 @@ public class ParserRegistryTest {
/* Test integer */
parserRegistry.createParser(TypeToken.get(int.class), ParserParameters.empty())
.orElseThrow(() -> new IllegalArgumentException("No parser found for int.class"));
.orElseThrow(() -> new IllegalArgumentException("No parser found for int.class"));
/* Test Enum */
parserRegistry.createParser(TypeToken.get(CommandManager.ManagerSettings.class), ParserParameters.empty())
.orElseThrow(() -> new IllegalArgumentException("No parser found for enum"));
.orElseThrow(() -> new IllegalArgumentException("No parser found for enum"));
}
}

View file

@ -39,29 +39,29 @@ class StringArgumentTest {
static void setup() {
manager = new TestCommandManager();
manager.command(manager.commandBuilder("quoted")
.argument(StringArgument.of("message1", StringArgument.StringMode.QUOTED))
.argument(StringArgument.of("message2"))
.handler(c -> {
final String message1 = c.get("message1");
final String message2 = c.get("message2");
storage[0] = message1;
storage[1] = message2;
})
.build());
.argument(StringArgument.of("message1", StringArgument.StringMode.QUOTED))
.argument(StringArgument.of("message2"))
.handler(c -> {
final String message1 = c.get("message1");
final String message2 = c.get("message2");
storage[0] = message1;
storage[1] = message2;
})
.build());
manager.command(manager.commandBuilder("single")
.argument(StringArgument.of("message"))
.handler(c -> {
final String message = c.get("message");
storage[0] = message;
})
.build());
.argument(StringArgument.of("message"))
.handler(c -> {
final String message = c.get("message");
storage[0] = message;
})
.build());
manager.command(manager.commandBuilder("greedy")
.argument(StringArgument.of("message", StringArgument.StringMode.GREEDY))
.handler(c -> {
final String message = c.get("message");
storage[0] = message;
})
.build());
.argument(StringArgument.of("message", StringArgument.StringMode.GREEDY))
.handler(c -> {
final String message = c.get("message");
storage[0] = message;
})
.build());
}
private static void clear() {