From c7e978e65d39e3d6629febfef91035fcf35e2ad3 Mon Sep 17 00:00:00 2001 From: Slqmy <90862990+Slqmy@users.noreply.github.com> Date: Sun, 4 Aug 2024 20:42:22 +0100 Subject: [PATCH] Allow inserting components directly into the message --- .../language/LanguageManager.java | 89 +++++++++++++++++-- .../languages/English (United Kingdom).yaml | 4 +- .../languages/English (United States).yaml | 4 +- 3 files changed, 85 insertions(+), 12 deletions(-) diff --git a/src/main/java/net/slqmy/template_paper_plugin/language/LanguageManager.java b/src/main/java/net/slqmy/template_paper_plugin/language/LanguageManager.java index 29a6f11b..3b0be7f5 100644 --- a/src/main/java/net/slqmy/template_paper_plugin/language/LanguageManager.java +++ b/src/main/java/net/slqmy/template_paper_plugin/language/LanguageManager.java @@ -1,14 +1,20 @@ package net.slqmy.template_paper_plugin.language; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent.Builder; import net.kyori.adventure.text.minimessage.MiniMessage; import net.slqmy.template_paper_plugin.TemplatePaperPlugin; import net.slqmy.template_paper_plugin.data.player.PlayerProfile; import net.slqmy.template_paper_plugin.util.FileUtil; import java.util.UUID; +import java.util.function.Consumer; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.stream.Stream; import java.util.Map; import java.util.Set; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -25,6 +31,8 @@ public class LanguageManager { private final TemplatePaperPlugin plugin; + private final Pattern placeholderPattern = Pattern.compile("\\{(\\d+)}"); + private final String languagesFolderName = "languages"; private final String languagesFolderPath; private final File languagesFolder; @@ -134,42 +142,89 @@ public String getRawMessageString(Message message, String language) { return getRawMessageString(message, language, true); } - public String getFormattedMessageString(Message message, String language, boolean fallbackOnDefaultLanguage, Object... arguments) { - return String.format(getRawMessageString(message, language, fallbackOnDefaultLanguage), arguments); + public Component getMessage(Message message, String language, boolean fallbackOnDefaultLanguage, Component... arguments) { + String miniMessageString = getRawMessageString(message, language, fallbackOnDefaultLanguage); + + Matcher matcher = placeholderPattern.matcher(miniMessageString); + + String[] parts = placeholderPattern.split(miniMessageString); + List argumentIndexes = new ArrayList<>(); + + while (matcher.find()) { + argumentIndexes.add(Integer.parseUnsignedInt(matcher.group(1))); + } + + Component output = miniMessage.deserialize(parts[0]); + + for (int i = 1; i < parts.length; i++) { + int argumentIndex = argumentIndexes.get(i - 1); + output.append(arguments[argumentIndex]); + + String part = parts[i]; + Component component = miniMessage.deserialize(part); + + output.append(component); + } + + return output; } - public String getFormattedMessageString(Message message, String language, Object... arguments) { - return getFormattedMessageString(message, language, true, arguments); + public Component getMessage(Message message, String language, Component... arguments) { + return getMessage(message, language, true, arguments); } public Component getMessage(Message message, String language, boolean fallbackOnDefaultLanguage, Object... arguments) { - return miniMessage.deserialize(getFormattedMessageString(message, language, fallbackOnDefaultLanguage, arguments)); + return getMessage(message, language, fallbackOnDefaultLanguage, toComponents(arguments)); } public Component getMessage(Message message, String language, Object... arguments) { return getMessage(message, language, true, arguments); } - public Component getMessage(Message message, CommandSender commandSender, boolean fallbackOnDefaultLanguage, Object... arguments) { + public Component getMessage(Message message, CommandSender commandSender, boolean fallbackOnDefaultLanguage, Component... arguments) { return getMessage(message, getLanguage(commandSender), fallbackOnDefaultLanguage, arguments); } + public Component getMessage(Message message, CommandSender commandSender, Component... arguments) { + return getMessage(message, commandSender, true, arguments); + } + + public Component getMessage(Message message, CommandSender commandSender, boolean fallbackOnDefaultLanguage, Object... arguments) { + return getMessage(message, commandSender, fallbackOnDefaultLanguage, toComponents(arguments)); + } + public Component getMessage(Message message, CommandSender commandSender, Object... arguments) { return getMessage(message, commandSender, true, arguments); } - public Component getMessage(Message message, UUID uuid, boolean fallbackOnDefaultLanguage, Object... arguments) { + public Component getMessage(Message message, UUID uuid, boolean fallbackOnDefaultLanguage, Component... arguments) { return getMessage(message, getLanguage(uuid), fallbackOnDefaultLanguage, arguments); } + public Component getMessage(Message message, UUID uuid, Component... arguments) { + return getMessage(message, uuid, true, arguments); + } + + public Component getMessage(Message message, UUID uuid, boolean fallbackOnDefaultLanguage, Object... arguments) { + return getMessage(message, uuid, fallbackOnDefaultLanguage, toComponents(arguments)); + } + public Component getMessage(Message message, UUID uuid, Object... arguments) { return getMessage(message, uuid, true, arguments); } - public Component getMessage(Message message, PlayerProfile playerProfile, boolean fallbackOnDefaultLanguage, Object... arguments) { + public Component getMessage(Message message, PlayerProfile playerProfile, boolean fallbackOnDefaultLanguage, Component... arguments) { return getMessage(message, getLanguage(playerProfile), fallbackOnDefaultLanguage, arguments); } + public Component getMessage(Message message, PlayerProfile playerProfile, Component... arguments) { + return getMessage(message, playerProfile, true, arguments); + } + + public Component getMessage(Message message, PlayerProfile playerProfile, boolean fallbackOnDefaultLanguage, Object... arguments) { + return getMessage(message, playerProfile, fallbackOnDefaultLanguage, toComponents(arguments)); + } + public Component getMessage(Message message, PlayerProfile playerProfile, Object... arguments) { return getMessage(message, playerProfile, true, arguments); } @@ -219,4 +274,22 @@ private String getProfileLanguage(CommandSender commandSender) { return defaultLanguage; } } + + public Component[] toComponents(Object[] ...objects) { + return Stream.of(objects).map((object) -> toComponent(object)).toArray(Component[]::new); + } + + public Component toComponent(Object object) { + if (object instanceof Component component) { + return component; + } + + if (object instanceof Consumer consumer) { + try { + return Component.text((Consumer) consumer); + } catch (ClassCastException exception) {} + } + + return Component.text(object.toString()); + } } diff --git a/src/main/resources/languages/English (United Kingdom).yaml b/src/main/resources/languages/English (United Kingdom).yaml index d74d1725..85e08b13 100644 --- a/src/main/resources/languages/English (United Kingdom).yaml +++ b/src/main/resources/languages/English (United Kingdom).yaml @@ -1,2 +1,2 @@ -SET_LANGUAGE_SUCCESSFULLY: "Successfully set your language to %s!" -UNKNOWN_LANGUAGE: "Unsupported or unknown language: '%s'." +SET_LANGUAGE_SUCCESSFULLY: "Successfully set your language to {1}!" +UNKNOWN_LANGUAGE: "Unsupported or unknown language: '{0}'." diff --git a/src/main/resources/languages/English (United States).yaml b/src/main/resources/languages/English (United States).yaml index d74d1725..85e08b13 100644 --- a/src/main/resources/languages/English (United States).yaml +++ b/src/main/resources/languages/English (United States).yaml @@ -1,2 +1,2 @@ -SET_LANGUAGE_SUCCESSFULLY: "Successfully set your language to %s!" -UNKNOWN_LANGUAGE: "Unsupported or unknown language: '%s'." +SET_LANGUAGE_SUCCESSFULLY: "Successfully set your language to {1}!" +UNKNOWN_LANGUAGE: "Unsupported or unknown language: '{0}'."