mirror of
https://github.com/PurpurMC/Purpur.git
synced 2025-02-17 13:00:04 +08:00
97 lines
3.5 KiB
Diff
97 lines
3.5 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: William Blake Galbreath <Blake.Galbreath@GMail.com>
|
|
Date: Fri, 10 Jul 2020 12:43:25 -0500
|
|
Subject: [PATCH] ChatColor conveniences
|
|
|
|
|
|
diff --git a/src/main/java/org/bukkit/ChatColor.java b/src/main/java/org/bukkit/ChatColor.java
|
|
index 918a045165cdcde264bc24082b7afebb407271de..687d11619379aead7f665d4a5f8f8bcc857cb8e9 100644
|
|
--- a/src/main/java/org/bukkit/ChatColor.java
|
|
+++ b/src/main/java/org/bukkit/ChatColor.java
|
|
@@ -3,6 +3,7 @@ package org.bukkit;
|
|
import com.google.common.base.Preconditions;
|
|
import com.google.common.collect.Maps;
|
|
import java.util.Map;
|
|
+import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
import org.jetbrains.annotations.Contract;
|
|
import org.jetbrains.annotations.NotNull;
|
|
@@ -456,4 +457,77 @@ public enum ChatColor {
|
|
BY_CHAR.put(color.code, color);
|
|
}
|
|
}
|
|
+
|
|
+ // Purpur start
|
|
+ /**
|
|
+ * Convert legacy string into a string ready to be parsed by MiniMessage
|
|
+ *
|
|
+ * @param str Legacy string
|
|
+ * @return MiniMessage ready string
|
|
+ */
|
|
+ @NotNull
|
|
+ public static String toMM(@NotNull String str) {
|
|
+ StringBuilder sb = new StringBuilder(str);
|
|
+ Matcher m = STRIP_COLOR_PATTERN.matcher(sb);
|
|
+ while (m.find()) {
|
|
+ sb.replace(m.start(), m.end(), sb.substring(m.start(), m.end()).toLowerCase());
|
|
+ }
|
|
+ return sb.toString()
|
|
+ .replace("&0", "<black>")
|
|
+ .replace("&1", "<dark_blue>")
|
|
+ .replace("&2", "<dark_green>")
|
|
+ .replace("&3", "<dark_aqua>")
|
|
+ .replace("&4", "<dark_red>")
|
|
+ .replace("&5", "<dark_purple>")
|
|
+ .replace("&6", "<gold>")
|
|
+ .replace("&7", "<grey>")
|
|
+ .replace("&8", "<dark_grey>")
|
|
+ .replace("&9", "<blue>")
|
|
+ .replace("&a", "<green>")
|
|
+ .replace("&b", "<aqua>")
|
|
+ .replace("&c", "<red>")
|
|
+ .replace("&d", "<light_purple>")
|
|
+ .replace("&e", "<yellow>")
|
|
+ .replace("&f", "<white>")
|
|
+ .replace("&k", "<obf>")
|
|
+ .replace("&l", "<b>")
|
|
+ .replace("&m", "<st>")
|
|
+ .replace("&n", "<u>")
|
|
+ .replace("&o", "<i>")
|
|
+ .replace("&r", "<r>");
|
|
+ }
|
|
+
|
|
+ @NotNull
|
|
+ public static net.kyori.adventure.text.Component parseMM(@NotNull String string, @Nullable Object... args) {
|
|
+ return net.kyori.adventure.text.minimessage.MiniMessage.miniMessage().deserialize(String.format(string, args));
|
|
+ }
|
|
+
|
|
+ @Deprecated
|
|
+ public static final Pattern HEX_PATTERN = Pattern.compile("(#[A-Fa-f0-9]{6})");
|
|
+
|
|
+ @Deprecated
|
|
+ @Nullable
|
|
+ public static String replaceHex(@Nullable String str) {
|
|
+ if (str != null) {
|
|
+ java.util.regex.Matcher matcher = HEX_PATTERN.matcher(str);
|
|
+ while (matcher.find()) {
|
|
+ String group = matcher.group(1);
|
|
+ str = str.replace(group, net.md_5.bungee.api.ChatColor.of(group).toString());
|
|
+ }
|
|
+ }
|
|
+ return str;
|
|
+ }
|
|
+
|
|
+ @Deprecated
|
|
+ @Nullable
|
|
+ public static String color(@Nullable String str) {
|
|
+ return color(str, true);
|
|
+ }
|
|
+
|
|
+ @Deprecated
|
|
+ @Nullable
|
|
+ public static String color(@Nullable String str, boolean parseHex) {
|
|
+ return str != null ? net.md_5.bungee.api.ChatColor.translateAlternateColorCodes('&', parseHex ? replaceHex(str) : str) : str;
|
|
+ }
|
|
+ // Purpur end
|
|
}
|