fix: 保留 MiniMessage 十六进制颜色标签
This commit is contained in:
@@ -12,9 +12,13 @@ import java.util.stream.Collectors;
|
||||
|
||||
public class ColorUtil {
|
||||
|
||||
private static final Pattern HEX_PATTERN = Pattern.compile("#[a-fA-F0-9]{6}");
|
||||
private static final Pattern LEGACY_HEX_PATTERN = Pattern.compile("(?<!<)#[a-fA-F0-9]{6}");
|
||||
private static final MiniMessage MINI_MESSAGE = MiniMessage.miniMessage();
|
||||
private static final LegacyComponentSerializer LEGACY_SERIALIZER = LegacyComponentSerializer.legacySection();
|
||||
private static final LegacyComponentSerializer LEGACY_SERIALIZER = LegacyComponentSerializer.builder()
|
||||
.character('§')
|
||||
.hexColors()
|
||||
.useUnusualXRepeatedCharacterHexFormat()
|
||||
.build();
|
||||
|
||||
public static String color(String text) {
|
||||
if (text == null) {
|
||||
@@ -35,7 +39,7 @@ public class ColorUtil {
|
||||
}
|
||||
|
||||
private static String translateHexColors(String text) {
|
||||
Matcher matcher = HEX_PATTERN.matcher(text);
|
||||
Matcher matcher = LEGACY_HEX_PATTERN.matcher(text);
|
||||
while (matcher.find()) {
|
||||
String hexCode = text.substring(matcher.start(), matcher.end());
|
||||
String replaceSharp = hexCode.replace('#', 'x');
|
||||
@@ -45,7 +49,7 @@ public class ColorUtil {
|
||||
builder.append("&").append(c);
|
||||
}
|
||||
text = text.replace(hexCode, builder.toString());
|
||||
matcher = HEX_PATTERN.matcher(text);
|
||||
matcher = LEGACY_HEX_PATTERN.matcher(text);
|
||||
}
|
||||
|
||||
return text;
|
||||
|
||||
@@ -28,6 +28,14 @@ class ColorUtilTest {
|
||||
assertEquals("§l加粗", ColorUtil.color("<bold>加粗</bold>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void colorSupportsPureMiniMessageHexTags() {
|
||||
assertEquals(
|
||||
"§fꑟ §x§f§4§9§e§0§b已接取任务 §x§f§1§f§c§d§e资源采集",
|
||||
ColorUtil.color("<!i><white>ꑟ <#f49e0b>已接取任务 <#f1fcde>资源采集")
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
void colorSupportsMixedMiniMessageAndLegacyFormatting() {
|
||||
assertEquals("§a领取§r §c奖励", ColorUtil.color("<green>领取</green> &c奖励"));
|
||||
|
||||
Reference in New Issue
Block a user