From d7eef6ff2ec249581fb3f2e0093a5c3a54d818a4 Mon Sep 17 00:00:00 2001 From: Mystiflow Date: Wed, 14 Mar 2018 08:51:08 +0000 Subject: [PATCH] #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. --- .../bungee/api/chat/ComponentBuilder.java | 2 +- .../md_5/bungee/api/chat/ComponentsTest.java | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/chat/src/main/java/net/md_5/bungee/api/chat/ComponentBuilder.java b/chat/src/main/java/net/md_5/bungee/api/chat/ComponentBuilder.java index e1e96b68..7f4ae5ca 100644 --- a/chat/src/main/java/net/md_5/bungee/api/chat/ComponentBuilder.java +++ b/chat/src/main/java/net/md_5/bungee/api/chat/ComponentBuilder.java @@ -122,11 +122,11 @@ public final class ComponentBuilder { Preconditions.checkArgument( components.length != 0, "No components to append" ); + BaseComponent previous = current; for ( BaseComponent component : components ) { parts.add( current ); - BaseComponent previous = current; current = component.duplicate(); current.copyFormatting( previous, retention, false ); } 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 ce56c7f7..5bf1bf8e 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 @@ -5,9 +5,29 @@ import net.md_5.bungee.chat.ComponentSerializer; import org.junit.Assert; import org.junit.Test; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + 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 list = new ArrayList( 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 public void testComponentFormatRetention() {