Adds option for only converting hex color codes

This commit is contained in:
Kristian Knarvik 2022-11-06 16:23:52 +01:00
parent 26eedac108
commit de546cac36
3 changed files with 14 additions and 5 deletions

View File

@ -18,6 +18,11 @@ public enum ColorConversion {
/** /**
* Ampersand color codes, and hexadecimal color codes are converted into colors * Ampersand color codes, and hexadecimal color codes are converted into colors
*/ */
RGB RGB,
/**
* Only hexadecimal color codes are converted into colors, not &[a-f0-9] color codes.
*/
RGB_ONLY,
} }

View File

@ -61,7 +61,8 @@ public final class ColorHelper {
return switch (colorConversion) { return switch (colorConversion) {
case NONE -> message; case NONE -> message;
case NORMAL -> ChatColor.translateAlternateColorCodes('&', message); case NORMAL -> ChatColor.translateAlternateColorCodes('&', message);
case RGB -> translateAllColorCodes(message); case RGB -> translateAllColorCodes(message, false);
case RGB_ONLY -> translateAllColorCodes(message, true);
}; };
} }
@ -69,10 +70,13 @@ public final class ColorHelper {
* Translates all found color codes to formatting in a string * Translates all found color codes to formatting in a string
* *
* @param message <p>The string to search for color codes</p> * @param message <p>The string to search for color codes</p>
* @param onlyRGB <p>Whether to only convert RGB (hexadecimal) color codes. If false, &[a-f0-9] will be converted as well.</p>
* @return <p>The message with color codes translated</p> * @return <p>The message with color codes translated</p>
*/ */
private static String translateAllColorCodes(String message) { private static String translateAllColorCodes(String message, boolean onlyRGB) {
message = ChatColor.translateAlternateColorCodes('&', message); if (!onlyRGB) {
message = ChatColor.translateAlternateColorCodes('&', message);
}
Pattern pattern; Pattern pattern;
if (requireAmpersandInHexColors) { if (requireAmpersandInHexColors) {
pattern = Pattern.compile("(&#[a-fA-F0-9]{6})"); pattern = Pattern.compile("(&#[a-fA-F0-9]{6})");