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 a5b4b14646..fa8ca01a11 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 @@ -213,14 +213,29 @@ public BaseComponent duplicateWithoutFormatting() public static String toLegacyText(BaseComponent... components) { StringBuilder builder = new StringBuilder(); - ComponentStyle currentStyle = new ComponentStyle(); - currentStyle.setColor( ChatColor.RESET ); + ComponentStyle currentLegacy = new ComponentStyle(); + currentLegacy.setColor( ChatColor.RESET ); + + toLegacyText( builder, currentLegacy, components ); + return builder.toString(); + } + /** + * Converts the components to a string that uses the old formatting codes + * ({@link net.md_5.bungee.api.ChatColor#COLOR_CHAR} + * + * @param builder the StringBuilder to append to + * @param currentLegacy the style at the end of {@code builder} + * @param components the components to convert + * @return the style at the end of the legacy string + */ + public static ComponentStyle toLegacyText(StringBuilder builder, ComponentStyle currentLegacy, BaseComponent... components) + { for ( BaseComponent msg : components ) { - currentStyle = msg.toLegacyText( builder, currentStyle ); + currentLegacy = msg.toLegacyText( builder, currentLegacy ); } - return builder.toString(); + return currentLegacy; } /** 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 a4c45e6584..9d09b35260 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 @@ -3,6 +3,7 @@ import static net.md_5.bungee.api.ChatColor.*; import static org.junit.jupiter.api.Assertions.*; import java.awt.Color; +import java.util.Arrays; import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.function.Function; @@ -905,4 +906,21 @@ public void testExtraFormattingNested() // see TextComponent#toLegacyText comment as to why we keep it assertEquals( GOLD + "Hello " + BOLD + "World" + GOLD + RED + "!" + GOLD + " xd", component.toLegacyText() ); } + + @Test + public void testArrayToLegacyConversionContext() + { + TextComponent world = new TextComponent( "World" ); + world.setBold( true ); + BaseComponent[] components = new BaseComponent[] + { + new TextComponent( "Hello " ), + world, + new TextComponent( "!" ) + }; + System.out.println( "components = " + Arrays.toString( components ) ); + + assertEquals( "Hello World!", BaseComponent.toPlainText( components ) ); + assertEquals( "Hello " + BOLD + "World" + RESET + "!", BaseComponent.toLegacyText( components ) ); + } }