From 39f0a874c7a04e1135863d585f135ae497ae8246 Mon Sep 17 00:00:00 2001 From: KingSimon <19822231+KingOfSquares@users.noreply.github.com> Date: Sun, 21 Nov 2021 22:42:35 +0100 Subject: [PATCH] Abstract out style methods to StyleWriter/Reader --- .../net/kyori/adventure/text/Component.java | 31 +++- .../adventure/text/ComponentBuilder.java | 19 +-- .../text/format/MutableStyleWriter.java | 74 +++++++++ .../kyori/adventure/text/format/Style.java | 28 ++-- .../adventure/text/format/StyleImpl.java | 25 --- .../adventure/text/format/StyleReader.java | 103 ++++++++++++ .../adventure/text/format/StyleWriter.java | 155 ++++++++++++++++++ 7 files changed, 378 insertions(+), 57 deletions(-) create mode 100644 api/src/main/java/net/kyori/adventure/text/format/MutableStyleWriter.java create mode 100644 api/src/main/java/net/kyori/adventure/text/format/StyleReader.java create mode 100644 api/src/main/java/net/kyori/adventure/text/format/StyleWriter.java 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 2d5f700885..1b4639f38f 100644 --- a/api/src/main/java/net/kyori/adventure/text/Component.java +++ b/api/src/main/java/net/kyori/adventure/text/Component.java @@ -43,6 +43,8 @@ import net.kyori.adventure.text.event.HoverEvent; import net.kyori.adventure.text.event.HoverEventSource; import net.kyori.adventure.text.format.Style; +import net.kyori.adventure.text.format.StyleReader; +import net.kyori.adventure.text.format.StyleWriter; import net.kyori.adventure.text.format.TextColor; import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.text.serializer.ComponentSerializer; @@ -101,7 +103,7 @@ * @since 4.0.0 */ @ApiStatus.NonExtendable -public interface Component extends ComponentBuilderApplicable, ComponentLike, Examinable, HoverEventSource { +public interface Component extends ComponentBuilderApplicable, ComponentLike, Examinable, HoverEventSource, StyleReader, StyleWriter { /** * A predicate that checks equality of two {@code Component}s using {@link Objects#equals(Object, Object)}. * @@ -1802,6 +1804,27 @@ default void detectCycle(final @NotNull Component that) { return this.style(this.style().merge(that.style(), merges)); } + /** + * Gets the font. + * + * @return the font of this component + * @since 4.10.0 + */ + default @Nullable Key font() { + return this.style().font(); + } + + /** + * Sets the font. + * + * @param key a font + * @return a component + * @since 4.10.0 + */ + default @NotNull Component font(final @Nullable Key key) { + return this.style(this.style().font(key)); + } + /** * Gets the color of this component. * @@ -1846,7 +1869,7 @@ default void detectCycle(final @NotNull Component that) { * @since 4.0.0 */ default boolean hasDecoration(final @NotNull TextDecoration decoration) { - return this.decoration(decoration) == TextDecoration.State.TRUE; + return StyleReader.super.hasDecoration(decoration); } /** @@ -1858,7 +1881,7 @@ default boolean hasDecoration(final @NotNull TextDecoration decoration) { */ @Contract(pure = true) default @NotNull Component decorate(final @NotNull TextDecoration decoration) { - return this.decoration(decoration, TextDecoration.State.TRUE); + return StyleWriter.super.decorate(decoration); } /** @@ -1885,7 +1908,7 @@ default boolean hasDecoration(final @NotNull TextDecoration decoration) { */ @Contract(pure = true) default @NotNull Component decoration(final @NotNull TextDecoration decoration, final boolean flag) { - return this.decoration(decoration, TextDecoration.State.byBoolean(flag)); + return StyleWriter.super.decoration(decoration, flag); } /** 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 a9e3b62938..0bc32c0792 100644 --- a/api/src/main/java/net/kyori/adventure/text/ComponentBuilder.java +++ b/api/src/main/java/net/kyori/adventure/text/ComponentBuilder.java @@ -31,10 +31,12 @@ import net.kyori.adventure.key.Key; import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.event.HoverEventSource; +import net.kyori.adventure.text.format.MutableStyleWriter; import net.kyori.adventure.text.format.Style; import net.kyori.adventure.text.format.TextColor; import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.util.Buildable; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -46,7 +48,8 @@ * @param the builder type * @since 4.0.0 */ -public interface ComponentBuilder, B extends ComponentBuilder> extends Buildable.Builder, ComponentBuilderApplicable, ComponentLike { +@ApiStatus.NonExtendable +public interface ComponentBuilder, B extends ComponentBuilder> extends Buildable.Builder, ComponentBuilderApplicable, ComponentLike, MutableStyleWriter { /** * Appends a component to this component. * @@ -227,9 +230,7 @@ public interface ComponentBuilder, B extends @Contract("_, _ -> this") @SuppressWarnings("unchecked") default @NotNull B decorations(final @NotNull Set decorations, final boolean flag) { - final TextDecoration.State state = TextDecoration.State.byBoolean(flag); - decorations.forEach(decoration -> this.decoration(decoration, state)); - return (B) this; + return MutableStyleWriter.super.decorations(decorations, flag); } /** @@ -254,10 +255,7 @@ public interface ComponentBuilder, B extends @Contract("_ -> this") @SuppressWarnings("unchecked") default @NotNull B decorate(final @NotNull TextDecoration@NotNull... decorations) { - for (int i = 0, length = decorations.length; i < length; i++) { - this.decorate(decorations[i]); - } - return (B) this; + return MutableStyleWriter.super.decorate(decorations); } /** @@ -286,10 +284,7 @@ public interface ComponentBuilder, B extends @Contract("_ -> this") @SuppressWarnings("unchecked") default @NotNull B decorations(final @NotNull Map decorations) { - for (final Map.Entry entry : decorations.entrySet()) { - this.decoration(entry.getKey(), entry.getValue()); - } - return (B) this; + return MutableStyleWriter.super.decorations(decorations); } /** diff --git a/api/src/main/java/net/kyori/adventure/text/format/MutableStyleWriter.java b/api/src/main/java/net/kyori/adventure/text/format/MutableStyleWriter.java new file mode 100644 index 0000000000..e03d021508 --- /dev/null +++ b/api/src/main/java/net/kyori/adventure/text/format/MutableStyleWriter.java @@ -0,0 +1,74 @@ +package net.kyori.adventure.text.format; + +import java.util.Map; +import java.util.Set; + +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; + +/** + * Writes style properties to a mutable object. Used to override some default methods from {@link net.kyori.adventure.text.format.StyleWriter} + * with faster alternatives that only work for mutable objects. + * + * @param The type implementing this interface e.g. {@link net.kyori.adventure.text.Component} + * @see net.kyori.adventure.text.format.StyleWriter + * @since 4.10.0 + */ +@ApiStatus.NonExtendable +public interface MutableStyleWriter> extends StyleWriter { + + /** + * Sets {@code decorations} to {@link TextDecoration.State#TRUE}. + * + * @param decorations the decorations + * @return a mutable object ({@code T}) + * @since 4.10.0 + */ + @Override + @Contract("_ -> this") + @SuppressWarnings("unchecked") + default @NotNull T decorate(final @NotNull TextDecoration@NotNull... decorations) { + for (int i = 0, length = decorations.length; i < length; i++) { + this.decorate(decorations[i]); + } + return (T) this; + } + + /** + * Sets decorations using the specified {@code decorations} map. + * + *

If a given decoration does not have a value explicitly set, the value of that particular decoration is not changed.

+ * + * @param decorations a map containing text decorations and their respective state. + * @return a mutable object ({@code T}) + * @since 4.10.0 + */ + @Override + @Contract("_ -> this") + @SuppressWarnings("unchecked") + default @NotNull T decorations(final @NotNull Map decorations) { + for (final Map.Entry entry : decorations.entrySet()) { + this.decoration(entry.getKey(), entry.getValue()); + } + return (T) this; + } + + /** + * Sets the state of a set of decorations to {@code flag}. + * + * @param decorations the decorations + * @param flag {@code true} if this mutable object should have the decorations, {@code false} if + * this mutable object should not have the decorations + * @return a mutable object ({@code T}) + * @since 4.10.0 + */ + @Override + @Contract("_, _ -> this") + @SuppressWarnings("unchecked") + default @NotNull T decorations(final @NotNull Set decorations, final boolean flag) { + final TextDecoration.State state = TextDecoration.State.byBoolean(flag); + decorations.forEach(decoration -> this.decoration(decoration, state)); + return (T) this; + } +} 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 702a0d6d30..672795edf6 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 @@ -60,7 +60,7 @@ * @since 4.0.0 */ @ApiStatus.NonExtendable -public interface Style extends Buildable, Examinable { +public interface Style extends Buildable, Examinable, StyleReader, StyleWriter