From db91520abe5a1db32928ff7c28ed80a16dd5c11e Mon Sep 17 00:00:00 2001 From: Bjarne Koll Date: Mon, 13 Sep 2021 22:54:45 +0200 Subject: [PATCH] Add decorations(Map) to component/style builders This commit introduces a new decorations method to the component and style builderthat takes in a map of text decorations and their state. This methodalready exists on the Component interface but was previously missing on the respective builders. As the style builder stores the decorations in individual fields and hence cannot natively work with the map layout, the method was implemented as a default method on the interface and simply delegates the map entriess to the normal decorate methods. The main motivation for the addition of this method is the easy application of a preset map of decorations. --- .../adventure/text/ComponentBuilder.java | 19 +++++++++++++++++++ .../kyori/adventure/text/format/Style.java | 17 +++++++++++++++++ 2 files changed, 36 insertions(+) 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 ea3520454..a9e3b6293 100644 --- a/api/src/main/java/net/kyori/adventure/text/ComponentBuilder.java +++ b/api/src/main/java/net/kyori/adventure/text/ComponentBuilder.java @@ -24,6 +24,7 @@ package net.kyori.adventure.text; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.function.Consumer; import java.util.function.Function; @@ -273,6 +274,24 @@ public interface ComponentBuilder, B extends return this.decoration(decoration, TextDecoration.State.byBoolean(flag)); } + /** + * Sets decorations for this component's style 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 this builder + * @since 4.10.0 + */ + @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; + } + /** * Sets the value of a decoration on 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 56c488630..09ffd8dc6 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 @@ -689,6 +689,23 @@ interface Builder extends Buildable.Builder