From f54f0e3f6a4598a529c1502c2233635dc912460d Mon Sep 17 00:00:00 2001 From: CosmoConsole <39194801+CosmoConsole@users.noreply.github.com> Date: Sat, 12 May 2018 17:27:31 +0300 Subject: [PATCH] #2421: fromLegacyText - return formatted component even if empty --- .../md_5/bungee/api/chat/TextComponent.java | 12 ++---------- .../md_5/bungee/api/chat/ComponentsTest.java | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/chat/src/main/java/net/md_5/bungee/api/chat/TextComponent.java b/chat/src/main/java/net/md_5/bungee/api/chat/TextComponent.java index 694719c8..2411793b 100644 --- a/chat/src/main/java/net/md_5/bungee/api/chat/TextComponent.java +++ b/chat/src/main/java/net/md_5/bungee/api/chat/TextComponent.java @@ -132,17 +132,9 @@ public final class TextComponent extends BaseComponent } builder.append( c ); } - if ( builder.length() > 0 ) - { - component.setText( builder.toString() ); - components.add( component ); - } - // The client will crash if the array is empty - if ( components.isEmpty() ) - { - components.add( new TextComponent( "" ) ); - } + component.setText( builder.toString() ); + components.add( component ); return components.toArray( new BaseComponent[ components.size() ] ); } 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 6eda2a72..27b5bbea 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 @@ -113,9 +113,9 @@ public class ComponentsTest BaseComponent[] test2 = TextComponent.fromLegacyText( "Text http://spigotmc.org " + ChatColor.GREEN + "google.com/test" ); Assert.assertEquals( "Text http://spigotmc.org google.com/test", BaseComponent.toPlainText( test2 ) ); - //The extra ChatColor.WHITEs are sometimes inserted when not needed but it doesn't change the result + //The extra ChatColor instances are sometimes inserted when not needed but it doesn't change the result Assert.assertEquals( ChatColor.WHITE + "Text " + ChatColor.WHITE + "http://spigotmc.org" + ChatColor.WHITE - + " " + ChatColor.GREEN + "google.com/test", BaseComponent.toLegacyText( test2 ) ); + + " " + ChatColor.GREEN + "google.com/test" + ChatColor.GREEN, BaseComponent.toLegacyText( test2 ) ); ClickEvent url1 = test2[1].getClickEvent(); Assert.assertNotNull( url1 ); @@ -275,6 +275,20 @@ public class ComponentsTest Assert.assertEquals( emptyLegacyText, invalidColorCodesLegacyText ); } + @Test + public void testFormattingOnlyTextConversion() + { + String text = "§a"; + + BaseComponent[] converted = TextComponent.fromLegacyText( text ); + Assert.assertEquals( ChatColor.GREEN, converted[0].getColor() ); + + String roundtripLegacyText = BaseComponent.toLegacyText( converted ); + + // color code should not be lost during conversion + Assert.assertEquals( text, roundtripLegacyText ); + } + private String fromAndToLegacyText(String legacyText) { return BaseComponent.toLegacyText( TextComponent.fromLegacyText( legacyText ) );