#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() ); setColor( component.getColorRaw() );
setInsertion( old.getInsertion() ); setBold( component.isBoldRaw() );
setClickEvent( old.getClickEvent() ); setItalic( component.isItalicRaw() );
setHoverEvent( old.getHoverEvent() ); setUnderlined( component.isUnderlinedRaw() );
if ( old.getExtra() != null ) 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(); 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,18 +66,21 @@ public class TranslatableComponent extends BaseComponent
public TranslatableComponent(String translate, Object... with) public TranslatableComponent(String translate, Object... with)
{ {
setTranslate( translate ); setTranslate( translate );
List<BaseComponent> temp = new ArrayList<BaseComponent>(); if ( with != null && with.length != 0 )
for ( Object w : with )
{ {
if ( w instanceof String ) List<BaseComponent> temp = new ArrayList<BaseComponent>();
for ( Object w : with )
{ {
temp.add( new TextComponent( (String) w ) ); if ( w instanceof String )
} else {
{ temp.add( new TextComponent( (String) w ) );
temp.add( (BaseComponent) w ); } else
{
temp.add( (BaseComponent) w );
}
} }
setWith( temp );
} }
setWith( temp );
} }
/** /**
@ -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()
{ {