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 5948424e..08358294 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 @@ -1,29 +1,78 @@ package io.fairyproject.bukkit.util; +import io.fairyproject.mc.MCAdventure; +import lombok.experimental.UtilityClass; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import org.bukkit.ChatColor; import java.util.HashMap; +import java.util.Locale; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; -public final class LegacyAdventureUtil { +@UtilityClass +public class LegacyAdventureUtil { + private static final Map INDEX = new HashMap<>(); - private static final Pattern HEX_PATTERN = Pattern.compile("&#([A-Fa-f0-9]{6})"); - public static Component deserialize(String text) { - return MiniMessage.miniMessage().deserialize(fromLegacy(text, '&')); + static { + INDEX.put(ChatColor.BLACK, NamedTextColor.BLACK.toString()); + INDEX.put(ChatColor.DARK_BLUE, NamedTextColor.DARK_BLUE.toString()); + INDEX.put(ChatColor.DARK_GREEN, NamedTextColor.DARK_GREEN.toString()); + INDEX.put(ChatColor.DARK_AQUA, NamedTextColor.DARK_AQUA.toString()); + INDEX.put(ChatColor.DARK_RED, NamedTextColor.DARK_RED.toString()); + INDEX.put(ChatColor.DARK_PURPLE, NamedTextColor.DARK_PURPLE.toString()); + INDEX.put(ChatColor.GOLD, NamedTextColor.GOLD.toString()); + INDEX.put(ChatColor.GRAY, NamedTextColor.GRAY.toString()); + INDEX.put(ChatColor.DARK_GRAY, NamedTextColor.DARK_GRAY.toString()); + INDEX.put(ChatColor.BLUE, NamedTextColor.BLUE.toString()); + INDEX.put(ChatColor.GREEN, NamedTextColor.GREEN.toString()); + INDEX.put(ChatColor.AQUA, NamedTextColor.AQUA.toString()); + INDEX.put(ChatColor.RED, NamedTextColor.RED.toString()); + INDEX.put(ChatColor.LIGHT_PURPLE, NamedTextColor.LIGHT_PURPLE.toString()); + INDEX.put(ChatColor.YELLOW, NamedTextColor.YELLOW.toString()); + INDEX.put(ChatColor.WHITE, NamedTextColor.WHITE.toString()); + INDEX.put(ChatColor.MAGIC, TextDecoration.OBFUSCATED.toString()); + INDEX.put(ChatColor.BOLD, TextDecoration.BOLD.toString()); + INDEX.put(ChatColor.STRIKETHROUGH, TextDecoration.STRIKETHROUGH.toString()); + INDEX.put(ChatColor.UNDERLINE, TextDecoration.UNDERLINED.toString()); + INDEX.put(ChatColor.ITALIC, TextDecoration.ITALIC.toString()); + INDEX.put(ChatColor.RESET, "reset"); + } + + public String decodeAndLegacy(String legacyText) { + return decodeAndLegacy(legacyText, TagResolver.empty()); + } + + public String decodeAndLegacy(String legacyText, TagResolver tagResolver) { + if (legacyText == null || legacyText.isEmpty()) + return ""; + return MCAdventure.asLegacyString( + decode(legacyText, tagResolver), + Locale.ENGLISH + ); + } + + public Component decode(String legacyText) { + return decode(legacyText, TagResolver.empty()); + } + + public Component decode(String legacyText, TagResolver tagResolver) { + if (legacyText == null) + return Component.empty(); + return MiniMessage.miniMessage().deserialize(fromLegacy(legacyText, '&'), tagResolver); } public static String fromLegacy(String text, char code) { StringBuilder stringBuilder = new StringBuilder(); int lastIndex = 0; - Matcher matcher = HEX_PATTERN.matcher(text); + Matcher matcher = getHexPattern(code).matcher(text); while (matcher.find()) { stringBuilder.append(text, lastIndex, matcher.start()); stringBuilder.append("<#").append(matcher.group(1)).append(">"); @@ -60,32 +109,8 @@ public static String fromLegacy(String text, char code) { return stringBuilder.toString(); } - private LegacyAdventureUtil() { - throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); + private Pattern getHexPattern(char code) { + return Pattern.compile(code + "#([A-Fa-f0-9]{6})"); } - static { - INDEX.put(ChatColor.BLACK, NamedTextColor.BLACK.toString()); - INDEX.put(ChatColor.DARK_BLUE, NamedTextColor.DARK_BLUE.toString()); - INDEX.put(ChatColor.DARK_GREEN, NamedTextColor.DARK_GREEN.toString()); - INDEX.put(ChatColor.DARK_AQUA, NamedTextColor.DARK_AQUA.toString()); - INDEX.put(ChatColor.DARK_RED, NamedTextColor.DARK_RED.toString()); - INDEX.put(ChatColor.DARK_PURPLE, NamedTextColor.DARK_PURPLE.toString()); - INDEX.put(ChatColor.GOLD, NamedTextColor.GOLD.toString()); - INDEX.put(ChatColor.GRAY, NamedTextColor.GRAY.toString()); - INDEX.put(ChatColor.DARK_GRAY, NamedTextColor.DARK_GRAY.toString()); - INDEX.put(ChatColor.BLUE, NamedTextColor.BLUE.toString()); - INDEX.put(ChatColor.GREEN, NamedTextColor.GREEN.toString()); - INDEX.put(ChatColor.AQUA, NamedTextColor.AQUA.toString()); - INDEX.put(ChatColor.RED, NamedTextColor.RED.toString()); - INDEX.put(ChatColor.LIGHT_PURPLE, NamedTextColor.LIGHT_PURPLE.toString()); - INDEX.put(ChatColor.YELLOW, NamedTextColor.YELLOW.toString()); - INDEX.put(ChatColor.WHITE, NamedTextColor.WHITE.toString()); - INDEX.put(ChatColor.MAGIC, TextDecoration.OBFUSCATED.toString()); - INDEX.put(ChatColor.BOLD, TextDecoration.BOLD.toString()); - INDEX.put(ChatColor.STRIKETHROUGH, TextDecoration.STRIKETHROUGH.toString()); - INDEX.put(ChatColor.UNDERLINE, TextDecoration.UNDERLINED.toString()); - INDEX.put(ChatColor.ITALIC, TextDecoration.ITALIC.toString()); - INDEX.put(ChatColor.RESET, "reset"); - } } 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 index 055d6e97..09e6ae4e 100644 --- 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 @@ -13,7 +13,7 @@ public class LegacyAdventureUtilTest { @Test public void shouldParseLegacyChatColor() { assertEquals( - LegacyAdventureUtil.deserialize("&6Hello"), + LegacyAdventureUtil.decode("&6Hello"), Component.text("Hello").color(NamedTextColor.GOLD) ); } @@ -21,7 +21,7 @@ public void shouldParseLegacyChatColor() { @Test public void shouldParseHexColor() { assertEquals( - LegacyAdventureUtil.deserialize("&#ff0000Hello"), + LegacyAdventureUtil.decode("&#ff0000Hello"), Component.text("Hello").color(TextColor.color(255, 0, 0)) ); } @@ -29,9 +29,8 @@ public void shouldParseHexColor() { @Test public void complexTestcases() { assertEquals( - LegacyAdventureUtil.deserialize("&7[&r&#b92b27&lW&#aa235a&lO a1b8d&lRb13c0&lK&7]"), + 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