Skip to content

Commit 27a9998

Browse files
committed
feat: support 1.21.4+ shadowColor in component parser
Closes #470
1 parent 1283f29 commit 27a9998

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

Diff for: core/src/main/java/com/rexcantor64/triton/language/parser/AdvancedComponent.java

+17
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import lombok.val;
88
import net.md_5.bungee.api.ChatColor;
99
import net.md_5.bungee.api.chat.*;
10+
import net.md_5.bungee.api.chat.TextComponent;
1011

12+
import java.awt.*;
1113
import java.util.ArrayList;
1214
import java.util.HashMap;
1315
import java.util.List;
@@ -48,6 +50,16 @@ public static AdvancedComponent fromBaseComponent(boolean onlyText, BaseComponen
4850
boolean hasHover = false;
4951
boolean hasFont = false;
5052
builder.append(ComponentUtils.getColorFromBaseComponent(comp).toString());
53+
try {
54+
Color shadowColor = comp.getShadowColor();
55+
if (shadowColor != null) {
56+
// getRGB also includes alpha information
57+
builder.append("§s");
58+
builder.append(String.format("%08x", shadowColor.getRGB()));
59+
}
60+
} catch (NoSuchMethodError ignore) {
61+
// old versions of Spigot don't have getShadowColor()
62+
}
5163
if (comp.isBold())
5264
builder.append(ChatColor.BOLD.toString());
5365
if (comp.isItalic())
@@ -159,6 +171,11 @@ private List<BaseComponent> toBaseComponent(String text) {
159171
val color = text.substring(i + 1, i + 13);
160172
format = ChatColor.of("#" + color.replace("\u00A7", ""));
161173
i += 12;
174+
} else if (lowercaseChar == 's' && i + 8 < text.length()) {
175+
val color = text.substring(i + 1, i + 9);
176+
component.setShadowColor(new Color(Integer.parseUnsignedInt(color, 16), true));
177+
i += 8;
178+
continue;
162179
} else {
163180
format = ChatColor.getByChar(lowercaseChar);
164181
}

Diff for: core/src/main/java/com/rexcantor64/triton/utils/ComponentUtils.java

+11
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ public static void copyFormatting(BaseComponent origin, BaseComponent target) {
7373
} catch (NoSuchMethodError ignore) {
7474
// Ignore, it's an outdated server
7575
}
76+
try {
77+
target.setShadowColor(origin.getShadowColorRaw());
78+
} catch (NoSuchMethodError ignore) {
79+
// Ignore, it's an outdated server
80+
}
7681
}
7782

7883
public static ChatColor getColorFromBaseComponent(BaseComponent bc) {
@@ -100,6 +105,12 @@ public static boolean haveSameFormatting(BaseComponent c1, BaseComponent c2) {
100105
} catch (NoSuchMethodError ignore) {
101106
// Ignore, it's an outdated server
102107
}
108+
try {
109+
if (!Objects.equals(c1.getShadowColorRaw(), c2.getShadowColorRaw()))
110+
return false;
111+
} catch (NoSuchMethodError ignore) {
112+
// Ignore, it's an outdated server
113+
}
103114
return c1.getColorRaw() == c2.getColorRaw() &&
104115
c1.isBoldRaw() == c2.isBoldRaw() &&
105116
c1.isItalicRaw() == c2.isItalicRaw() &&

Diff for: core/src/test/java/com/rexcantor64/triton/language/parser/AdvancedComponentTest.java

+24
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import net.md_5.bungee.chat.ComponentSerializer;
99
import org.junit.jupiter.api.Test;
1010

11+
import java.awt.*;
12+
1113
import static org.junit.jupiter.api.Assertions.assertEquals;
1214

1315
public class AdvancedComponentTest {
@@ -33,4 +35,26 @@ public void testColorCodeBetweenClickEvent() {
3335
String expectedResultJson = "{\"extra\":[{\"strikethrough\":true,\"color\":\"gray\",\"hoverEvent\":{\"action\":\"show_text\",\"contents\":\"\"},\"extra\":[{\"text\":\"Testing\"}],\"text\":\"\"},{\"color\":\"gray\",\"hoverEvent\":{\"action\":\"show_text\",\"contents\":\"\"},\"extra\":[{\"text\":\"another test\"}],\"text\":\"\"}],\"text\":\"\"}";
3436
assertEquals(expectedResultJson, ComponentSerializer.toString(components));
3537
}
38+
39+
@Test
40+
public void testShadowColor() {
41+
BaseComponent root = new TextComponent();
42+
BaseComponent child1 = new TextComponent("Testing");
43+
child1.setColor(ChatColor.GRAY);
44+
child1.setStrikethrough(true);
45+
child1.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text("")));
46+
root.addExtra(child1);
47+
BaseComponent child2 = new TextComponent("another test");
48+
child2.setColor(ChatColor.GRAY);
49+
child2.setShadowColor(new Color(0x33, 0x44, 0x55, 0x88));
50+
root.addExtra(child2);
51+
52+
AdvancedComponent advancedComponent = AdvancedComponent.fromBaseComponent(root);
53+
advancedComponent.setText(advancedComponent.getTextClean());
54+
55+
BaseComponent[] components = advancedComponent.toBaseComponent();
56+
57+
String expectedResultJson = "{\"extra\":[{\"strikethrough\":true,\"color\":\"gray\",\"hoverEvent\":{\"action\":\"show_text\",\"contents\":\"\"},\"extra\":[{\"text\":\"Testing\"}],\"text\":\"\"},{\"color\":\"gray\",\"shadow_color\":-2009906091,\"text\":\"another test\"}],\"text\":\"\"}";
58+
assertEquals(expectedResultJson, ComponentSerializer.toString(components));
59+
}
3660
}

0 commit comments

Comments
 (0)