#2421: fromLegacyText - return formatted component even if empty

This commit is contained in:
CosmoConsole 2018-05-12 17:27:31 +03:00 committed by md_5
parent 88bacf12a3
commit f54f0e3f6a
2 changed files with 18 additions and 12 deletions

View File

@ -132,17 +132,9 @@ public final class TextComponent extends BaseComponent
} }
builder.append( c ); builder.append( c );
} }
if ( builder.length() > 0 )
{
component.setText( builder.toString() ); component.setText( builder.toString() );
components.add( component ); components.add( component );
}
// The client will crash if the array is empty
if ( components.isEmpty() )
{
components.add( new TextComponent( "" ) );
}
return components.toArray( new BaseComponent[ components.size() ] ); return components.toArray( new BaseComponent[ components.size() ] );
} }

View File

@ -113,9 +113,9 @@ public class ComponentsTest
BaseComponent[] test2 = TextComponent.fromLegacyText( "Text http://spigotmc.org " + ChatColor.GREEN + "google.com/test" ); 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 ) ); 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 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(); ClickEvent url1 = test2[1].getClickEvent();
Assert.assertNotNull( url1 ); Assert.assertNotNull( url1 );
@ -275,6 +275,20 @@ public class ComponentsTest
Assert.assertEquals( emptyLegacyText, invalidColorCodesLegacyText ); 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) private String fromAndToLegacyText(String legacyText)
{ {
return BaseComponent.toLegacyText( TextComponent.fromLegacyText( legacyText ) ); return BaseComponent.toLegacyText( TextComponent.fromLegacyText( legacyText ) );