From 2277bf5ef8c28aaf2c841d97c9f8e39583a498f7 Mon Sep 17 00:00:00 2001 From: Aldin Date: Thu, 26 May 2022 11:18:45 +0200 Subject: [PATCH] feat: allow a custom CaptionVariableReplacementHandler (#352) --- CHANGELOG.md | 1 + .../commandframework/CommandManager.java | 27 ++++++++++++++++++- .../context/CommandContext.java | 5 ++-- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index caae82bf..d9453c02 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added +- Allow for setting a custom `CaptionVariableReplacementHandler` on the command manager ([#352](https://github.com/Incendo/cloud/pull/352)) - Annotations: Annotation string processors ([#353](https://github.com/Incendo/cloud/pull/353)) ### Fixed diff --git a/cloud-core/src/main/java/cloud/commandframework/CommandManager.java b/cloud-core/src/main/java/cloud/commandframework/CommandManager.java index 09b76748..d886bea5 100644 --- a/cloud-core/src/main/java/cloud/commandframework/CommandManager.java +++ b/cloud-core/src/main/java/cloud/commandframework/CommandManager.java @@ -35,7 +35,9 @@ import cloud.commandframework.arguments.parser.ParserParameter; import cloud.commandframework.arguments.parser.ParserRegistry; import cloud.commandframework.arguments.parser.StandardParserRegistry; import cloud.commandframework.captions.CaptionRegistry; +import cloud.commandframework.captions.CaptionVariableReplacementHandler; import cloud.commandframework.captions.SimpleCaptionRegistryFactory; +import cloud.commandframework.captions.SimpleCaptionVariableReplacementHandler; import cloud.commandframework.context.CommandContext; import cloud.commandframework.context.CommandContextFactory; import cloud.commandframework.context.StandardCommandContextFactory; @@ -96,6 +98,7 @@ public abstract class CommandManager { private final CommandTree commandTree; private final CommandSuggestionEngine commandSuggestionEngine; + private CaptionVariableReplacementHandler captionVariableReplacementHandler = new SimpleCaptionVariableReplacementHandler(); private CommandSyntaxFormatter commandSyntaxFormatter = new StandardCommandSyntaxFormatter<>(); private CommandSuggestionProcessor commandSuggestionProcessor = new FilteringCommandSuggestionProcessor<>(); private CommandRegistrationHandler commandRegistrationHandler; @@ -240,6 +243,28 @@ public abstract class CommandManager { return this.command(command.manager(this).build()); } + /** + * Get the caption variable replacement handler. + * + * @return the caption variable replacement handler + * @since 1.7.0 + */ + public @NonNull CaptionVariableReplacementHandler captionVariableReplacementHandler() { + return this.captionVariableReplacementHandler; + } + + /** + * Sets the caption variable replacement handler. + * + * @param captionVariableReplacementHandler new replacement handler + * @since 1.7.0 + */ + public void captionVariableReplacementHandler( + final @NonNull CaptionVariableReplacementHandler captionVariableReplacementHandler + ) { + this.captionVariableReplacementHandler = captionVariableReplacementHandler; + } + /** * Get the command syntax formatter * @@ -455,7 +480,7 @@ public abstract class CommandManager { * @param aliases Command aliases * @return Builder instance * @deprecated for removal since 1.4.0. Use {@link #commandBuilder(String, CommandMeta, ArgumentDescription, String...)} - * instead. + * instead. */ @Deprecated public Command.@NonNull Builder commandBuilder( diff --git a/cloud-core/src/main/java/cloud/commandframework/context/CommandContext.java b/cloud-core/src/main/java/cloud/commandframework/context/CommandContext.java index 1f24c9fa..3b4a0de2 100644 --- a/cloud-core/src/main/java/cloud/commandframework/context/CommandContext.java +++ b/cloud-core/src/main/java/cloud/commandframework/context/CommandContext.java @@ -52,8 +52,7 @@ import org.checkerframework.checker.nullness.qual.Nullable; */ public final class CommandContext { - private final CaptionVariableReplacementHandler captionVariableReplacementHandler = - new SimpleCaptionVariableReplacementHandler(); + private final CaptionVariableReplacementHandler captionVariableReplacementHandler; private final Map, ArgumentTiming> argumentTimings = new HashMap<>(); private final FlagContext flagContext = FlagContext.create(); private final Map, Object> internalStorage = new HashMap<>(); @@ -104,6 +103,7 @@ public final class CommandContext { this.commandSender = commandSender; this.suggestions = suggestions; this.captionRegistry = captionRegistry; + this.captionVariableReplacementHandler = new SimpleCaptionVariableReplacementHandler(); this.commandManager = null; } @@ -124,6 +124,7 @@ public final class CommandContext { this.suggestions = suggestions; this.commandManager = commandManager; this.captionRegistry = commandManager.getCaptionRegistry(); + this.captionVariableReplacementHandler = commandManager.captionVariableReplacementHandler(); } /**