Skip to content

Commit

Permalink
fix: add back old methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lulu2002 committed Aug 18, 2024
1 parent 189bc3d commit d49d018
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -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<ChatColor, String> 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(">");
Expand Down Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,24 @@ public class LegacyAdventureUtilTest {
@Test
public void shouldParseLegacyChatColor() {
assertEquals(
LegacyAdventureUtil.deserialize("&6Hello"),
LegacyAdventureUtil.decode("&6Hello"),
Component.text("Hello").color(NamedTextColor.GOLD)
);
}

@Test
public void shouldParseHexColor() {
assertEquals(
LegacyAdventureUtil.deserialize("&#ff0000Hello"),
LegacyAdventureUtil.decode("&#ff0000Hello"),
Component.text("Hello").color(TextColor.color(255, 0, 0))
);
}

@Test
public void complexTestcases() {
assertEquals(
LegacyAdventureUtil.deserialize("&7[&r&#b92b27&lW&#aa235a&lO&#9a1b8d&lR&#8b13c0&lK&7]"),
LegacyAdventureUtil.decode("&7[&r&#b92b27&lW&#aa235a&lO&#9a1b8d&lR&#8b13c0&lK&7]"),
MiniMessage.miniMessage().deserialize("<gray>[<reset><#b92b27><bold>W<#aa235a><bold>O<#9a1b8d><bold>R<#8b13c0><bold>K<gray>]")
);
}
}

}

0 comments on commit d49d018

Please sign in to comment.