diff --git a/api/src/main/java/net/kyori/adventure/text/Component.java b/api/src/main/java/net/kyori/adventure/text/Component.java index 8c85c3c89..90db3633f 100644 --- a/api/src/main/java/net/kyori/adventure/text/Component.java +++ b/api/src/main/java/net/kyori/adventure/text/Component.java @@ -28,6 +28,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import java.util.function.Consumer; import java.util.function.Function; @@ -42,6 +43,7 @@ import net.kyori.adventure.text.format.TextColor; import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.text.serializer.ComponentSerializer; +import net.kyori.adventure.translation.Translatable; import net.kyori.adventure.util.Buildable; import net.kyori.adventure.util.IntFunction2; import net.kyori.examination.Examinable; @@ -1081,6 +1083,18 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex return translatable(key, Style.empty()); } + /** + * Creates a translatable component with a translation key. + * + * @param translatable the translatable object to get the key from + * @return a translatable component + * @since 4.8.0 + */ + @Contract(value = "_ -> new", pure = true) + static @NonNull TranslatableComponent translatable(final @NonNull Translatable translatable) { + return translatable(Objects.requireNonNull(translatable, "translatable").translationKey(), Style.empty()); + } + /** * Creates a translatable component with a translation key and styling. * @@ -1094,6 +1108,19 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex return new TranslatableComponentImpl(Collections.emptyList(), style, key, Collections.emptyList()); } + /** + * Creates a translatable component with a translation key and styling. + * + * @param translatable the translatable object to get the key from + * @param style the style + * @return a translatable component + * @since 4.8.0 + */ + @Contract(value = "_, _ -> new", pure = true) + static @NonNull TranslatableComponent translatable(final @NonNull Translatable translatable, final @NonNull Style style) { + return translatable(Objects.requireNonNull(translatable, "translatable").translationKey(), style); + } + /** * Creates a translatable component with a translation key, and optional color. * @@ -1107,6 +1134,19 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex return translatable(key, Style.style(color)); } + /** + * Creates a translatable component with a translation key, and optional color. + * + * @param translatable the translatable object to get the key from + * @param color the color + * @return a translatable component + * @since 4.8.0 + */ + @Contract(value = "_, _ -> new", pure = true) + static @NonNull TranslatableComponent translatable(final @NonNull Translatable translatable, final @Nullable TextColor color) { + return translatable(Objects.requireNonNull(translatable, "translatable").translationKey(), color); + } + /** * Creates a translatable component with a translation key, and optional color and decorations. * @@ -1121,6 +1161,20 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex return translatable(key, Style.style(color, decorations)); } + /** + * Creates a translatable component with a translation key, and optional color and decorations. + * + * @param translatable the translatable object to get the key from + * @param color the color + * @param decorations the decorations + * @return a translatable component + * @since 4.8.0 + */ + @Contract(value = "_, _, _ -> new", pure = true) + static @NonNull TranslatableComponent translatable(final @NonNull Translatable translatable, final @Nullable TextColor color, final TextDecoration@NonNull... decorations) { + return translatable(Objects.requireNonNull(translatable, "translatable").translationKey(), color, decorations); + } + /** * Creates a translatable component with a translation key, and optional color and decorations. * @@ -1135,6 +1189,20 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex return translatable(key, Style.style(color, decorations)); } + /** + * Creates a translatable component with a translation key, and optional color and decorations. + * + * @param translatable the translatable object to get the key from + * @param color the color + * @param decorations the decorations + * @return a translatable component + * @since 4.8.0 + */ + @Contract(value = "_, _, _ -> new", pure = true) + static @NonNull TranslatableComponent translatable(final @NonNull Translatable translatable, final @Nullable TextColor color, final @NonNull Set decorations) { + return translatable(Objects.requireNonNull(translatable, "translatable").translationKey(), color, decorations); + } + /** * Creates a translatable component with a translation key and arguments. * @@ -1148,6 +1216,19 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex return translatable(key, Style.empty(), args); } + /** + * Creates a translatable component with a translation key and arguments. + * + * @param translatable the translatable object to get the key from + * @param args the translation arguments + * @return a translatable component + * @since 4.8.0 + */ + @Contract(value = "_, _ -> new", pure = true) + static @NonNull TranslatableComponent translatable(final @NonNull Translatable translatable, final @NonNull ComponentLike@NonNull... args) { + return translatable(Objects.requireNonNull(translatable, "translatable").translationKey(), args); + } + /** * Creates a translatable component with a translation key and styling. * @@ -1162,6 +1243,20 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex return new TranslatableComponentImpl(Collections.emptyList(), style, key, args); } + /** + * Creates a translatable component with a translation key and styling. + * + * @param translatable the translatable object to get the key from + * @param style the style + * @param args the translation arguments + * @return a translatable component + * @since 4.8.0 + */ + @Contract(value = "_, _, _ -> new", pure = true) + static @NonNull TranslatableComponent translatable(final @NonNull Translatable translatable, final @NonNull Style style, final @NonNull ComponentLike@NonNull... args) { + return translatable(Objects.requireNonNull(translatable, "translatable").translationKey(), style, args); + } + /** * Creates a translatable component with a translation key, arguments, and optional color. * @@ -1176,6 +1271,20 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex return translatable(key, Style.style(color), args); } + /** + * Creates a translatable component with a translation key, arguments, and optional color. + * + * @param translatable the translatable object to get the key from + * @param color the color + * @param args the translation arguments + * @return a translatable component + * @since 4.8.0 + */ + @Contract(value = "_, _, _ -> new", pure = true) + static @NonNull TranslatableComponent translatable(final @NonNull Translatable translatable, final @Nullable TextColor color, final @NonNull ComponentLike@NonNull... args) { + return translatable(Objects.requireNonNull(translatable, "translatable").translationKey(), color, args); + } + /** * Creates a translatable component with a translation key, arguments, and optional color and decorations. * @@ -1191,6 +1300,21 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex return translatable(key, Style.style(color, decorations), args); } + /** + * Creates a translatable component with a translation key, arguments, and optional color and decorations. + * + * @param translatable the translatable object to get the key from + * @param color the color + * @param decorations the decorations + * @param args the translation arguments + * @return a translatable component + * @since 4.8.0 + */ + @Contract(value = "_, _, _, _ -> new", pure = true) + static @NonNull TranslatableComponent translatable(final @NonNull Translatable translatable, final @Nullable TextColor color, final @NonNull Set decorations, final @NonNull ComponentLike@NonNull... args) { + return translatable(Objects.requireNonNull(translatable, "translatable").translationKey(), color, decorations, args); + } + /** * Creates a translatable component with a translation key and arguments. * @@ -1204,6 +1328,19 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex return new TranslatableComponentImpl(Collections.emptyList(), Style.empty(), key, args); } + /** + * Creates a translatable component with a translation key and arguments. + * + * @param translatable the translatable object to get the key from + * @param args the translation arguments + * @return a translatable component + * @since 4.8.0 + */ + @Contract(value = "_, _ -> new", pure = true) + static @NonNull TranslatableComponent translatable(final @NonNull Translatable translatable, final @NonNull List args) { + return translatable(Objects.requireNonNull(translatable, "translatable").translationKey(), args); + } + /** * Creates a translatable component with a translation key and styling. * @@ -1218,6 +1355,20 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex return new TranslatableComponentImpl(Collections.emptyList(), style, key, args); } + /** + * Creates a translatable component with a translation key and styling. + * + * @param translatable the translatable object to get the key from + * @param style the style + * @param args the translation arguments + * @return a translatable component + * @since 4.8.0 + */ + @Contract(value = "_, _, _ -> new", pure = true) + static @NonNull TranslatableComponent translatable(final @NonNull Translatable translatable, final @NonNull Style style, final @NonNull List args) { + return translatable(Objects.requireNonNull(translatable, "translatable").translationKey(), style, args); + } + /** * Creates a translatable component with a translation key, arguments, and optional color. * @@ -1232,6 +1383,20 @@ static TranslatableComponent translatable(final @NonNull String key, final @Null return translatable(key, Style.style(color), args); } + /** + * Creates a translatable component with a translation key, arguments, and optional color. + * + * @param translatable the translatable object to get the key from + * @param color the color + * @param args the translation arguments + * @return a translatable component + * @since 4.8.0 + */ + @Contract(value = "_, _, _ -> new", pure = true) + static TranslatableComponent translatable(final @NonNull Translatable translatable, final @Nullable TextColor color, final @NonNull List args) { + return translatable(Objects.requireNonNull(translatable, "translatable").translationKey(), color, args); + } + /** * Creates a translatable component with a translation key, arguments, and optional color and decorations. * @@ -1247,6 +1412,21 @@ static TranslatableComponent translatable(final @NonNull String key, final @Null return translatable(key, Style.style(color, decorations), args); } + /** + * Creates a translatable component with a translation key, arguments, and optional color and decorations. + * + * @param translatable the translatable object to get the key from + * @param color the color + * @param decorations the decorations + * @param args the translation arguments + * @return a translatable component + * @since 4.8.0 + */ + @Contract(value = "_, _, _, _ -> new", pure = true) + static @NonNull TranslatableComponent translatable(final @NonNull Translatable translatable, final @Nullable TextColor color, final @NonNull Set decorations, final @NonNull List args) { + return translatable(Objects.requireNonNull(translatable, "translatable").translationKey(), color, decorations, args); + } + /** * Gets the unmodifiable list of children. * diff --git a/api/src/main/java/net/kyori/adventure/text/TranslatableComponent.java b/api/src/main/java/net/kyori/adventure/text/TranslatableComponent.java index 399b95987..08bd6a387 100644 --- a/api/src/main/java/net/kyori/adventure/text/TranslatableComponent.java +++ b/api/src/main/java/net/kyori/adventure/text/TranslatableComponent.java @@ -25,8 +25,10 @@ import java.util.List; import java.util.Locale; +import java.util.Objects; import net.kyori.adventure.audience.Audience; import net.kyori.adventure.translation.GlobalTranslator; +import net.kyori.adventure.translation.Translatable; import net.kyori.adventure.translation.TranslationRegistry; import org.checkerframework.checker.nullness.qual.NonNull; import org.jetbrains.annotations.Contract; @@ -65,6 +67,18 @@ public interface TranslatableComponent extends BuildableComponent { + /** + * Sets the translation key. + * + * @param translatable the translatable object to get the key from + * @return this builder + * @since 4.8.0 + */ + @Contract(pure = true) + default @NonNull Builder key(final @NonNull Translatable translatable) { + return this.key(Objects.requireNonNull(translatable, "translatable").translationKey()); + } + /** * Sets the translation key. * diff --git a/api/src/main/java/net/kyori/adventure/translation/Translatable.java b/api/src/main/java/net/kyori/adventure/translation/Translatable.java new file mode 100644 index 000000000..cae8f37fc --- /dev/null +++ b/api/src/main/java/net/kyori/adventure/translation/Translatable.java @@ -0,0 +1,41 @@ +/* + * This file is part of adventure, licensed under the MIT License. + * + * Copyright (c) 2017-2021 KyoriPowered + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package net.kyori.adventure.translation; + +import org.checkerframework.checker.nullness.qual.NonNull; + +/** + * Something that has a translation key. + * + * @since 4.8.0 + */ +public interface Translatable { + /** + * Gets the translation key. + * + * @return the translation key + * @since 4.8.0 + */ + @NonNull String translationKey(); +}