Skip to content

Commit 2ecdc8f

Browse files
committed
fix setting player color on 1.8 via prefix (#119)
1 parent d505394 commit 2ecdc8f

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

implementation/src/main/java/net/megavex/scoreboardlibrary/implementation/commons/LegacyFormatUtil.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.megavex.scoreboardlibrary.implementation.commons;
22

33
import net.kyori.adventure.text.Component;
4+
import net.kyori.adventure.text.TextComponent;
45
import net.kyori.adventure.text.format.NamedTextColor;
56
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
67
import net.kyori.adventure.text.serializer.legacy.LegacyFormat;
@@ -14,6 +15,7 @@
1415
import java.util.Objects;
1516

1617
import static net.kyori.adventure.text.Component.empty;
18+
import static net.kyori.adventure.text.Component.space;
1719
import static net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection;
1820
import static net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.parseChar;
1921

@@ -57,7 +59,24 @@ public static String serialize(@Nullable Component component, @Nullable Locale l
5759
translated = component;
5860
}
5961

60-
return legacySection().serialize(translated);
62+
String legacyFormat = legacySection().serialize(translated);
63+
64+
// Legacy format serializer ignores empty components, so it's impossible to set player color on 1.8 via team prefix
65+
// Need to manually add the missing legacy format of the last component
66+
Component lastChild = translated;
67+
while (!lastChild.children().isEmpty()) {
68+
lastChild = lastChild.children().get(0);
69+
}
70+
71+
if (lastChild instanceof TextComponent) {
72+
String content = ((TextComponent) lastChild).content();
73+
if (content.isEmpty()) {
74+
String ending = legacySection().serialize(lastChild.append(space()));
75+
legacyFormat += ending.substring(0, ending.length() - 1);
76+
}
77+
}
78+
79+
return legacyFormat;
6180
}
6281

6382
public static char getChar(@Nullable NamedTextColor color) {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package net.megavex.scoreboardlibrary.implementation;
2+
3+
import net.kyori.adventure.text.Component;
4+
import net.kyori.adventure.text.format.NamedTextColor;
5+
import net.kyori.adventure.text.format.TextDecoration;
6+
import net.megavex.scoreboardlibrary.implementation.commons.LegacyFormatUtil;
7+
import org.junit.jupiter.api.Assertions;
8+
import org.junit.jupiter.api.Test;
9+
10+
import static net.kyori.adventure.text.Component.text;
11+
12+
class LegacyFormatUtilTest {
13+
@Test
14+
void serializeTest() {
15+
assertSerialization(text(""), "");
16+
assertSerialization(text(" ", NamedTextColor.RED), "§c ");
17+
assertSerialization(text("", NamedTextColor.RED), "§c");
18+
19+
assertSerialization(text("Text", NamedTextColor.AQUA, TextDecoration.BOLD), "§b§lText");
20+
Component nested = text("Text ", NamedTextColor.AQUA, TextDecoration.BOLD).append(text("", NamedTextColor.AQUA).append(text("", NamedTextColor.RED)));
21+
assertSerialization(nested, "§b§lText §c");
22+
}
23+
24+
private void assertSerialization(Component component, String expected) {
25+
String legacy = LegacyFormatUtil.serialize(component, null);
26+
Assertions.assertEquals(expected, legacy);
27+
}
28+
}

0 commit comments

Comments
 (0)