From f331ef8a7afb96edfc4a0944db6efec6125c98e0 Mon Sep 17 00:00:00 2001 From: srnyx <25808801+srnyx@users.noreply.github.com> Date: Wed, 28 Jun 2023 12:04:14 -0400 Subject: [PATCH] Add option for parsing PAPI placeholders in a message --- .../srnyx/annoyingapi/AnnoyingMessage.java | 41 ++++++++++++++++--- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/api/src/main/java/xyz/srnyx/annoyingapi/AnnoyingMessage.java b/api/src/main/java/xyz/srnyx/annoyingapi/AnnoyingMessage.java index fc374a0..490a571 100644 --- a/api/src/main/java/xyz/srnyx/annoyingapi/AnnoyingMessage.java +++ b/api/src/main/java/xyz/srnyx/annoyingapi/AnnoyingMessage.java @@ -35,23 +35,51 @@ * Represents a message from the {@link AnnoyingOptions#messagesFileName} file */ public class AnnoyingMessage extends Stringable { + /** + * The {@link AnnoyingPlugin} instance + */ @NotNull private final AnnoyingPlugin plugin; + /** + * The key of the message in the messages file + */ @NotNull private final String key; + /** + * Whether to parse PAPI placeholders + */ + private final boolean parsePapiPlaceholders; + /** + * The cached splitter for placeholder parameters + */ @Nullable private String splitterPlaceholder; + /** + * The replacements for the message + */ @NotNull private final Set replacements = new HashSet<>(); /** * Constructs a new {@link AnnoyingMessage} with the specified key * - * @param plugin the plugin getting the message - * @param key the key of the message + * @param plugin {@link #plugin} + * @param key {@link #key} + * @param parsePapiPlaceholders {@link #parsePapiPlaceholders} */ - public AnnoyingMessage(@NotNull AnnoyingPlugin plugin, @NotNull String key) { + public AnnoyingMessage(@NotNull AnnoyingPlugin plugin, @NotNull String key, boolean parsePapiPlaceholders) { this.plugin = plugin; this.key = key; + this.parsePapiPlaceholders = parsePapiPlaceholders; plugin.globalPlaceholders.forEach((placeholder, value) -> replace("%" + placeholder + "%", value)); } + /** + * Constructs a new {@link AnnoyingMessage} with the specified key + * + * @param plugin {@link #plugin} + * @param key {@link #key} + */ + public AnnoyingMessage(@NotNull AnnoyingPlugin plugin, @NotNull String key) { + this(plugin, key, true); + } + /** * Constructs a new {@link AnnoyingMessage} from another {@link AnnoyingMessage} (copy constructor) * @@ -60,6 +88,7 @@ public AnnoyingMessage(@NotNull AnnoyingPlugin plugin, @NotNull String key) { public AnnoyingMessage(@NotNull AnnoyingMessage message) { this.plugin = message.plugin; this.key = message.key; + this.parsePapiPlaceholders = message.parsePapiPlaceholders; this.splitterPlaceholder = message.splitterPlaceholder; this.replacements.addAll(message.replacements); } @@ -132,7 +161,8 @@ public BaseComponent[] getComponents(@Nullable AnnoyingSender sender) { String string = messages.getString(key); if (string == null) return json.append(key, "&cCheck &4" + plugin.options.messagesFileName + "&c!").build(); for (final Replacement replacement : replacements) string = replacement.process(string); - final String[] split = plugin.parsePapiPlaceholders(player, string).split(splitterJson, 3); + if (parsePapiPlaceholders) string = plugin.parsePapiPlaceholders(player, string); + final String[] split = string.split(splitterJson, 3); return json.append(split[0], extractHover(split), ClickEvent.Action.SUGGEST_COMMAND, extractFunction(split)).build(); } @@ -144,9 +174,10 @@ public BaseComponent[] getComponents(@Nullable AnnoyingSender sender) { continue; } for (final Replacement replacement : replacements) subMessage = replacement.process(subMessage); + if (parsePapiPlaceholders) subMessage = plugin.parsePapiPlaceholders(player, subMessage); // Get component parts - final String[] split = plugin.parsePapiPlaceholders(player, subMessage).split(splitterJson, 3); + final String[] split = subMessage.split(splitterJson, 3); final String display = split[0]; final String hover = extractHover(split); final String function = extractFunction(split);