Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support 1.21.4+ shadowColor in component parser #479

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import lombok.val;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.*;
import net.md_5.bungee.api.chat.TextComponent;

import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -48,6 +50,16 @@ public static AdvancedComponent fromBaseComponent(boolean onlyText, BaseComponen
boolean hasHover = false;
boolean hasFont = false;
builder.append(ComponentUtils.getColorFromBaseComponent(comp).toString());
try {
Color shadowColor = comp.getShadowColor();
if (shadowColor != null) {
// getRGB also includes alpha information
builder.append("§s");
builder.append(String.format("%08x", shadowColor.getRGB()));
}
} catch (NoSuchMethodError ignore) {
// old versions of Spigot don't have getShadowColor()
}
if (comp.isBold())
builder.append(ChatColor.BOLD.toString());
if (comp.isItalic())
Expand Down Expand Up @@ -159,6 +171,11 @@ private List<BaseComponent> toBaseComponent(String text) {
val color = text.substring(i + 1, i + 13);
format = ChatColor.of("#" + color.replace("\u00A7", ""));
i += 12;
} else if (lowercaseChar == 's' && i + 8 < text.length()) {
val color = text.substring(i + 1, i + 9);
component.setShadowColor(new Color(Integer.parseUnsignedInt(color, 16), true));
i += 8;
continue;
} else {
format = ChatColor.getByChar(lowercaseChar);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public static void copyFormatting(BaseComponent origin, BaseComponent target) {
} catch (NoSuchMethodError ignore) {
// Ignore, it's an outdated server
}
try {
target.setShadowColor(origin.getShadowColorRaw());
} catch (NoSuchMethodError ignore) {
// Ignore, it's an outdated server
}
}

public static ChatColor getColorFromBaseComponent(BaseComponent bc) {
Expand Down Expand Up @@ -100,6 +105,12 @@ public static boolean haveSameFormatting(BaseComponent c1, BaseComponent c2) {
} catch (NoSuchMethodError ignore) {
// Ignore, it's an outdated server
}
try {
if (!Objects.equals(c1.getShadowColorRaw(), c2.getShadowColorRaw()))
return false;
} catch (NoSuchMethodError ignore) {
// Ignore, it's an outdated server
}
return c1.getColorRaw() == c2.getColorRaw() &&
c1.isBoldRaw() == c2.isBoldRaw() &&
c1.isItalicRaw() == c2.isItalicRaw() &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import net.md_5.bungee.chat.ComponentSerializer;
import org.junit.jupiter.api.Test;

import java.awt.*;

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

public class AdvancedComponentTest {
Expand All @@ -33,4 +35,26 @@ public void testColorCodeBetweenClickEvent() {
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\":\"\"}";
assertEquals(expectedResultJson, ComponentSerializer.toString(components));
}

@Test
public void testShadowColor() {
BaseComponent root = new TextComponent();
BaseComponent child1 = new TextComponent("Testing");
child1.setColor(ChatColor.GRAY);
child1.setStrikethrough(true);
child1.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text("")));
root.addExtra(child1);
BaseComponent child2 = new TextComponent("another test");
child2.setColor(ChatColor.GRAY);
child2.setShadowColor(new Color(0x33, 0x44, 0x55, 0x88));
root.addExtra(child2);

AdvancedComponent advancedComponent = AdvancedComponent.fromBaseComponent(root);
advancedComponent.setText(advancedComponent.getTextClean());

BaseComponent[] components = advancedComponent.toBaseComponent();

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\":\"\"}";
assertEquals(expectedResultJson, ComponentSerializer.toString(components));
}
}
Loading