Skip to content

Commit

Permalink
Allow inserting components directly into the message
Browse files Browse the repository at this point in the history
  • Loading branch information
esotericenderman committed Aug 4, 2024
1 parent 3981ef6 commit c7e978e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Integer> 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);
}
Expand Down Expand Up @@ -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<? super Builder>) consumer);
} catch (ClassCastException exception) {}
}

return Component.text(object.toString());
}
}
4 changes: 2 additions & 2 deletions src/main/resources/languages/English (United Kingdom).yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
SET_LANGUAGE_SUCCESSFULLY: "<green>Successfully set your language to </green>%s<green>!</green>"
UNKNOWN_LANGUAGE: "Unsupported or unknown language: '%s'."
SET_LANGUAGE_SUCCESSFULLY: "<green>Successfully set your language to </green>{1}<green>!</green>"
UNKNOWN_LANGUAGE: "Unsupported or unknown language: '{0}'."
4 changes: 2 additions & 2 deletions src/main/resources/languages/English (United States).yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
SET_LANGUAGE_SUCCESSFULLY: "<green>Successfully set your language to </green>%s<green>!</green>"
UNKNOWN_LANGUAGE: "Unsupported or unknown language: '%s'."
SET_LANGUAGE_SUCCESSFULLY: "<green>Successfully set your language to </green>{1}<green>!</green>"
UNKNOWN_LANGUAGE: "Unsupported or unknown language: '{0}'."

0 comments on commit c7e978e

Please sign in to comment.