|
29 | 29 | import cloud.commandframework.CommandHelpHandler; |
30 | 30 | import cloud.commandframework.CommandManager; |
31 | 31 | import cloud.commandframework.Description; |
| 32 | +import cloud.commandframework.util.StringUtils; |
32 | 33 | import java.util.ArrayList; |
33 | 34 | import java.util.Collections; |
34 | 35 | import java.util.HashMap; |
|
38 | 39 | import java.util.function.BiFunction; |
39 | 40 | import java.util.function.Function; |
40 | 41 | import java.util.function.Predicate; |
| 42 | +import java.util.regex.Pattern; |
41 | 43 | import net.kyori.adventure.audience.Audience; |
42 | 44 | import net.kyori.adventure.text.Component; |
43 | 45 | import net.kyori.adventure.text.LinearComponents; |
@@ -88,14 +90,22 @@ public final class MinecraftHelp<C> { |
88 | 90 | public static final String MESSAGE_CLICK_FOR_NEXT_PAGE = "click_for_next_page"; |
89 | 91 | public static final String MESSAGE_CLICK_FOR_PREVIOUS_PAGE = "click_for_previous_page"; |
90 | 92 |
|
| 93 | + private static final Pattern STRING_PLACEHOLDER_PATTERN = Pattern.compile("<([a-z_]+)>"); |
| 94 | + |
91 | 95 | private final AudienceProvider<C> audienceProvider; |
92 | 96 | private final CommandManager<C> commandManager; |
93 | 97 | private final String commandPrefix; |
94 | 98 | private final Map<String, String> messageMap = new HashMap<>(); |
95 | 99 |
|
96 | 100 | private Predicate<Command<C>> commandFilter = c -> true; |
97 | | - private MessageProvider<C> messageProvider = |
98 | | - (sender, key, args) -> text(this.messageMap.get(key)); |
| 101 | + private MessageProvider<C> messageProvider = (sender, key, args) -> { |
| 102 | + final String message = this.messageMap.get(key); |
| 103 | + if (args.isEmpty()) { |
| 104 | + return text(message); |
| 105 | + } |
| 106 | + return text(StringUtils.replaceAll( |
| 107 | + message, STRING_PLACEHOLDER_PATTERN, matchResult -> args.get(matchResult.group(1)))); |
| 108 | + }; |
99 | 109 | private BiFunction<C, String, Component> descriptionDecorator = (sender, description) -> Component.text(description); |
100 | 110 | private HelpColors colors = DEFAULT_HELP_COLORS; |
101 | 111 | private int headerFooterLength = DEFAULT_HEADER_FOOTER_LENGTH; |
@@ -221,12 +231,15 @@ public void descriptionDecorator(final @NonNull BiFunction<@NonNull C, @NonNull |
221 | 231 | } |
222 | 232 |
|
223 | 233 | /** |
224 | | - * Configure a message |
| 234 | + * Configure a plain string message, used by the default {@link MessageProvider}. |
| 235 | + * Placeholders in the format {@literal <name>} will be replaced. |
225 | 236 | * |
226 | 237 | * @param key Message key. These are constants in {@link MinecraftHelp} |
227 | 238 | * @param value The text for the message |
| 239 | + * @since 2.0.0 |
228 | 240 | */ |
229 | | - public void setMessage( |
| 241 | + @API(status = API.Status.STABLE, since = "2.0.0") |
| 242 | + public void message( |
230 | 243 | final @NonNull String key, |
231 | 244 | final @NonNull String value |
232 | 245 | ) { |
@@ -678,16 +691,7 @@ private String pageCommand(final String query, final int page) { |
678 | 691 | args.put("page", String.valueOf(attemptedPage)); |
679 | 692 | args.put("max_pages", String.valueOf(maxPages)); |
680 | 693 | return this.highlight( |
681 | | - this.messageProvider.provide(sender, MESSAGE_PAGE_OUT_OF_RANGE, args) |
682 | | - .color(this.colors.text) |
683 | | - .replaceText(config -> { |
684 | | - config.matchLiteral("<page>"); |
685 | | - config.replacement(String.valueOf(attemptedPage)); |
686 | | - }) |
687 | | - .replaceText(config -> { |
688 | | - config.matchLiteral("<max_pages>"); |
689 | | - config.replacement(String.valueOf(maxPages)); |
690 | | - }) |
| 694 | + this.messageProvider.provide(sender, MESSAGE_PAGE_OUT_OF_RANGE, args).color(this.colors.text) |
691 | 695 | ); |
692 | 696 | } |
693 | 697 |
|
|
0 commit comments