Hex color support for language files

This commit is contained in:
PikaMug 2020-08-25 04:46:16 -04:00
parent 0db18eade8
commit 39a1c3edc7
2 changed files with 22 additions and 10 deletions

View File

@ -29,6 +29,8 @@ import net.citizensnpcs.api.npc.NPC;
public class ConfigUtil { public class ConfigUtil {
private static Pattern hexPattern = Pattern.compile("(?i)%#([0-9A-F]{6})%");
/** /**
* Checks whether items in a list are instances of a class<p> * Checks whether items in a list are instances of a class<p>
* *
@ -191,11 +193,11 @@ public class ConfigUtil {
parsed = parsed.replace("<br>", "\n"); parsed = parsed.replace("<br>", "\n");
parsed = ChatColor.translateAlternateColorCodes('&', parsed); parsed = ChatColor.translateAlternateColorCodes('&', parsed);
Matcher matcher = Pattern.compile("(?i)<#([0-9A-F]{6})>").matcher(parsed); final Matcher matcher = hexPattern.matcher(parsed);
while (matcher.find()) { while (matcher.find()) {
StringBuilder hex = new StringBuilder(); final StringBuilder hex = new StringBuilder();
hex.append(ChatColor.COLOR_CHAR + "x"); hex.append(ChatColor.COLOR_CHAR + "x");
char[] chars = matcher.group(1).toCharArray(); final char[] chars = matcher.group(1).toCharArray();
for (int index = 0; index < chars.length; index++) { for (int index = 0; index < chars.length; index++) {
hex.append(ChatColor.COLOR_CHAR).append(Character.toLowerCase(chars[index])); hex.append(ChatColor.COLOR_CHAR).append(Character.toLowerCase(chars[index]));
} }

View File

@ -21,6 +21,8 @@ import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
@ -36,6 +38,7 @@ public class Lang {
private static String iso = "en-US"; private static String iso = "en-US";
private static final LinkedHashMap<String, String> langMap = new LinkedHashMap<String, String>(); private static final LinkedHashMap<String, String> langMap = new LinkedHashMap<String, String>();
private static Pattern hexPattern = Pattern.compile("(?i)%#([0-9A-F]{6})%");
public static String getISO() { public static String getISO() {
return iso; return iso;
@ -252,6 +255,16 @@ public class Lang {
s = s.replace(token, tokenMap.get(token)); s = s.replace(token, tokenMap.get(token));
s = s.replace(token.toUpperCase(), tokenMap.get(token)); s = s.replace(token.toUpperCase(), tokenMap.get(token));
} }
final Matcher matcher = hexPattern.matcher(s);
while (matcher.find()) {
final StringBuilder hex = new StringBuilder();
hex.append(ChatColor.COLOR_CHAR + "x");
final char[] chars = matcher.group(1).toCharArray();
for (int index = 0; index < chars.length; index++) {
hex.append(ChatColor.COLOR_CHAR).append(Character.toLowerCase(chars[index]));
}
s = s.replace(matcher.group(), hex.toString());
}
return s; return s;
} }
@ -259,15 +272,12 @@ public class Lang {
if (tokenMap.isEmpty()) { if (tokenMap.isEmpty()) {
LangToken.init(); LangToken.init();
} }
for (final String token : tokenMap.keySet()) { s = convertString(s);
s = s.replace(token, tokenMap.get(token));
s = s.replace(token.toUpperCase(), tokenMap.get(token));
if (Bukkit.getServer().getPluginManager().getPlugin("PlaceholderAPI") != null ) { if (Bukkit.getServer().getPluginManager().getPlugin("PlaceholderAPI") != null ) {
if (!Bukkit.getServer().getPluginManager().getPlugin("PlaceholderAPI").isEnabled()) { if (!Bukkit.getServer().getPluginManager().getPlugin("PlaceholderAPI").isEnabled()) {
s = PlaceholderAPI.setPlaceholders(p, s); s = PlaceholderAPI.setPlaceholders(p, s);
} }
} }
}
return s; return s;
} }
} }