diff --git a/chat/src/main/java/net/md_5/bungee/api/chat/ComponentBuilder.java b/chat/src/main/java/net/md_5/bungee/api/chat/ComponentBuilder.java index 2b02a49d..537a0662 100644 --- a/chat/src/main/java/net/md_5/bungee/api/chat/ComponentBuilder.java +++ b/chat/src/main/java/net/md_5/bungee/api/chat/ComponentBuilder.java @@ -204,6 +204,33 @@ public final class ComponentBuilder return this; } + /** + * Appends the {@link TranslationProvider} object to the builder and makes + * the last element the current target for formatting. The components will + * have all the formatting from previous part. + * + * @param translatable the translatable object to append + * @return this ComponentBuilder for chaining + */ + public ComponentBuilder append(TranslationProvider translatable) + { + return append( translatable, FormatRetention.ALL ); + } + + /** + * Appends the {@link TranslationProvider} object to the builder and makes + * the last element the current target for formatting. You can specify the + * amount of formatting retained from previous part. + * + * @param translatable the translatable object to append + * @param retention the formatting to retain + * @return this ComponentBuilder for chaining + */ + public ComponentBuilder append(TranslationProvider translatable, FormatRetention retention) + { + return append( translatable.asTranslatableComponent(), retention ); + } + /** * Appends the text to the builder and makes it the current target for * formatting. The text will have all the formatting from previous part. diff --git a/chat/src/main/java/net/md_5/bungee/api/chat/TranslatableComponent.java b/chat/src/main/java/net/md_5/bungee/api/chat/TranslatableComponent.java index ab0fda6a..13c09564 100644 --- a/chat/src/main/java/net/md_5/bungee/api/chat/TranslatableComponent.java +++ b/chat/src/main/java/net/md_5/bungee/api/chat/TranslatableComponent.java @@ -86,6 +86,21 @@ public final class TranslatableComponent extends BaseComponent } } + /** + * Creates a translatable component with the passed substitutions + * + * @param translatable the translatable object + * @param with the {@link java.lang.String}s and + * {@link net.md_5.bungee.api.chat.BaseComponent}s to use into the + * translation + * @see #translate + * @see #setWith(java.util.List) + */ + public TranslatableComponent(TranslationProvider translatable, Object... with) + { + this( translatable.getTranslationKey(), with ); + } + /** * Creates a duplicate of this TranslatableComponent. * diff --git a/chat/src/main/java/net/md_5/bungee/api/chat/TranslationProvider.java b/chat/src/main/java/net/md_5/bungee/api/chat/TranslationProvider.java new file mode 100644 index 00000000..926dd9db --- /dev/null +++ b/chat/src/main/java/net/md_5/bungee/api/chat/TranslationProvider.java @@ -0,0 +1,38 @@ +package net.md_5.bungee.api.chat; + +/** + * An object capable of being translated by the client in a + * {@link TranslatableComponent}. + */ +public interface TranslationProvider +{ + + /** + * Get the translation key. + * + * @return the translation key + */ + String getTranslationKey(); + + /** + * Get this translatable object as a {@link TranslatableComponent}. + * + * @return the translatable component + */ + default TranslatableComponent asTranslatableComponent() + { + return asTranslatableComponent( (Object[]) null ); + } + + /** + * Get this translatable object as a {@link TranslatableComponent}. + * + * @param with the {@link String Strings} and + * {@link BaseComponent BaseComponents} to use in the translation + * @return the translatable component + */ + default TranslatableComponent asTranslatableComponent(Object... with) + { + return new TranslatableComponent( this, with ); + } +}