Skip to content

Commit a232bed

Browse files
committed
fix(core): order of end delimiters on serialized component
Previously, the delimiters were incorrectly nested, leading to components with both click and hover (or font) to throw a string index out of bounds exception when deserializing.
1 parent fd5931c commit a232bed

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

core/src/main/java/com/rexcantor64/triton/language/parser/LegacyParser.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import net.kyori.adventure.text.TranslationArgument;
1919
import net.kyori.adventure.text.event.ClickEvent;
2020
import net.kyori.adventure.text.event.HoverEvent;
21-
import net.kyori.adventure.text.flattener.ComponentFlattener;
22-
import net.kyori.adventure.text.flattener.FlattenerListener;
2321
import net.kyori.adventure.text.format.NamedTextColor;
2422
import net.kyori.adventure.text.format.ShadowColor;
2523
import net.kyori.adventure.text.format.Style;
@@ -736,6 +734,17 @@ public void popStyle(@NotNull Style style) {
736734
this.linkBugStyle = null;
737735
val i = this.topIndex--;
738736

737+
// The order needs to be the reverse of pushStyle
738+
@Nullable val font = style.font();
739+
if (font != null && (i == 0 || !font.equals(this.stack[i - 1].font()))) {
740+
this.stringBuilder.append(FONT_END_DELIM);
741+
}
742+
743+
@Nullable val hoverEvent = style.hoverEvent();
744+
if (hoverEvent != null && (i == 0 || !hoverEvent.equals(this.stack[i - 1].hoverEvent()))) {
745+
this.stringBuilder.append(HOVER_END_DELIM);
746+
}
747+
739748
@Nullable val clickEvent = style.clickEvent();
740749
if (clickEvent != null && (i == 0 || !clickEvent.equals(this.stack[i - 1].clickEvent()))) {
741750
// See comment on linkBugStyle
@@ -745,16 +754,6 @@ public void popStyle(@NotNull Style style) {
745754
this.linkBugStyle = this.stack[i].clickEvent(null);
746755
}
747756
}
748-
749-
@Nullable val hoverEvent = style.hoverEvent();
750-
if (hoverEvent != null && (i == 0 || !hoverEvent.equals(this.stack[i - 1].hoverEvent()))) {
751-
this.stringBuilder.append(HOVER_END_DELIM);
752-
}
753-
754-
@Nullable val font = style.font();
755-
if (font != null && (i == 0 || !font.equals(this.stack[i - 1].font()))) {
756-
this.stringBuilder.append(FONT_END_DELIM);
757-
}
758757
}
759758

760759
public String toString() {

core/src/test/java/com/rexcantor64/triton/language/parser/LegacyParserTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,4 +441,28 @@ public void testParseComponentWithDisabledLine() {
441441

442442
assertEquals(TranslationResult.ResultState.TO_REMOVE, result.getState());
443443
}
444+
445+
@Test
446+
public void testParseComponentWithClickAndHoverAndFontInSameComponent() {
447+
Component comp = Component.text()
448+
.content("[lang]without.formatting[/lang]")
449+
.hoverEvent(HoverEvent.showText(Component.text("hover")))
450+
.clickEvent(ClickEvent.copyToClipboard("click"))
451+
.font(Key.key("testing:testing"))
452+
.asComponent();
453+
454+
TranslationResult<Component> result = parser.translateComponent(new SerializedComponent(comp), configuration)
455+
.map(SerializedComponent::toComponent);
456+
457+
Component expected = Component.text()
458+
.content("This is text without formatting")
459+
.hoverEvent(HoverEvent.showText(Component.text("hover")))
460+
.clickEvent(ClickEvent.copyToClipboard("click"))
461+
.font(Key.key("testing:testing"))
462+
.asComponent();
463+
464+
assertEquals(TranslationResult.ResultState.CHANGED, result.getState());
465+
assertNotNull(result.getResultRaw());
466+
assertEquals(expected.compact(), result.getResultRaw().compact());
467+
}
444468
}

0 commit comments

Comments
 (0)