From 6ab0f5eba75abdec9aaadfae133e296187c4550c Mon Sep 17 00:00:00 2001 From: md_5 Date: Sun, 31 Mar 2024 09:51:05 +1100 Subject: [PATCH] #3621: Warn about use of valid chat colors and add test --- .../java/net/md_5/bungee/api/chat/BaseComponent.java | 3 +++ .../java/net/md_5/bungee/api/chat/ComponentStyle.java | 3 +++ .../java/net/md_5/bungee/api/chat/ComponentsTest.java | 11 +++++++++++ 3 files changed, 17 insertions(+) diff --git a/chat/src/main/java/net/md_5/bungee/api/chat/BaseComponent.java b/chat/src/main/java/net/md_5/bungee/api/chat/BaseComponent.java index 4490df112..5a3a54364 100644 --- a/chat/src/main/java/net/md_5/bungee/api/chat/BaseComponent.java +++ b/chat/src/main/java/net/md_5/bungee/api/chat/BaseComponent.java @@ -253,6 +253,9 @@ public abstract class BaseComponent /** * Set this component's color. + *

+ * Warning: This should be a color, not formatting code (ie, + * {@link ChatColor#getColor()} should not be null). * * @param color the component color, or null to use the default */ diff --git a/chat/src/main/java/net/md_5/bungee/api/chat/ComponentStyle.java b/chat/src/main/java/net/md_5/bungee/api/chat/ComponentStyle.java index 8b18aa768..7daaffa70 100644 --- a/chat/src/main/java/net/md_5/bungee/api/chat/ComponentStyle.java +++ b/chat/src/main/java/net/md_5/bungee/api/chat/ComponentStyle.java @@ -18,6 +18,9 @@ public final class ComponentStyle implements Cloneable /** * The color of this style. + *

+ * Warning: This should be a color, not formatting code (ie, + * {@link ChatColor#getColor()} should not be null). */ private ChatColor color; /** diff --git a/chat/src/test/java/net/md_5/bungee/api/chat/ComponentsTest.java b/chat/src/test/java/net/md_5/bungee/api/chat/ComponentsTest.java index 8b41182ab..ad844e53b 100644 --- a/chat/src/test/java/net/md_5/bungee/api/chat/ComponentsTest.java +++ b/chat/src/test/java/net/md_5/bungee/api/chat/ComponentsTest.java @@ -155,6 +155,17 @@ public class ComponentsTest assertThrows( IndexOutOfBoundsException.class, () -> builder.getComponent( 1 ) ); } + @Test + public void testFormatNotColor() + { + BaseComponent[] component = new ComponentBuilder().color( ChatColor.BOLD ).append( "Test" ).create(); + + String json = ComponentSerializer.toString( component ); + BaseComponent[] parsed = ComponentSerializer.parse( json ); + + assertNull( parsed[0].getColorRaw(), "Format should not be preserved as color" ); + } + @Test public void testComponentParting() {