#2255: Fix ComponentBuilder clone constructor

* Fix ComponentBuilder clone constructor #2255
* Fix appending text to a ComponentBuilder if current is not a TextComponent
This commit is contained in:
Mystiflow 2017-09-16 07:06:51 +01:00 committed by md-5
parent ef326dba19
commit 3db9fb1b69
5 changed files with 62 additions and 41 deletions

View File

@ -76,20 +76,25 @@ public abstract class BaseComponent
BaseComponent(BaseComponent old) BaseComponent(BaseComponent old)
{ {
setColor( old.getColorRaw() ); copyFormatting( old );
setBold( old.isBoldRaw() ); }
setItalic( old.isItalicRaw() );
setUnderlined( old.isUnderlinedRaw() ); public void copyFormatting( BaseComponent component )
setStrikethrough( old.isStrikethroughRaw() );
setObfuscated( old.isObfuscatedRaw() );
setInsertion( old.getInsertion() );
setClickEvent( old.getClickEvent() );
setHoverEvent( old.getHoverEvent() );
if ( old.getExtra() != null )
{ {
for ( BaseComponent component : old.getExtra() ) 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 )
{ {
addExtra( component.duplicate() ); for ( BaseComponent extra : component.getExtra() )
{
addExtra( extra.duplicate() );
} }
} }
} }
@ -101,6 +106,13 @@ public abstract class BaseComponent
*/ */
public abstract BaseComponent duplicate(); 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 * Converts the components to a string that uses the old formatting codes
* ({@link net.md_5.bungee.api.ChatColor#COLOR_CHAR} * ({@link net.md_5.bungee.api.ChatColor#COLOR_CHAR}

View File

@ -37,7 +37,7 @@ public class ComponentBuilder
*/ */
public ComponentBuilder(ComponentBuilder original) public ComponentBuilder(ComponentBuilder original)
{ {
current = new TextComponent( original.current ); current = original.current.duplicate();
for ( BaseComponent baseComponent : original.parts ) for ( BaseComponent baseComponent : original.parts )
{ {
parts.add( baseComponent.duplicate() ); parts.add( baseComponent.duplicate() );
@ -113,8 +113,9 @@ public class ComponentBuilder
{ {
parts.add( current ); parts.add( current );
current = new TextComponent( (TextComponent) current ); BaseComponent old = current;
( (TextComponent) current ).setText( text ); current = new TextComponent( text );
current.copyFormatting( old );
retain( retention ); retain( retention );
return this; return this;
@ -251,29 +252,13 @@ public class ComponentBuilder
switch ( retention ) switch ( retention )
{ {
case NONE: case NONE:
if ( current instanceof TextComponent ) current = current.duplicateWithoutFormatting();
{
current = new TextComponent( ( (TextComponent) current ).getText() );
} else if ( current instanceof TranslatableComponent )
{
TranslatableComponent oldComponent = (TranslatableComponent) current;
current = new TranslatableComponent( oldComponent.getTranslate(), oldComponent.getWith() );
}
break; break;
case ALL: case ALL:
// No changes are required // No changes are required
break; break;
case EVENTS: case EVENTS:
if ( current instanceof TextComponent ) current = current.duplicateWithoutFormatting();
{
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.setInsertion( previous.getInsertion() );
current.setClickEvent( previous.getClickEvent() ); current.setClickEvent( previous.getClickEvent() );
current.setHoverEvent( previous.getHoverEvent() ); current.setHoverEvent( previous.getHoverEvent() );

View File

@ -177,6 +177,12 @@ public class TextComponent extends BaseComponent
return new TextComponent( this ); return new TextComponent( this );
} }
@Override
public BaseComponent duplicateWithoutFormatting()
{
return new TextComponent( this.text );
}
@Override @Override
protected void toPlainText(StringBuilder builder) protected void toPlainText(StringBuilder builder)
{ {

View File

@ -66,6 +66,8 @@ public class TranslatableComponent extends BaseComponent
public TranslatableComponent(String translate, Object... with) public TranslatableComponent(String translate, Object... with)
{ {
setTranslate( translate ); setTranslate( translate );
if ( with != null && with.length != 0 )
{
List<BaseComponent> temp = new ArrayList<BaseComponent>(); List<BaseComponent> temp = new ArrayList<BaseComponent>();
for ( Object w : with ) for ( Object w : with )
{ {
@ -79,6 +81,7 @@ public class TranslatableComponent extends BaseComponent
} }
setWith( temp ); setWith( temp );
} }
}
/** /**
* Creates a duplicate of this TranslatableComponent. * Creates a duplicate of this TranslatableComponent.
@ -91,6 +94,12 @@ public class TranslatableComponent extends BaseComponent
return new TranslatableComponent( this ); 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 * Sets the translation substitutions to be used in this component. Removes
* any previously set substitutions * any previously set substitutions

View File

@ -13,6 +13,15 @@ import org.junit.Test;
public class ComponentsTest 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 @Test
public void testBuilderAppend() public void testBuilderAppend()
{ {