From 16298a75f29812e031e57beb8cc7fbc57ab0bb24 Mon Sep 17 00:00:00 2001 From: Parker Hawke Date: Fri, 10 Nov 2023 07:03:46 +1100 Subject: [PATCH] #3558: Add Translatable interface for fluid creation of TranslatableComponents --- .../bungee/api/chat/ComponentBuilder.java | 27 +++++++++++++ .../api/chat/TranslatableComponent.java | 15 ++++++++ .../bungee/api/chat/TranslationProvider.java | 38 +++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 chat/src/main/java/net/md_5/bungee/api/chat/TranslationProvider.java 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 2b02a49df..537a06620 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 ab0fda6a6..13c095646 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 000000000..926dd9db9 --- /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 ); + } +}