Update TextColorArgument with captions

This commit is contained in:
jmp 2020-10-16 13:54:49 -07:00 committed by Alexander Söderberg
parent fba29041e6
commit c7c286eb7a
3 changed files with 32 additions and 27 deletions

View file

@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added ExampleVelocityPlugin
- Added CloudInjectionModule to cloud-velocity
- Added PlayerArgument to cloud-velocity
- Added TextColorArgument to minecraft-extras
## [1.0.2] - 2020-10-18

View file

@ -29,6 +29,7 @@ import cloud.commandframework.arguments.parser.ArgumentParser;
import cloud.commandframework.captions.CaptionVariable;
import cloud.commandframework.captions.StandardCaptionKeys;
import cloud.commandframework.context.CommandContext;
import cloud.commandframework.exceptions.parsing.NoInputProvidedException;
import cloud.commandframework.exceptions.parsing.ParserException;
import cloud.commandframework.types.tuples.Pair;
import io.leangen.geantyref.TypeToken;
@ -44,11 +45,13 @@ import java.util.Queue;
import java.util.regex.Pattern;
/**
* Parser for color codes
* Parser for color codes.
* <p>
* Accepts {@link NamedTextColor NamedTextColors}, Legacy Minecraft {@literal &} color codes, Hex Codes (#RRGGBB)
*
* @param <C> Command sender type
*/
public final class ColorArgument<C> extends CommandArgument<C, TextColor> {
public final class TextColorArgument<C> extends CommandArgument<C, TextColor> {
private static final Pattern LEGACY_PREDICATE = Pattern.compile(
"&[0-9a-fA-F]"
@ -77,7 +80,7 @@ public final class ColorArgument<C> extends CommandArgument<C, TextColor> {
Pair.of('f', NamedTextColor.WHITE)
);
private ColorArgument(
private TextColorArgument(
final boolean required,
final @NonNull String name,
final @NonNull String defaultValue
@ -85,7 +88,7 @@ public final class ColorArgument<C> extends CommandArgument<C, TextColor> {
super(
required,
name,
new ColorArgumentParser<>(),
new TextColorParser<>(),
defaultValue,
TypeToken.get(TextColor.class),
null,
@ -94,14 +97,14 @@ public final class ColorArgument<C> extends CommandArgument<C, TextColor> {
}
/**
* Create a new required colour argument
* Create a new required TextColor argument
*
* @param name Argument name
* @param <C> Command sender type
* @return Created argument
*/
public static <C> @NonNull ColorArgument<C> of(final @NonNull String name) {
return new ColorArgument<>(
public static <C> @NonNull TextColorArgument<C> of(final @NonNull String name) {
return new TextColorArgument<>(
true,
name,
""
@ -109,14 +112,14 @@ public final class ColorArgument<C> extends CommandArgument<C, TextColor> {
}
/**
* Create a new optional colour argument
* Create a new optional TextColor argument
*
* @param name Argument name
* @param <C> Command sender type
* @return Created argument
*/
public static <C> @NonNull ColorArgument<C> optional(final @NonNull String name) {
return new ColorArgument<>(
public static <C> @NonNull TextColorArgument<C> optional(final @NonNull String name) {
return new TextColorArgument<>(
false,
name,
""
@ -124,18 +127,18 @@ public final class ColorArgument<C> extends CommandArgument<C, TextColor> {
}
/**
* Create a new optional colour argument
* Create a new optional TextColor argument
*
* @param name Argument name
* @param defaultValue Default value
* @param <C> Command sender type
* @return Created argument
*/
public static <C> @NonNull ColorArgument<C> optionalWithDefault(
public static <C> @NonNull TextColorArgument<C> optionalWithDefault(
final @NonNull String name,
final @NonNull String defaultValue
) {
return new ColorArgument<>(
return new TextColorArgument<>(
false,
name,
defaultValue
@ -143,7 +146,7 @@ public final class ColorArgument<C> extends CommandArgument<C, TextColor> {
}
public static final class ColorArgumentParser<C> implements ArgumentParser<C, TextColor> {
public static final class TextColorParser<C> implements ArgumentParser<C, TextColor> {
@Override
public @NonNull ArgumentParseResult<@NonNull TextColor> parse(
@ -152,9 +155,10 @@ public final class ColorArgument<C> extends CommandArgument<C, TextColor> {
) {
final String input = inputQueue.peek();
if (input == null) {
throw new NullPointerException(
"No input was supplied"
);
return ArgumentParseResult.failure(new NoInputProvidedException(
TextColorParser.class,
commandContext
));
}
if (LEGACY_PREDICATE.matcher(input).matches()) {
final char code = input.substring(1).toLowerCase().charAt(0);
@ -181,7 +185,7 @@ public final class ColorArgument<C> extends CommandArgument<C, TextColor> {
);
}
return ArgumentParseResult.failure(
new ColorArgumentParseException(
new TextColorParseException(
commandContext,
input
)
@ -211,14 +215,14 @@ public final class ColorArgument<C> extends CommandArgument<C, TextColor> {
}
private static final class ColorArgumentParseException extends ParserException {
private static final class TextColorParseException extends ParserException {
private ColorArgumentParseException(
private TextColorParseException(
final @NonNull CommandContext<?> commandContext,
final @NonNull String input
) {
super(
ColorArgumentParser.class,
TextColorParser.class,
commandContext,
StandardCaptionKeys.ARGUMENT_PARSE_FAILURE_COLOR,
CaptionVariable.of("input", input)

View file

@ -57,7 +57,7 @@ import cloud.commandframework.execution.AsynchronousCommandExecutionCoordinator;
import cloud.commandframework.execution.CommandExecutionCoordinator;
import cloud.commandframework.extra.confirmation.CommandConfirmationManager;
import cloud.commandframework.meta.CommandMeta;
import cloud.commandframework.minecraft.extras.ColorArgument;
import cloud.commandframework.minecraft.extras.TextColorArgument;
import cloud.commandframework.paper.PaperCommandManager;
import cloud.commandframework.types.tuples.Triplet;
import io.leangen.geantyref.TypeToken;
@ -319,23 +319,23 @@ public final class ExamplePlugin extends JavaPlugin {
.meta("description", "Sets the color scheme for '/example help'")
.literal("helpcolors")
.argument(
ColorArgument.of("primary"),
TextColorArgument.of("primary"),
Description.of("The primary color for the color scheme")
)
.argument(
ColorArgument.of("highlight"),
TextColorArgument.of("highlight"),
Description.of("The primary color used to highlight commands and queries")
)
.argument(
ColorArgument.of("alternate_highlight"),
TextColorArgument.of("alternate_highlight"),
Description.of("The secondary color used to highlight commands and queries")
)
.argument(
ColorArgument.of("text"),
TextColorArgument.of("text"),
Description.of("The color used for description text")
)
.argument(
ColorArgument.of("accent"),
TextColorArgument.of("accent"),
Description.of("The color used for accents and symbols")
)
.handler(c -> minecraftHelp.setHelpColors(MinecraftHelp.HelpColors.of(