From 3db9fb1b6942a4ae8b1e9661404533a5f4982ff9 Mon Sep 17 00:00:00 2001 From: Mystiflow Date: Sat, 16 Sep 2017 07:06:51 +0100 Subject: [PATCH] #2255: Fix ComponentBuilder clone constructor * Fix ComponentBuilder clone constructor #2255 * Fix appending text to a ComponentBuilder if current is not a TextComponent --- .../md_5/bungee/api/chat/BaseComponent.java | 36 ++++++++++++------- .../bungee/api/chat/ComponentBuilder.java | 27 ++++---------- .../md_5/bungee/api/chat/TextComponent.java | 6 ++++ .../api/chat/TranslatableComponent.java | 25 ++++++++----- .../net/md_5/bungee/chat/ComponentsTest.java | 9 +++++ 5 files changed, 62 insertions(+), 41 deletions(-) diff --git a/chat/src/main/java/net/md_5/bungee/api/chat/BaseComponent.java b/chat/src/main/java/net/md_5/bungee/api/chat/BaseComponent.java index 14c291d1..4bd60c33 100644 --- a/chat/src/main/java/net/md_5/bungee/api/chat/BaseComponent.java +++ b/chat/src/main/java/net/md_5/bungee/api/chat/BaseComponent.java @@ -76,20 +76,25 @@ public abstract class BaseComponent BaseComponent(BaseComponent old) { - setColor( old.getColorRaw() ); - setBold( old.isBoldRaw() ); - setItalic( old.isItalicRaw() ); - setUnderlined( old.isUnderlinedRaw() ); - setStrikethrough( old.isStrikethroughRaw() ); - setObfuscated( old.isObfuscatedRaw() ); - setInsertion( old.getInsertion() ); - setClickEvent( old.getClickEvent() ); - setHoverEvent( old.getHoverEvent() ); - if ( old.getExtra() != null ) + copyFormatting( old ); + } + + public void copyFormatting( BaseComponent component ) + { + setColor( component.getColorRaw() ); + setBold( component.isBoldRaw() ); + setItalic( component.isItalicRaw() ); + setUnderlined( component.isUnderlinedRaw() ); + setStrikethrough( component.isStrikethroughRaw() ); + setObfuscated( component.isObfuscatedRaw() ); + setInsertion( component.getInsertion() ); + setClickEvent( component.getClickEvent() ); + setHoverEvent( component.getHoverEvent() ); + if ( component.getExtra() != null ) { - for ( BaseComponent component : old.getExtra() ) + for ( BaseComponent extra : component.getExtra() ) { - addExtra( component.duplicate() ); + addExtra( extra.duplicate() ); } } } @@ -101,6 +106,13 @@ public abstract class BaseComponent */ public abstract BaseComponent duplicate(); + /** + * Clones the BaseComponent without formatting and returns the clone. + * + * @return The duplicate of this BaseComponent + */ + public abstract BaseComponent duplicateWithoutFormatting(); + /** * Converts the components to a string that uses the old formatting codes * ({@link net.md_5.bungee.api.ChatColor#COLOR_CHAR} 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 84490c63..6277f0c9 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 @@ -37,7 +37,7 @@ public class ComponentBuilder */ public ComponentBuilder(ComponentBuilder original) { - current = new TextComponent( original.current ); + current = original.current.duplicate(); for ( BaseComponent baseComponent : original.parts ) { parts.add( baseComponent.duplicate() ); @@ -113,8 +113,9 @@ public class ComponentBuilder { parts.add( current ); - current = new TextComponent( (TextComponent) current ); - ( (TextComponent) current ).setText( text ); + BaseComponent old = current; + current = new TextComponent( text ); + current.copyFormatting( old ); retain( retention ); return this; @@ -251,29 +252,13 @@ public class ComponentBuilder switch ( retention ) { case NONE: - 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 = current.duplicateWithoutFormatting(); break; case ALL: // No changes are required break; case EVENTS: - 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 = current.duplicateWithoutFormatting(); current.setInsertion( previous.getInsertion() ); current.setClickEvent( previous.getClickEvent() ); current.setHoverEvent( previous.getHoverEvent() ); 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 15a26785..cc7335f3 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 @@ -177,6 +177,12 @@ public class TextComponent extends BaseComponent return new TextComponent( this ); } + @Override + public BaseComponent duplicateWithoutFormatting() + { + return new TextComponent( this.text ); + } + @Override protected void toPlainText(StringBuilder builder) { diff --git a/chat/src/main/java/net/md_5/bungee/api/chat/TranslatableComponent.java b/chat/src/main/java/net/md_5/bungee/api/chat/TranslatableComponent.java index 79c2e32e..78b24ad6 100644 --- a/chat/src/main/java/net/md_5/bungee/api/chat/TranslatableComponent.java +++ b/chat/src/main/java/net/md_5/bungee/api/chat/TranslatableComponent.java @@ -66,18 +66,21 @@ public class TranslatableComponent extends BaseComponent public TranslatableComponent(String translate, Object... with) { setTranslate( translate ); - List temp = new ArrayList(); - for ( Object w : with ) + if ( with != null && with.length != 0 ) { - if ( w instanceof String ) + List temp = new ArrayList(); + for ( Object w : with ) { - temp.add( new TextComponent( (String) w ) ); - } else - { - temp.add( (BaseComponent) w ); + if ( w instanceof String ) + { + temp.add( new TextComponent( (String) w ) ); + } else + { + temp.add( (BaseComponent) w ); + } } + setWith( temp ); } - setWith( temp ); } /** @@ -91,6 +94,12 @@ public class TranslatableComponent extends BaseComponent return new TranslatableComponent( this ); } + @Override + public BaseComponent duplicateWithoutFormatting() + { + return new TranslatableComponent( this.translate, this.with ); + } + /** * Sets the translation substitutions to be used in this component. Removes * any previously set substitutions 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 c844dab5..3d9396d3 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,15 @@ import org.junit.Test; public class ComponentsTest { + @Test + public void testBuilderClone() + { + ComponentBuilder builder = new ComponentBuilder("Hel").color(ChatColor.RED).append("lo").color(ChatColor.DARK_RED); + ComponentBuilder cloned = new ComponentBuilder( builder ); + + Assert.assertEquals( TextComponent.toLegacyText( builder.create() ), TextComponent.toLegacyText( cloned.create() ) ); + } + @Test public void testBuilderAppend() {