#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)
{
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 )
{
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();
/**
* 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}

View File

@ -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() );

View File

@ -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)
{

View File

@ -66,6 +66,8 @@ public class TranslatableComponent extends BaseComponent
public TranslatableComponent(String translate, Object... with)
{
setTranslate( translate );
if ( with != null && with.length != 0 )
{
List<BaseComponent> temp = new ArrayList<BaseComponent>();
for ( Object w : with )
{
@ -79,6 +81,7 @@ public class TranslatableComponent extends BaseComponent
}
setWith( temp );
}
}
/**
* Creates a duplicate of this TranslatableComponent.
@ -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

View File

@ -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()
{