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 b09ae19c5..af094165f 100644 --- a/api/src/main/java/net/kyori/adventure/text/Component.java +++ b/api/src/main/java/net/kyori/adventure/text/Component.java @@ -1225,6 +1225,25 @@ public interface Component extends ComponentBuilderApplicable, ComponentLike, Ex return text(String.valueOf(value), color, decorations); } + /* + * -------------------------- + * ---- VirtualComponent ---- + * -------------------------- + */ + + /** + * Creates a virtual component with a value. + * + * @param virtual the value + * @return a virtual component + * @since 4.13.0 + */ + @Contract(value = "_ -> new", pure = true) + static @NotNull VirtualComponent virtual(final @NotNull VirtualComponentHolder> virtual) { + requireNonNull(virtual, "virtual"); + return VirtualComponentImpl.createVirtual(virtual); + } + /* * ------------------------------- * ---- TranslatableComponent ---- diff --git a/api/src/main/java/net/kyori/adventure/text/TextComponentImpl.java b/api/src/main/java/net/kyori/adventure/text/TextComponentImpl.java index f29847603..231521d1a 100644 --- a/api/src/main/java/net/kyori/adventure/text/TextComponentImpl.java +++ b/api/src/main/java/net/kyori/adventure/text/TextComponentImpl.java @@ -36,7 +36,7 @@ import static java.util.Objects.requireNonNull; -final class TextComponentImpl extends AbstractComponent implements TextComponent { +class TextComponentImpl extends AbstractComponent implements TextComponent { private static final boolean WARN_WHEN_LEGACY_FORMATTING_DETECTED = Boolean.TRUE.equals(AdventureProperties.TEXT_WARN_WHEN_LEGACY_FORMATTING_DETECTED.value()); @VisibleForTesting static final char SECTION_CHAR = 'ยง'; @@ -56,6 +56,10 @@ static TextComponent create(final @NotNull List extends ComponentLike> childre ); } + TextComponent create0(final @NotNull List extends ComponentLike> children, final @NotNull Style style, final @NotNull String content) { + return create(children, style, content); + } + private static @NotNull TextComponent createDirect(final @NotNull String content) { return new TextComponentImpl(Collections.emptyList(), Style.empty(), content); } @@ -90,17 +94,17 @@ static TextComponent create(final @NotNull List extends ComponentLike> childre @Override public @NotNull TextComponent content(final @NotNull String content) { if (Objects.equals(this.content, content)) return this; - return create(this.children, this.style, content); + return this.create0(this.children, this.style, content); } @Override public @NotNull TextComponent children(final @NotNull List extends ComponentLike> children) { - return create(children, this.style, this.content); + return this.create0(children, this.style, this.content); } @Override public @NotNull TextComponent style(final @NotNull Style style) { - return create(this.children, style, this.content); + return this.create0(this.children, style, this.content); } @Override diff --git a/api/src/main/java/net/kyori/adventure/text/VirtualComponent.java b/api/src/main/java/net/kyori/adventure/text/VirtualComponent.java new file mode 100644 index 000000000..d894ba6a7 --- /dev/null +++ b/api/src/main/java/net/kyori/adventure/text/VirtualComponent.java @@ -0,0 +1,43 @@ +/* + * This file is part of adventure, licensed under the MIT License. + * + * Copyright (c) 2017-2022 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.text; + +import org.jetbrains.annotations.NotNull; + +/** + * A virtual component. + * + *
This component type is transient, and not guaranteed to survive during any sort of transformations or serialization.
+ * + * @since 4.13.0 + */ +public interface VirtualComponent extends TextComponent { + /** + * Gets the virtual value holder. + * + * @return the virtual value holder + * @since 4.13.0 + */ + @NotNull VirtualComponentHolder> holder(); +} diff --git a/api/src/main/java/net/kyori/adventure/text/VirtualComponentHolder.java b/api/src/main/java/net/kyori/adventure/text/VirtualComponentHolder.java new file mode 100644 index 000000000..a27ecd7f6 --- /dev/null +++ b/api/src/main/java/net/kyori/adventure/text/VirtualComponentHolder.java @@ -0,0 +1,42 @@ +/* + * This file is part of adventure, licensed under the MIT License. + * + * Copyright (c) 2017-2022 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.text; + +import org.jetbrains.annotations.UnknownNullability; + +/** + * A holder for a value. + * + * @param