Skip to content

Commit

Permalink
feat: support 1.21.4+ shadowColor in legacy parser
Browse files Browse the repository at this point in the history
See #470
  • Loading branch information
diogotcorreia committed Jan 23, 2025
1 parent ad4fa94 commit a9c665e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import net.kyori.adventure.text.flattener.ComponentFlattener;
import net.kyori.adventure.text.flattener.FlattenerListener;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.ShadowColor;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
Expand Down Expand Up @@ -67,6 +68,7 @@ public class LegacyParser implements MessageParser {
private static final char SECTION_CHAR = '§';
private static final char HEX_PREFIX = '#';
private static final char HEX_CODE = 'x';
private static final char SHADOW_COLOR_CODE = 's';
private static final String VALID_COLOR_CODES = "0123456789AaBbCcDdEeFfKkLlMmNnOoRrXx";
private static final Pattern FORMATTING_STRIP_PATTERN = Pattern.compile(
CLICK_DELIM + "\\d[\\w-]{36}|" + HOVER_DELIM + "[\\w-]{36}|" + CLICK_END_DELIM + "|" + HOVER_END_DELIM
Expand Down Expand Up @@ -361,6 +363,13 @@ public SerializedComponent(String legacyText) {
val color = text.substring(i + 1, i + 13);
format = TextColor.fromHexString(HEX_PREFIX + color.replace(String.valueOf(SECTION_CHAR), ""));
i += 12;
} else if (lowercaseChar == SHADOW_COLOR_CODE && i + 9 < text.length()) {
@Subst("#ffffffff") val color = text.substring(i + 1, i + 10);
val shadowColor = ShadowColor.fromHexString(color);
currentStyle = currentStyle.shadowColor(shadowColor);
componentBuilder.style(currentStyle);
i += 9;
continue;
} else {
format = CharacterAndFormat.defaults().stream()
.filter(characterAndFormat -> characterAndFormat.character() == lowercaseChar)
Expand Down Expand Up @@ -552,6 +561,13 @@ public void pushStyle(@NotNull Style style) {
this.stringBuilder.append(formatToString(color));
}

@Nullable val shadowColor = style.shadowColor();
if (shadowColor != null) {
this.stringBuilder.append(SECTION_CHAR).append('s');
// format: #RRGGBBAA
this.stringBuilder.append(shadowColor.asHexString());
}

style.decorations().entrySet().stream()
.filter(entry -> entry.getValue() == TextDecoration.State.TRUE)
.forEach(entry -> this.stringBuilder.append(formatToString(entry.getKey())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.ShadowColor;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -56,6 +57,7 @@ public class LegacyParserTest {
Component.text("ipsum dolor ")
.color(NamedTextColor.BLACK)
.decorate(TextDecoration.BOLD)
.shadowColor(ShadowColor.shadowColor(0xdd, 0xee, 0xff, 0x44))
)
.append(
Component.text("sit amet,")
Expand All @@ -74,6 +76,10 @@ public class LegacyParserTest {
.hoverEvent(HoverEvent.showText(Component.text("hello world")))
)
)
.append(
Component.text("Aenean tempor urna ac consequat sodales. ")
.shadowColor(ShadowColor.shadowColor(0xaa, 0xbb, 0xcc, 0x88))
)
.append(
Component.text("Maecenas imperdiet ")
.color(NamedTextColor.AQUA)
Expand All @@ -93,11 +99,11 @@ public void testSerializingComponent() {
assertEquals(1, serializedComponent.getHoverEvents().size());
assertEquals(1, serializedComponent.getTranslatableComponents().size());
assertEquals(
"§rLorem §0§lipsum dolor §r§lsit amet,§x§a§a§b§b§c§c\uE4005"
"§rLorem §0§s#DDEEFF44§lipsum dolor §r§lsit amet,§x§a§a§b§b§c§c\uE4005"
+ serializedComponent.getClickEvents().keySet().iterator().next()
+ " consectetur §x§a§a§b§b§c§c\uE800minecraft:default\uE802adipiscing \uE801§x§a§a§b§b§c§c\uE500"
+ serializedComponent.getHoverEvents().keySet().iterator().next()
+ "elit. \uE501\uE401§bMaecenas imperdiet §b\uE600some.key\uE600"
+ "elit. \uE501\uE401§r§s#AABBCC88Aenean tempor urna ac consequat sodales. §bMaecenas imperdiet §b\uE600some.key\uE600"
+ serializedComponent.getTranslatableComponents().keySet().iterator().next()
+ "\uE600",
serializedComponent.getText()
Expand Down

0 comments on commit a9c665e

Please sign in to comment.