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 4aa2d706..84490c63 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 @@ -1,5 +1,6 @@ package net.md_5.bungee.api.chat; +import com.google.common.base.Preconditions; import net.md_5.bungee.api.ChatColor; import java.util.ArrayList; import java.util.List; @@ -25,7 +26,7 @@ import java.util.List; public class ComponentBuilder { - private TextComponent current; + private BaseComponent current; private final List parts = new ArrayList(); /** @@ -53,6 +54,41 @@ public class ComponentBuilder current = new TextComponent( text ); } + /** + * Appends the components to the builder and makes it the current target for + * formatting. The text will have all the formatting from the previous part. + * + * @param components the components to append + * @return this ComponentBuilder for chaining + */ + public ComponentBuilder append(BaseComponent[] components) + { + return append( components, FormatRetention.ALL ); + } + + /** + * Appends the components to the builder and makes it the current target for + * formatting. You can specify the amount of formatting retained. + * + * @param components the components to append + * @param retention the formatting to retain + * @return this ComponentBuilder for chaining + */ + public ComponentBuilder append(BaseComponent[] components, FormatRetention retention) + { + Preconditions.checkArgument( components.length != 0, "No components to append" ); + + for ( BaseComponent component : components ) + { + parts.add( current ); + + current = component.duplicate(); + retain( retention ); + } + + return this; + } + /** * Appends the text to the builder and makes it the current target for * formatting. The text will have all the formatting from the previous part. @@ -77,8 +113,8 @@ public class ComponentBuilder { parts.add( current ); - current = new TextComponent( current ); - current.setText( text ); + current = new TextComponent( (TextComponent) current ); + ( (TextComponent) current ).setText( text ); retain( retention ); return this; @@ -215,13 +251,29 @@ public class ComponentBuilder switch ( retention ) { case NONE: - current = new TextComponent( current.getText() ); + if ( current instanceof TextComponent ) + { + current = new TextComponent( ( (TextComponent) current ).getText() ); + } else if ( current instanceof TranslatableComponent ) + { + TranslatableComponent oldComponent = (TranslatableComponent) current; + current = new TranslatableComponent( oldComponent.getTranslate(), oldComponent.getWith() ); + } + break; case ALL: // No changes are required break; case EVENTS: - current = new TextComponent( current.getText() ); + if ( current instanceof TextComponent ) + { + current = new TextComponent( ( (TextComponent) current ).getText() ); + } else if ( current instanceof TranslatableComponent ) + { + TranslatableComponent oldComponent = (TranslatableComponent) current; + current = new TranslatableComponent( oldComponent.getTranslate(), oldComponent.getWith() ); + } + current.setInsertion( previous.getInsertion() ); current.setClickEvent( previous.getClickEvent() ); current.setHoverEvent( previous.getHoverEvent() ); diff --git a/proxy/src/test/java/net/md_5/bungee/chat/ComponentsTest.java b/proxy/src/test/java/net/md_5/bungee/chat/ComponentsTest.java index 004a2b7a..c844dab5 100644 --- a/proxy/src/test/java/net/md_5/bungee/chat/ComponentsTest.java +++ b/proxy/src/test/java/net/md_5/bungee/chat/ComponentsTest.java @@ -13,6 +13,23 @@ import org.junit.Test; public class ComponentsTest { + @Test + public void testBuilderAppend() + { + ClickEvent clickEvent = new ClickEvent( ClickEvent.Action.RUN_COMMAND, "/help " ); + HoverEvent hoverEvent = new HoverEvent( HoverEvent.Action.SHOW_TEXT, new ComponentBuilder( "Hello world" ).create() ); + + ComponentBuilder builder = new ComponentBuilder( "Hello " ).color( ChatColor.YELLOW ); + builder.append( new ComponentBuilder( "world!" ).color( ChatColor.GREEN ).event( hoverEvent ).event( clickEvent ).create() ); + + BaseComponent[] components = builder.create(); + + Assert.assertEquals( components[1].getHoverEvent(), hoverEvent ); + Assert.assertEquals( components[1].getClickEvent(), clickEvent ); + Assert.assertEquals( "Hello world!", BaseComponent.toPlainText( components ) ); + Assert.assertEquals( ChatColor.YELLOW + "Hello " + ChatColor.GREEN + "world!", BaseComponent.toLegacyText( components ) ); + } + @Test public void testBasicComponent() {