mirror of
https://github.com/Winds-Studio/Leaf.git
synced 2025-12-22 00:19:20 +00:00
99 lines
3.6 KiB
Diff
99 lines
3.6 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] Purpur: ChatColor conveniences
|
|
|
|
Original license: MIT
|
|
Original project: https://github.com/PurpurMC/Purpur
|
|
|
|
diff --git a/src/main/java/org/bukkit/ChatColor.java b/src/main/java/org/bukkit/ChatColor.java
|
|
index f6eb30f53dad684f156102cf7147b2f00c82c71e..f1239a2618b08fa92e0e20692d1c3d20d1558502 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;
|
|
@@ -413,4 +414,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
|
|
}
|