From 9fc862cb74f3030bce095235648fb6f7892d9abe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joshua=20K=C3=BCpper?= Date: Thu, 24 Jul 2014 02:59:47 +0200 Subject: [PATCH] Make ComponentBuilder clonable --- .../md_5/bungee/api/chat/BaseComponent.java | 7 +++++ .../bungee/api/chat/ComponentBuilder.java | 15 +++++++++++ .../md_5/bungee/api/chat/TextComponent.java | 11 ++++++++ .../api/chat/TranslatableComponent.java | 26 +++++++++++++++++++ 4 files changed, 59 insertions(+) diff --git a/api/src/main/java/net/md_5/bungee/api/chat/BaseComponent.java b/api/src/main/java/net/md_5/bungee/api/chat/BaseComponent.java index 99a68571..c8171835 100644 --- a/api/src/main/java/net/md_5/bungee/api/chat/BaseComponent.java +++ b/api/src/main/java/net/md_5/bungee/api/chat/BaseComponent.java @@ -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} diff --git a/api/src/main/java/net/md_5/bungee/api/chat/ComponentBuilder.java b/api/src/main/java/net/md_5/bungee/api/chat/ComponentBuilder.java index 18d00bc0..614d1c63 100644 --- a/api/src/main/java/net/md_5/bungee/api/chat/ComponentBuilder.java +++ b/api/src/main/java/net/md_5/bungee/api/chat/ComponentBuilder.java @@ -28,6 +28,21 @@ public class ComponentBuilder private TextComponent current; private final List 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. * diff --git a/api/src/main/java/net/md_5/bungee/api/chat/TextComponent.java b/api/src/main/java/net/md_5/bungee/api/chat/TextComponent.java index 251eaf5f..81a45b11 100644 --- a/api/src/main/java/net/md_5/bungee/api/chat/TextComponent.java +++ b/api/src/main/java/net/md_5/bungee/api/chat/TextComponent.java @@ -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) { diff --git a/api/src/main/java/net/md_5/bungee/api/chat/TranslatableComponent.java b/api/src/main/java/net/md_5/bungee/api/chat/TranslatableComponent.java index 0e81baa3..7a5187a4 100644 --- a/api/src/main/java/net/md_5/bungee/api/chat/TranslatableComponent.java +++ b/api/src/main/java/net/md_5/bungee/api/chat/TranslatableComponent.java @@ -32,6 +32,21 @@ public class TranslatableComponent extends BaseComponent */ private List 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