From a9c665e0660b101d869824aa2bbda009034532e9 Mon Sep 17 00:00:00 2001 From: Diogo Correia Date: Thu, 23 Jan 2025 19:57:39 +0100 Subject: [PATCH] feat: support 1.21.4+ shadowColor in legacy parser See #470 --- .../triton/language/parser/LegacyParser.java | 16 ++++++++++++++++ .../triton/language/parser/LegacyParserTest.java | 10 ++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/com/rexcantor64/triton/language/parser/LegacyParser.java b/core/src/main/java/com/rexcantor64/triton/language/parser/LegacyParser.java index 5a7e763c..0f2bfadf 100644 --- a/core/src/main/java/com/rexcantor64/triton/language/parser/LegacyParser.java +++ b/core/src/main/java/com/rexcantor64/triton/language/parser/LegacyParser.java @@ -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; @@ -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 @@ -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) @@ -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()))); diff --git a/core/src/test/java/com/rexcantor64/triton/language/parser/LegacyParserTest.java b/core/src/test/java/com/rexcantor64/triton/language/parser/LegacyParserTest.java index 91784398..198eba97 100644 --- a/core/src/test/java/com/rexcantor64/triton/language/parser/LegacyParserTest.java +++ b/core/src/test/java/com/rexcantor64/triton/language/parser/LegacyParserTest.java @@ -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; @@ -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,") @@ -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) @@ -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()