diff --git a/framework/platforms/bukkit-platform/src/main/java/io/fairyproject/bukkit/util/LegacyAdventureUtil.java b/framework/platforms/bukkit-platform/src/main/java/io/fairyproject/bukkit/util/LegacyAdventureUtil.java index 13e4e3b8..5a0da094 100644 --- a/framework/platforms/bukkit-platform/src/main/java/io/fairyproject/bukkit/util/LegacyAdventureUtil.java +++ b/framework/platforms/bukkit-platform/src/main/java/io/fairyproject/bukkit/util/LegacyAdventureUtil.java @@ -12,6 +12,8 @@ import java.util.HashMap; import java.util.Locale; import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; @UtilityClass public class LegacyAdventureUtil { @@ -67,19 +69,34 @@ public Component decode(String legacyText, TagResolver tagResolver) { } public String fromLegacy(String text, char code) { - if (text == null) - return ""; StringBuilder stringBuilder = new StringBuilder(); + int lastIndex = 0; + + Matcher matcher = getHexPattern(code).matcher(text); + while (matcher.find()) { + stringBuilder.append(text, lastIndex, matcher.start()); + stringBuilder.append("<#").append(matcher.group(1)).append(">"); + lastIndex = matcher.end(); + } + + if (lastIndex < text.length()) { + stringBuilder.append(text.substring(lastIndex)); + } + + text = stringBuilder.toString(); + stringBuilder.setLength(0); + lastIndex = 0; + char[] b = text.toCharArray(); + for (int i = 0; i < b.length - 1; ++i) { + if ((b[i] == 167 || b[i] == code) && "0123456789AaBbCcDdEeFfKkLlMmNnOoRrXx".indexOf(b[i + 1]) > -1) { - int lastIndex = 0; - for(int i = 0; i < b.length - 1; ++i) { - if ((b[i] == 'ยง' || b[i] == code) && "0123456789AaBbCcDdEeFfKkLlMmNnOoRrXx".indexOf(b[i + 1]) > -1) { - if (i > 0) + if (i > 0) { stringBuilder.append(text, lastIndex, i); - final ChatColor chatColor = ChatColor.getByChar(b[i + 1]); - final String s = INDEX.get(chatColor); + } + ChatColor chatColor = ChatColor.getByChar(b[i + 1]); + String s = INDEX.get(chatColor); stringBuilder.append("<").append(s).append(">"); lastIndex = i + 2; } @@ -88,7 +105,12 @@ public String fromLegacy(String text, char code) { if (lastIndex < text.length()) { stringBuilder.append(text.substring(lastIndex)); } + return stringBuilder.toString(); } + private Pattern getHexPattern(char code) { + return Pattern.compile(code + "#([A-Fa-f0-9]{6})"); + } + } diff --git a/framework/platforms/bukkit-platform/src/test/java/io/fairyproject/bukkit/util/LegacyAdventureUtilTest.java b/framework/platforms/bukkit-platform/src/test/java/io/fairyproject/bukkit/util/LegacyAdventureUtilTest.java new file mode 100644 index 00000000..09e6ae4e --- /dev/null +++ b/framework/platforms/bukkit-platform/src/test/java/io/fairyproject/bukkit/util/LegacyAdventureUtilTest.java @@ -0,0 +1,36 @@ +package io.fairyproject.bukkit.util; + +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.format.TextColor; +import net.kyori.adventure.text.minimessage.MiniMessage; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class LegacyAdventureUtilTest { + + @Test + public void shouldParseLegacyChatColor() { + assertEquals( + LegacyAdventureUtil.decode("&6Hello"), + Component.text("Hello").color(NamedTextColor.GOLD) + ); + } + + @Test + public void shouldParseHexColor() { + assertEquals( + LegacyAdventureUtil.decode("&#ff0000Hello"), + Component.text("Hello").color(TextColor.color(255, 0, 0)) + ); + } + + @Test + public void complexTestcases() { + assertEquals( + LegacyAdventureUtil.decode("&7[&r&#b92b27&lW&#aa235a&lO a1b8d&lRb13c0&lK&7]"), + MiniMessage.miniMessage().deserialize("[<#b92b27>W<#aa235a>O<#9a1b8d>R<#8b13c0>K]") + ); + } +} \ No newline at end of file