Skip to content

Commit

Permalink
fix chatcolor parsing on folia
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Mar 4, 2024
1 parent d80969d commit 640e6c5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Set;
import java.util.jar.JarFile;
import java.util.logging.Logger;
import java.util.regex.Matcher;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

import java.io.File;
import java.util.List;
import java.util.Locale;

public class LanguageCache {

private final ConfigFile lang;

private final Locale locale;
public final Component no_permission, invalid_syntax, failed_argument_parse, chat_commandwhitelist_badcommand, misc_joinMessage, misc_leaveMessage,
misc_MaskedKickMessage, preventions_witherSpawningDisabledInRadius, misc_enabledConnectionMsgs, misc_disabledConnectionMsgs, elytra_disablePacketElytraFly,
elytra_global_YouAreFlyingIn, elytra_global_New, elytra_global_New_UpperCase, elytra_global_New_Color, elytra_global_Old, elytra_global_Old_UpperCase,
Expand All @@ -24,6 +25,7 @@ public class LanguageCache {
public final List<Component> misc_firstJoinMessage, help_Message;

public LanguageCache(String locale) throws Exception {
this.locale = Locale.forLanguageTag(locale.replace("_", "-"));
AnarchyExploitFixes plugin = AnarchyExploitFixes.getInstance();
File langYML = new File(plugin.getDataFolder() + File.separator + "lang", locale + ".yml");
// Check if the lang folder has already been created
Expand Down Expand Up @@ -133,28 +135,28 @@ public LanguageCache(String locale) throws Exception {

public Component getTranslation(String path, String defaultTranslation, boolean upperCase) {
lang.addDefault(path, defaultTranslation);
defaultTranslation = translateChatColor(defaultTranslation);
return MiniMessage.miniMessage().deserialize(upperCase ? lang.getString(path, defaultTranslation).toUpperCase() : lang.getString(path, defaultTranslation));
final String serialized = translateChatColor(lang.getString(path, defaultTranslation));
return MiniMessage.miniMessage().deserialize(upperCase ? serialized.toUpperCase(locale) : serialized);
}

public Component getTranslation(String path, String defaultTranslation, boolean upperCase, String comment) {
lang.addDefault(path, defaultTranslation, comment);
defaultTranslation = translateChatColor(defaultTranslation);
return MiniMessage.miniMessage().deserialize(upperCase ? lang.getString(path, defaultTranslation).toUpperCase() : lang.getString(path, defaultTranslation));
final String serialized = translateChatColor(lang.getString(path, defaultTranslation));
return MiniMessage.miniMessage().deserialize(upperCase ? serialized.toUpperCase(locale) : serialized);
}

public List<Component> getListTranslation(String path, List<String> defaultTranslation, boolean upperCase) {
lang.addDefault(path, defaultTranslation);
return lang.getStringList(path).stream()
.map(LanguageCache::translateChatColor)
.map(line -> MiniMessage.miniMessage().deserialize(upperCase ? line.toUpperCase() : line)).toList();
.map(serialized -> MiniMessage.miniMessage().deserialize(upperCase ? serialized.toUpperCase(locale) : serialized)).toList();
}

public List<Component> getListTranslation(String path, List<String> defaultTranslation, boolean upperCase, String comment) {
lang.addDefault(path, defaultTranslation, comment);
return lang.getStringList(path).stream()
.map(LanguageCache::translateChatColor)
.map(line -> MiniMessage.miniMessage().deserialize(upperCase ? line.toUpperCase() : line)).toList();
.map(serialized -> MiniMessage.miniMessage().deserialize(upperCase ? serialized.toUpperCase(locale) : serialized)).toList();
}

private static String translateChatColor(String string) {
Expand Down

0 comments on commit 640e6c5

Please sign in to comment.