Răsfoiți Sursa

add support for RGB color codes (format: &#RRGGBB)

closes #106
mastercake10 1 an în urmă
părinte
comite
648f6ec6c8

+ 3 - 0
Plugin/src/main/resources/config.yml

@@ -1,3 +1,5 @@
+# Config generated for TimeIsMoney ${project.version}
+
 configuration-version: 12
 
 debug-log: false
@@ -60,6 +62,7 @@ payouts:
     permission: tim.vip
 
 # Translations
+# Both legacy color codes, &c etc. and hex RGB color codes, #&FF00FF, are supported.
 message: "&aYou earned &c%money% &afor 10 minutes online time!"
 message_payoutlimit_reached: "&cYou have reached the payout limit today. You earned 0$"
 message_afk: "&cYou havn't earned money because you were afk!"

+ 23 - 1
Tools/src/main/java/de/Linus122/TimeIsMoney/tools/Utils.java

@@ -4,6 +4,9 @@ import org.bukkit.Bukkit;
 import org.bukkit.ChatColor;
 import org.bukkit.entity.Player;
 
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
 /**
  * Utility class.
  *
@@ -29,7 +32,7 @@ public class Utils {
 		if(s == null) {
 			return "";
 		}
-		return ChatColor.translateAlternateColorCodes('&', s);
+		return ChatColor.translateAlternateColorCodes('&', parseRGB(s));
 	}
 	
 	public static String applyPlaceholders(Player player, String s) {
@@ -38,4 +41,23 @@ public class Utils {
         }
         return s;
 	}
+
+	private static final Pattern rgbColor = Pattern.compile("(?<!\\\\)(&#[a-fA-F0-9]{6})");
+
+	public static String parseRGB(String msg) {
+		Matcher matcher = rgbColor.matcher(msg);
+		while (matcher.find()) {
+			String color = msg.substring(matcher.start(), matcher.end());
+			String hex = color.replace("&", "");
+			try {
+				msg = msg.replace(color, String.valueOf(net.md_5.bungee.api.ChatColor.of(hex)));
+			} catch(NoSuchMethodError e) {
+				// older spigot versions
+
+				msg = msg.replace(color, "");
+			}
+			matcher = rgbColor.matcher(msg);
+		}
+		return msg;
+	}
 }