Skip to content

Commit

Permalink
fix: error while deserializing translatable component
Browse files Browse the repository at this point in the history
Also add tests.
  • Loading branch information
diogotcorreia committed Aug 24, 2024
1 parent 50e272a commit 1f38c5a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,14 @@ public SerializedComponent(Component component) {
}
list.add(componentBuilder.build());
componentBuilder = Component.text();
// TODO check if this works if style changes inside the children
componentBuilder.style(currentStyle);
} else if (c == TRANSLATABLE_DELIM) {
i++;
while (text.charAt(i) != TRANSLATABLE_DELIM) {
// ignore key (still here for backwards compatibility)
i++;
}
i++;
val uuid = new StringBuilder();
while (text.charAt(i) != TRANSLATABLE_DELIM) {
uuid.append(text.charAt(i));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,62 +9,84 @@
import net.kyori.adventure.text.format.TextDecoration;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class LegacyParserTest {

private final AdventureParser parser = new AdventureParser();
private final Component ALL_IN_ONE_COMPONENT = Component.text("Lorem ")
.append(
Component.text("ipsum dolor ")
.color(NamedTextColor.BLACK)
.decorate(TextDecoration.BOLD)
)
.append(
Component.text("sit amet,")
.decorate(TextDecoration.BOLD)
)
.append(
Component.text(" consectetur ")
.color(TextColor.color(0xaabbcc))
.clickEvent(ClickEvent.copyToClipboard("some text"))
.append(
Component.text("adipiscing ")
.font(Key.key("default"))
)
.append(
Component.text("elit. ")
.hoverEvent(HoverEvent.showText(Component.text("hello world")))
)
)
.append(
Component.text("Maecenas imperdiet ")
.color(NamedTextColor.AQUA)
.append(
Component.translatable(
"some.key",
Component.text("arg 1")
)
)
);

@Test
public void testSerializingComponent() {
Component component = Component.text("Lorem ")
.append(
Component.text("ipsum dolor ")
.color(NamedTextColor.BLACK)
.decorate(TextDecoration.BOLD)
)
.append(
Component.text("sit amet,")
.decorate(TextDecoration.BOLD)
)
.append(
Component.text(" consectetur ")
.color(TextColor.color(0xaabbcc))
.clickEvent(ClickEvent.copyToClipboard("some text"))
.append(
Component.text("adipiscing ")
.font(Key.key("default"))
)
.append(
Component.text("elit. ")
.hoverEvent(HoverEvent.showText(Component.text("hello world")))
)
)
.append(
Component.text("Maecenas imperdiet ")
.color(NamedTextColor.AQUA)
.append(
Component.translatable(
"some.key",
Component.text("arg 1")
)
)
);

LegacyParser.SerializedComponent serializedComponent = new LegacyParser.SerializedComponent(component);
LegacyParser.SerializedComponent serializedComponent = new LegacyParser.SerializedComponent(ALL_IN_ONE_COMPONENT);

assertEquals(1, serializedComponent.getClickEvents().size());
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"
+ 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"
+ serializedComponent.getTranslatableComponents().keySet().iterator().next()
+ "\uE600",
+ 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"
+ serializedComponent.getTranslatableComponents().keySet().iterator().next()
+ "\uE600",
serializedComponent.getText()
);
}

@Test
public void testSerializeDeserializeComponent() {
Component result = new LegacyParser.SerializedComponent(ALL_IN_ONE_COMPONENT).toComponent();

// slightly modify input to equivalent component, due to behaviour of the deserializer
List<Component> expectedChildren = new ArrayList<>(ALL_IN_ONE_COMPONENT.children());
expectedChildren.remove(expectedChildren.size() - 1); // remove last child
Component expected = ALL_IN_ONE_COMPONENT.children(expectedChildren)
.append(
Component.text("Maecenas imperdiet ")
.color(NamedTextColor.AQUA)
)
.append(
Component.translatable(
"some.key",
Component.text("arg 1")
).color(NamedTextColor.AQUA)
);

assertEquals(expected.compact(), result.compact());
}
}

0 comments on commit 1f38c5a

Please sign in to comment.