Make ComponentBuilder clonable

This commit is contained in:
Joshua Küpper 2014-07-24 02:59:47 +02:00 committed by Thinkofdeath
parent 0d174b51c5
commit 9fc862cb74
4 changed files with 59 additions and 0 deletions

View File

@ -80,6 +80,13 @@ public abstract class BaseComponent
setHoverEvent( old.getHoverEvent() );
}
/**
* Clones the BaseComponent and returns the clone.
*
* @return The duplicate of this BaseComponent
*/
public abstract BaseComponent duplicate();
/**
* Converts the components to a string that uses the old formatting codes
* ({@link net.md_5.bungee.api.ChatColor#COLOR_CHAR}

View File

@ -28,6 +28,21 @@ public class ComponentBuilder
private TextComponent current;
private final List<BaseComponent> parts = new ArrayList<>();
/**
* Creates a ComponentBuilder from the other given ComponentBuilder to clone it.
*
* @param original the original for the new ComponentBuilder.
*/
public ComponentBuilder(ComponentBuilder original)
{
current = new TextComponent( original.current );
for ( BaseComponent baseComponent : original.parts )
{
parts.add( baseComponent.duplicate() );
}
}
/**
* Creates a ComponentBuilder with the given text as the first part.
*

View File

@ -159,6 +159,17 @@ public class TextComponent extends BaseComponent
setExtra( Arrays.asList( extras ) );
}
/**
* Creates a duplicate of this TextComponent.
*
* @return the duplicate of this TextComponent.
*/
@Override
public BaseComponent duplicate()
{
return new TextComponent( this );
}
@Override
protected void toPlainText(StringBuilder builder)
{

View File

@ -32,6 +32,21 @@ public class TranslatableComponent extends BaseComponent
*/
private List<BaseComponent> with;
/**
* Creates a translatable component from the original to clone it.
*
* @param original the original for the new translatable component.
*/
public TranslatableComponent(TranslatableComponent original)
{
super( original );
setTranslate( original.getTranslate() );
for ( BaseComponent baseComponent : original.getWith() )
{
with.add( baseComponent.duplicate() );
}
}
/**
* Creates a translatable component with the passed substitutions
*
@ -59,6 +74,17 @@ public class TranslatableComponent extends BaseComponent
setWith( temp );
}
/**
* Creates a duplicate of this TranslatableComponent.
*
* @return the duplicate of this TranslatableComponent.
*/
@Override
public BaseComponent duplicate()
{
return new TranslatableComponent( this );
}
/**
* Sets the translation substitutions to be used in this component. Removes
* any previously set substitutions