#2388: Treat BaseComponent array appends as one. Fixes #2387.

Don't copy formatting of previous element in the array being appended
but instead from the last appended component in the builder.
Otherwise formatting will be overridden in an incorrect way from
legacy text conversions.

Added unit test failed before this change. Now passes.
This commit is contained in:
Mystiflow 2018-03-14 08:51:08 +00:00 committed by md-5
parent 7ee0b6dccb
commit d7eef6ff2e
2 changed files with 21 additions and 1 deletions

View File

@ -122,11 +122,11 @@ public final class ComponentBuilder
{ {
Preconditions.checkArgument( components.length != 0, "No components to append" ); Preconditions.checkArgument( components.length != 0, "No components to append" );
BaseComponent previous = current;
for ( BaseComponent component : components ) for ( BaseComponent component : components )
{ {
parts.add( current ); parts.add( current );
BaseComponent previous = current;
current = component.duplicate(); current = component.duplicate();
current.copyFormatting( previous, retention, false ); current.copyFormatting( previous, retention, false );
} }

View File

@ -5,9 +5,29 @@ import net.md_5.bungee.chat.ComponentSerializer;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ComponentsTest public class ComponentsTest
{ {
@Test
public void testLegacyComponentBuilderAppend()
{
String text = "§a§lHello §r§kworld§7!";
BaseComponent[] components = TextComponent.fromLegacyText( text );
BaseComponent[] builderComponents = new ComponentBuilder( "" ).append( components ).create();
List<BaseComponent> list = new ArrayList<BaseComponent>( Arrays.asList( builderComponents ) );
// Remove the first element (empty text component). This needs to be done because toLegacyText always
// appends &f regardless if the color is non null or not and would otherwise mess with our unit test.
list.remove( 0 );
Assert.assertEquals(
TextComponent.toLegacyText( components ),
TextComponent.toLegacyText( list.toArray( new BaseComponent[ list.size() ] ) )
);
}
@Test @Test
public void testComponentFormatRetention() public void testComponentFormatRetention()
{ {