From 4e5fe81af9fac69a7bbd2548052c14aeab9e03f8 Mon Sep 17 00:00:00 2001 From: KingSimon <19822231+KingOfSquares@users.noreply.github.com> Date: Sat, 11 Jun 2022 18:34:49 +0200 Subject: [PATCH 1/3] promote decorationIfAbsent to StyleSetter and public API --- .../text/AbstractComponentBuilder.java | 11 +++++++++ .../net/kyori/adventure/text/Component.java | 20 ++++++++++++++++ .../adventure/text/ComponentBuilder.java | 13 ++++++++++ .../kyori/adventure/text/format/Style.java | 24 +++++++++++++++++++ .../adventure/text/format/StyleImpl.java | 16 +++++++++++-- .../adventure/text/format/StyleSetter.java | 10 ++++++++ 6 files changed, 92 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/net/kyori/adventure/text/AbstractComponentBuilder.java b/api/src/main/java/net/kyori/adventure/text/AbstractComponentBuilder.java index 93f78a058..c73f48062 100644 --- a/api/src/main/java/net/kyori/adventure/text/AbstractComponentBuilder.java +++ b/api/src/main/java/net/kyori/adventure/text/AbstractComponentBuilder.java @@ -243,6 +243,17 @@ private void prepareChildren() { return (B) this; } + @Override + @SuppressWarnings("unchecked") + public @NotNull B decorationIfAbsent(final @NotNull TextDecoration decoration, final TextDecoration.@NotNull State state) { + requireNonNull(state, "state"); + final TextDecoration.@NotNull State thisState = this.buildStyle().decoration(decoration); + if (thisState == TextDecoration.State.NOT_SET) { + this.styleBuilder().decoration(decoration, state); + } + return (B) this; + } + @Override @SuppressWarnings("unchecked") public @NotNull B clickEvent(final @Nullable ClickEvent event) { 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 1f8419863..9761cccc8 100644 --- a/api/src/main/java/net/kyori/adventure/text/Component.java +++ b/api/src/main/java/net/kyori/adventure/text/Component.java @@ -2014,6 +2014,26 @@ default boolean hasDecoration(final @NotNull TextDecoration decoration) { return this.style(this.style().decorations(decorations)); } + /** + * Sets the state of a decoration on this component to {@code state} if the current state of + * the decoration is {@link TextDecoration.State#NOT_SET}. + * + * @param decoration the decoration + * @param state the state + * @return a component + * @since 4.12.0 + */ + @Override + default @NotNull Component decorationIfAbsent(final @NotNull TextDecoration decoration, @NotNull final TextDecoration.State state) { + requireNonNull(state, "state"); + //Not delegating this method prevents object creation if decoration is absent + final TextDecoration.@NotNull State thisState = this.decoration(decoration); + if (thisState == TextDecoration.State.NOT_SET) { + return this.style(this.style().decoration(decoration, state)); + } + return this; + } + /** * Gets the click event of this component. * diff --git a/api/src/main/java/net/kyori/adventure/text/ComponentBuilder.java b/api/src/main/java/net/kyori/adventure/text/ComponentBuilder.java index c32396098..04047e784 100644 --- a/api/src/main/java/net/kyori/adventure/text/ComponentBuilder.java +++ b/api/src/main/java/net/kyori/adventure/text/ComponentBuilder.java @@ -308,6 +308,19 @@ public interface ComponentBuilder, B extends @Override @NotNull B decoration(final @NotNull TextDecoration decoration, final TextDecoration.@NotNull State state); + /** + * Sets the state of a decoration on this component to {@code state} if the current state of + * the decoration is {@link TextDecoration.State#NOT_SET}. + * + * @param decoration the decoration + * @param state the state + * @return this builder + * @since 4.12.0 + */ + @Contract("_, _ -> this") + @Override + @NotNull B decorationIfAbsent(final @NotNull TextDecoration decoration, @NotNull final TextDecoration.State state); + /** * Sets the click event of this component. * diff --git a/api/src/main/java/net/kyori/adventure/text/format/Style.java b/api/src/main/java/net/kyori/adventure/text/format/Style.java index 34163f6b9..024d04bd5 100644 --- a/api/src/main/java/net/kyori/adventure/text/format/Style.java +++ b/api/src/main/java/net/kyori/adventure/text/format/Style.java @@ -364,6 +364,18 @@ default boolean hasDecoration(final @NotNull TextDecoration decoration) { @Override @NotNull Style decorations(final @NotNull Map decorations); + /** + * Sets the state of a decoration on this style to {@code state} if the current state of + * the decoration is {@link TextDecoration.State#NOT_SET}. + * + * @param decoration the decoration + * @param state the state + * @return a style + * @since 4.12.0 + */ + @Override + @NotNull Style decorationIfAbsent(final @NotNull TextDecoration decoration, @NotNull final TextDecoration.State state); + /** * Gets the click event. * @@ -761,6 +773,18 @@ interface Builder extends AbstractBuilder