Skip to content

Commit e15e706

Browse files
authored
Small MinecraftHelp cleanup (#534)
1 parent 3647a0b commit e15e706

2 files changed

Lines changed: 51 additions & 14 deletions

File tree

  • cloud-core/src/main/java/cloud/commandframework/util
  • cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/minecraft/extras

cloud-core/src/main/java/cloud/commandframework/util/StringUtils.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
//
2424
package cloud.commandframework.util;
2525

26+
import java.util.function.Function;
27+
import java.util.regex.MatchResult;
28+
import java.util.regex.Matcher;
29+
import java.util.regex.Pattern;
2630
import org.apiguardian.api.API;
2731
import org.checkerframework.checker.nullness.qual.NonNull;
2832

@@ -51,4 +55,33 @@ public static int countCharOccurrences(final @NonNull String haystack, final cha
5155
}
5256
return occurrences;
5357
}
58+
59+
/**
60+
* Replace all matches in a string.
61+
*
62+
* @param string string to process
63+
* @param pattern replacement pattern
64+
* @param replacer replacement function
65+
* @return processed string
66+
*/
67+
public static @NonNull String replaceAll(
68+
final @NonNull String string,
69+
final @NonNull Pattern pattern,
70+
final @NonNull Function<@NonNull MatchResult, @NonNull String> replacer
71+
) {
72+
final Matcher matcher = pattern.matcher(string);
73+
matcher.reset();
74+
boolean result = matcher.find();
75+
if (result) {
76+
final StringBuffer sb = new StringBuffer();
77+
do {
78+
final String replacement = replacer.apply(matcher);
79+
matcher.appendReplacement(sb, replacement);
80+
result = matcher.find();
81+
} while (result);
82+
matcher.appendTail(sb);
83+
return sb.toString();
84+
}
85+
return string;
86+
}
5487
}

cloud-minecraft/cloud-minecraft-extras/src/main/java/cloud/commandframework/minecraft/extras/MinecraftHelp.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import cloud.commandframework.CommandHelpHandler;
3030
import cloud.commandframework.CommandManager;
3131
import cloud.commandframework.Description;
32+
import cloud.commandframework.util.StringUtils;
3233
import java.util.ArrayList;
3334
import java.util.Collections;
3435
import java.util.HashMap;
@@ -38,6 +39,7 @@
3839
import java.util.function.BiFunction;
3940
import java.util.function.Function;
4041
import java.util.function.Predicate;
42+
import java.util.regex.Pattern;
4143
import net.kyori.adventure.audience.Audience;
4244
import net.kyori.adventure.text.Component;
4345
import net.kyori.adventure.text.LinearComponents;
@@ -88,14 +90,22 @@ public final class MinecraftHelp<C> {
8890
public static final String MESSAGE_CLICK_FOR_NEXT_PAGE = "click_for_next_page";
8991
public static final String MESSAGE_CLICK_FOR_PREVIOUS_PAGE = "click_for_previous_page";
9092

93+
private static final Pattern STRING_PLACEHOLDER_PATTERN = Pattern.compile("<([a-z_]+)>");
94+
9195
private final AudienceProvider<C> audienceProvider;
9296
private final CommandManager<C> commandManager;
9397
private final String commandPrefix;
9498
private final Map<String, String> messageMap = new HashMap<>();
9599

96100
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+
};
99109
private BiFunction<C, String, Component> descriptionDecorator = (sender, description) -> Component.text(description);
100110
private HelpColors colors = DEFAULT_HELP_COLORS;
101111
private int headerFooterLength = DEFAULT_HEADER_FOOTER_LENGTH;
@@ -221,12 +231,15 @@ public void descriptionDecorator(final @NonNull BiFunction<@NonNull C, @NonNull
221231
}
222232

223233
/**
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.
225236
*
226237
* @param key Message key. These are constants in {@link MinecraftHelp}
227238
* @param value The text for the message
239+
* @since 2.0.0
228240
*/
229-
public void setMessage(
241+
@API(status = API.Status.STABLE, since = "2.0.0")
242+
public void message(
230243
final @NonNull String key,
231244
final @NonNull String value
232245
) {
@@ -678,16 +691,7 @@ private String pageCommand(final String query, final int page) {
678691
args.put("page", String.valueOf(attemptedPage));
679692
args.put("max_pages", String.valueOf(maxPages));
680693
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)
691695
);
692696
}
693697

0 commit comments

Comments
 (0)