Skip to content

Commit

Permalink
feat: make LegacyAdventureUtil supports hex color format (ex: &#ffffff)
Browse files Browse the repository at this point in the history
  • Loading branch information
lulu2002 committed Aug 18, 2024
1 parent 24e8fbe commit 189bc3d
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,68 @@
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;

@UtilityClass
public class LegacyAdventureUtil {

public final 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, '&'));
}

public static String fromLegacy(String text, char code) {
StringBuilder stringBuilder = new StringBuilder();
int lastIndex = 0;

Matcher matcher = HEX_PATTERN.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) {

if (i > 0) {
stringBuilder.append(text, lastIndex, i);
}

ChatColor chatColor = ChatColor.getByChar(b[i + 1]);
String s = INDEX.get(chatColor);
stringBuilder.append("<").append(s).append(">");
lastIndex = i + 2;
}
}

if (lastIndex < text.length()) {
stringBuilder.append(text.substring(lastIndex));
}

return stringBuilder.toString();
}

private LegacyAdventureUtil() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}

static {
INDEX.put(ChatColor.BLACK, NamedTextColor.BLACK.toString());
Expand All @@ -42,53 +88,4 @@ public class LegacyAdventureUtil {
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 String fromLegacy(String text, char code) {
if (text == null)
return "";
StringBuilder stringBuilder = new StringBuilder();
char[] b = text.toCharArray();

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)
stringBuilder.append(text, lastIndex, i);
final ChatColor chatColor = ChatColor.getByChar(b[i + 1]);
final String s = INDEX.get(chatColor);

stringBuilder.append("<").append(s).append(">");
lastIndex = i + 2;
}
}

if (lastIndex < text.length()) {
stringBuilder.append(text.substring(lastIndex));
}
return stringBuilder.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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.deserialize("&6Hello"),
Component.text("Hello").color(NamedTextColor.GOLD)
);
}

@Test
public void shouldParseHexColor() {
assertEquals(
LegacyAdventureUtil.deserialize("&#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]"),
MiniMessage.miniMessage().deserialize("<gray>[<reset><#b92b27><bold>W<#aa235a><bold>O<#9a1b8d><bold>R<#8b13c0><bold>K<gray>]")
);
}
}

0 comments on commit 189bc3d

Please sign in to comment.