Clean up descriptions

This commit is contained in:
Alexander Söderberg 2020-09-23 20:55:17 +02:00
parent b0cd22886d
commit 3b2ccdca14
No known key found for this signature in database
GPG key ID: C0207FF7EA146678
10 changed files with 281 additions and 37 deletions

View file

@ -27,6 +27,7 @@ import com.intellectualsites.commands.arguments.CommandArgument;
import com.intellectualsites.commands.arguments.StaticArgument;
import com.intellectualsites.commands.execution.CommandExecutionHandler;
import com.intellectualsites.commands.meta.CommandMeta;
import com.intellectualsites.commands.meta.SimpleCommandMeta;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@ -44,10 +45,9 @@ import java.util.function.Consumer;
*
* @param <C> Command sender type
*/
@SuppressWarnings("unused")
public class Command<C> {
@Nonnull private final Map<CommandArgument<C, ?>, String> arguments;
@Nonnull private final Map<CommandArgument<C, ?>, Description> arguments;
@Nonnull private final CommandExecutionHandler<C> commandExecutionHandler;
@Nullable private final Class<? extends C> senderType;
@Nonnull private final String commandPermission;
@ -62,7 +62,7 @@ public class Command<C> {
* @param commandPermission Command permission
* @param commandMeta Command meta instance
*/
public Command(@Nonnull final Map<CommandArgument<C, ?>, String> commandArguments,
public Command(@Nonnull final Map<CommandArgument<C, ?>, Description> commandArguments,
@Nonnull final CommandExecutionHandler<C> commandExecutionHandler,
@Nullable final Class<? extends C> senderType,
@Nonnull final String commandPermission,
@ -99,7 +99,7 @@ public class Command<C> {
* @param senderType Required sender type. May be {@code null}
* @param commandMeta Command meta instance
*/
public Command(@Nonnull final Map<CommandArgument<C, ?>, String> commandArguments,
public Command(@Nonnull final Map<CommandArgument<C, ?>, Description> commandArguments,
@Nonnull final CommandExecutionHandler<C> commandExecutionHandler,
@Nullable final Class<? extends C> senderType,
@Nonnull final CommandMeta commandMeta) {
@ -114,11 +114,33 @@ public class Command<C> {
* @param commandPermission Command permission
* @param commandMeta Command meta instance
*/
public Command(@Nonnull final Map<CommandArgument<C, ?>, String> commandArguments,
public Command(@Nonnull final Map<CommandArgument<C, ?>, Description> commandArguments,
@Nonnull final CommandExecutionHandler<C> commandExecutionHandler,
@Nonnull final String commandPermission,
@Nonnull final CommandMeta commandMeta) {
this(commandArguments, commandExecutionHandler, null, "", commandMeta);
this(commandArguments, commandExecutionHandler, null, commandPermission, commandMeta);
}
/**
* Create a new command builder. Is recommended to use the builder methods
* in {@link CommandManager} rather than invoking this method directly.
*
* @param commandName Base command argument
* @param commandMeta Command meta instance
* @param description Command description
* @param aliases Command aliases
* @param <C> Command sender type
* @return Command builder
*/
@Nonnull
public static <C> Builder<C> newBuilder(@Nonnull final String commandName,
@Nonnull final CommandMeta commandMeta,
@Nonnull final Description description,
@Nonnull final String... aliases) {
final Map<CommandArgument<C, ?>, Description> map = new LinkedHashMap<>();
map.put(StaticArgument.required(commandName, aliases), description);
return new Builder<>(null, commandMeta, null, map,
new CommandExecutionHandler.NullCommandExecutionHandler<>(), "");
}
/**
@ -135,8 +157,8 @@ public class Command<C> {
public static <C> Builder<C> newBuilder(@Nonnull final String commandName,
@Nonnull final CommandMeta commandMeta,
@Nonnull final String... aliases) {
final Map<CommandArgument<C, ?>, String> map = new LinkedHashMap<>();
map.put(StaticArgument.required(commandName, aliases), "");
final Map<CommandArgument<C, ?>, Description> map = new LinkedHashMap<>();
map.put(StaticArgument.required(commandName, aliases), Description.empty());
return new Builder<>(null, commandMeta, null, map,
new CommandExecutionHandler.NullCommandExecutionHandler<>(), "");
}
@ -199,7 +221,7 @@ public class Command<C> {
*/
@Nonnull
public String getArgumentDescription(@Nonnull final CommandArgument<C, ?> argument) {
return this.arguments.get(argument);
return this.arguments.get(argument).getDescription();
}
@ -212,7 +234,7 @@ public class Command<C> {
public static final class Builder<C> {
@Nonnull private final CommandMeta commandMeta;
@Nonnull private final Map<CommandArgument<C, ?>, String> commandArguments;
@Nonnull private final Map<CommandArgument<C, ?>, Description> commandArguments;
@Nonnull private final CommandExecutionHandler<C> commandExecutionHandler;
@Nullable private final Class<? extends C> senderType;
@Nonnull private final String commandPermission;
@ -221,7 +243,7 @@ public class Command<C> {
private Builder(@Nullable final CommandManager<C> commandManager,
@Nonnull final CommandMeta commandMeta,
@Nullable final Class<? extends C> senderType,
@Nonnull final Map<CommandArgument<C, ?>, String> commandArguments,
@Nonnull final Map<CommandArgument<C, ?>, Description> commandArguments,
@Nonnull final CommandExecutionHandler<C> commandExecutionHandler,
@Nonnull final String commandPermission) {
this.commandManager = commandManager;
@ -232,6 +254,20 @@ public class Command<C> {
this.commandMeta = Objects.requireNonNull(commandMeta, "Meta may not be null");
}
/**
* Add command meta to the internal command meta map
*
* @param key Meta key
* @param value Meta value
* @return New builder instance using the inserted meta key-value pair
*/
@Nonnull
public Builder<C> meta(@Nonnull final String key, @Nonnull final String value) {
final CommandMeta commandMeta = SimpleCommandMeta.builder().with(this.commandMeta).build();
return new Builder<>(this.commandManager, commandMeta, this.senderType, this.commandArguments,
this.commandExecutionHandler, this.commandPermission);
}
/**
* Supply a command manager instance to the builder. This will be used when attempting to
* retrieve command argument parsers, in the case that they're needed. This
@ -258,6 +294,21 @@ public class Command<C> {
return this.argument(StaticArgument.required(main, aliases));
}
/**
* Inserts a required {@link StaticArgument} into the command chain
*
* @param main Main argument name
* @param description Literal description
* @param aliases Argument aliases
* @return New builder instance with the modified command chain
*/
@Nonnull
public Builder<C> literal(@Nonnull final String main,
@Nonnull final Description description,
@Nonnull final String... aliases) {
return this.argument(StaticArgument.required(main, aliases), description);
}
/**
* Add a new command argument with an empty description to the command
*
@ -267,7 +318,7 @@ public class Command<C> {
*/
@Nonnull
public <T> Builder<C> argument(@Nonnull final CommandArgument<C, T> argument) {
return this.argument(argument, "");
return this.argument(argument, Description.empty());
}
/**
@ -275,12 +326,13 @@ public class Command<C> {
*
* @param argument Argument to add
* @param description Argument description
* @param <T> Argument type
* @param <T> Argument type
* @return New builder instance with the command argument inserted into the argument list
*/
@Nonnull
public <T> Builder<C> argument(@Nonnull final CommandArgument<C, T> argument, @Nonnull final String description) {
final Map<CommandArgument<C, ?>, String> commandArgumentMap = new LinkedHashMap<>(this.commandArguments);
public <T> Builder<C> argument(@Nonnull final CommandArgument<C, T> argument,
@Nonnull final Description description) {
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);

View file

@ -220,6 +220,23 @@ public abstract class CommandManager<C> {
*/
public abstract boolean hasPermission(@Nonnull C sender, @Nonnull String permission);
/**
* Create a new command builder
*
* @param name Command name
* @param aliases Command aliases
* @param description Command description
* @param meta Command meta
* @return Builder instance
*/
@Nonnull
public Command.Builder<C> commandBuilder(@Nonnull final String name,
@Nonnull final Collection<String> aliases,
@Nonnull final Description description,
@Nonnull final CommandMeta meta) {
return Command.newBuilder(name, meta, description, aliases.toArray(new String[0]));
}
/**
* Create a new command builder
*
@ -232,7 +249,24 @@ public abstract class CommandManager<C> {
public Command.Builder<C> commandBuilder(@Nonnull final String name,
@Nonnull final Collection<String> aliases,
@Nonnull final CommandMeta meta) {
return Command.newBuilder(name, meta, aliases.toArray(new String[0]));
return Command.newBuilder(name, meta, Description.empty(), aliases.toArray(new String[0]));
}
/**
* Create a new command builder
*
* @param name Command name
* @param meta Command meta
* @param description Command description
* @param aliases Command aliases
* @return Builder instance
*/
@Nonnull
public Command.Builder<C> commandBuilder(@Nonnull final String name,
@Nonnull final CommandMeta meta,
@Nonnull final Description description,
@Nonnull final String... aliases) {
return Command.newBuilder(name, meta, description, aliases);
}
/**
@ -247,21 +281,39 @@ public abstract class CommandManager<C> {
public Command.Builder<C> commandBuilder(@Nonnull final String name,
@Nonnull final CommandMeta meta,
@Nonnull final String... aliases) {
return Command.newBuilder(name, meta, aliases);
return Command.newBuilder(name, meta, Description.empty(), aliases);
}
/**
* Create a new command builder using a default command meta instance.
*
* @param name Command name
* @param aliases Command aliases
* @param name Command name
* @param description Command description
* @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
*/
@Nonnull
public Command.Builder<C> commandBuilder(@Nonnull final String name, @Nonnull final String... aliases) {
return Command.<C>newBuilder(name, this.createDefaultCommandMeta(), aliases).manager(this);
public Command.Builder<C> commandBuilder(@Nonnull final String name,
@Nonnull final Description description,
@Nonnull final 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
* @return Builder instance
* @throws UnsupportedOperationException If the command manager does not support default command meta creation
* @see #createDefaultCommandMeta() Default command meta creation
*/
@Nonnull
public Command.Builder<C> commandBuilder(@Nonnull final String name,
@Nonnull final String... aliases) {
return Command.<C>newBuilder(name, this.createDefaultCommandMeta(), Description.empty(), aliases).manager(this);
}
/**
@ -344,8 +396,8 @@ public abstract class CommandManager<C> {
/**
* Postprocess a command context instance
*
* @param context Command context
* @param command Command instance
* @param context Command context
* @param command Command instance
* @return {@link State#ACCEPTED} if the command should be parsed and executed, else {@link State#REJECTED}
* @see #registerCommandPostProcessor(CommandPostprocessor) Register a command postprocessor
*/

View file

@ -0,0 +1,86 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package com.intellectualsites.commands;
import javax.annotation.Nonnull;
/**
* {@link com.intellectualsites.commands.arguments.CommandArgument} description
*/
public final class Description {
/**
* Empty command description
*/
private static final Description EMPTY = Description.of("");
private final String description;
private Description(@Nonnull final String description) {
this.description = description;
}
/**
* Get an empty command description
*
* @return Command description
*/
@Nonnull
public static Description empty() {
return EMPTY;
}
/**
* Create a command description instance
*
* @param string Command description
* @return Created command description
*/
@Nonnull
public static Description of(@Nonnull final String string) {
return new Description(string);
}
/**
* Get the command description
*
* @return Command description
*/
@Nonnull
public String getDescription() {
return this.description;
}
/**
* Get the command description
*
* @return Command description
*/
@Nonnull
@Override
public String toString() {
return this.description;
}
}

View file

@ -85,15 +85,15 @@ public final class BooleanArgument<C> extends CommandArgument<C, Boolean> {
/**
* Create a new required command argument with a default value
*
* @param name Argument name
* @param defaultNum Default num
* @param <C> Command sender type
* @param name Argument name
* @param defaultBoolean Default num
* @param <C> Command sender type
* @return Created argument
*/
@Nonnull
public static <C> CommandArgument<C, Boolean> optional(@Nonnull final String name,
final String defaultNum) {
return BooleanArgument.<C>newBuilder(name).asOptionalWithDefault(defaultNum).build();
final boolean defaultBoolean) {
return BooleanArgument.<C>newBuilder(name).asOptionalWithDefault(Boolean.toString(defaultBoolean)).build();
}
/**

View file

@ -125,4 +125,17 @@ public final class CommandContext<C> {
return (T) value;
}
/**
* Get a value if it exists, else return the provided default value
*
* @param key Argument key
* @param defaultValue Default value
* @param <T> Argument type
* @return Argument, or supplied default value
*/
@Nonnull
public <T> T getOrDefault(@Nonnull final String key, @Nonnull final T defaultValue) {
return this.<T>get(key).orElse(defaultValue);
}
}

View file

@ -47,7 +47,7 @@ class CommandHelpHandlerTest {
manager.command(manager.commandBuilder("test", meta1).literal("this").literal("thing").build());
final SimpleCommandMeta meta2 = SimpleCommandMeta.builder().with("description", "Command with variables").build();
manager.command(manager.commandBuilder("test", meta2).literal("int").
argument(IntegerArgument.required("int"), "A number").build());
argument(IntegerArgument.required("int"), Description.of("A number")).build());
}
@Test