Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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})");
}

}
Original file line number Diff line number Diff line change
@@ -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&#9a1b8d&lR&#8b13c0&lK&7]"),
MiniMessage.miniMessage().deserialize("<gray>[<reset><#b92b27><bold>W<#aa235a><bold>O<#9a1b8d><bold>R<#8b13c0><bold>K<gray>]")
);
}
}