Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract out style methods to StyleWriter/Reader #501

Merged
merged 3 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions api/src/main/java/net/kyori/adventure/text/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.StyleGetter;
import net.kyori.adventure.text.format.StyleSetter;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.serializer.ComponentSerializer;
Expand Down Expand Up @@ -101,7 +103,7 @@
* @since 4.0.0
*/
@ApiStatus.NonExtendable
public interface Component extends ComponentBuilderApplicable, ComponentLike, Examinable, HoverEventSource<Component> {
public interface Component extends ComponentBuilderApplicable, ComponentLike, Examinable, HoverEventSource<Component>, StyleGetter, StyleSetter<Component> {
/**
* A predicate that checks equality of two {@code Component}s using {@link Objects#equals(Object, Object)}.
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 StyleGetter.super.hasDecoration(decoration);
}

/**
Expand All @@ -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 StyleSetter.super.decorate(decoration);
}

/**
Expand All @@ -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 StyleSetter.super.decoration(decoration, flag);
}

/**
Expand Down
19 changes: 7 additions & 12 deletions api/src/main/java/net/kyori/adventure/text/ComponentBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.MutableStyleSetter;
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;
Expand All @@ -46,7 +48,8 @@
* @param <B> the builder type
* @since 4.0.0
*/
public interface ComponentBuilder<C extends BuildableComponent<C, B>, B extends ComponentBuilder<C, B>> extends Buildable.Builder<C>, ComponentBuilderApplicable, ComponentLike {
@ApiStatus.NonExtendable
public interface ComponentBuilder<C extends BuildableComponent<C, B>, B extends ComponentBuilder<C, B>> extends Buildable.Builder<C>, ComponentBuilderApplicable, ComponentLike, MutableStyleSetter<B> {
/**
* Appends a component to this component.
*
Expand Down Expand Up @@ -227,9 +230,7 @@ public interface ComponentBuilder<C extends BuildableComponent<C, B>, B extends
@Contract("_, _ -> this")
@SuppressWarnings("unchecked")
default @NotNull B decorations(final @NotNull Set<TextDecoration> decorations, final boolean flag) {
final TextDecoration.State state = TextDecoration.State.byBoolean(flag);
decorations.forEach(decoration -> this.decoration(decoration, state));
return (B) this;
return MutableStyleSetter.super.decorations(decorations, flag);
}

/**
Expand All @@ -254,10 +255,7 @@ public interface ComponentBuilder<C extends BuildableComponent<C, B>, 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 MutableStyleSetter.super.decorate(decorations);
}

/**
Expand Down Expand Up @@ -286,10 +284,7 @@ public interface ComponentBuilder<C extends BuildableComponent<C, B>, B extends
@Contract("_ -> this")
@SuppressWarnings("unchecked")
default @NotNull B decorations(final @NotNull Map<TextDecoration, TextDecoration.State> decorations) {
for (final Map.Entry<TextDecoration, TextDecoration.State> entry : decorations.entrySet()) {
this.decoration(entry.getKey(), entry.getValue());
}
return (B) this;
return MutableStyleSetter.super.decorations(decorations);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* 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.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;

import static java.util.Objects.requireNonNull;

/**
* Writes style properties to a mutable object. Used to override some default methods from {@link net.kyori.adventure.text.format.StyleSetter}
* with faster alternatives that only work for mutable objects.
*
* @param <T> The type implementing this interface e.g. {@link net.kyori.adventure.text.Component}
* @see net.kyori.adventure.text.format.StyleSetter
* @since 4.10.0
*/
@ApiStatus.NonExtendable
public interface MutableStyleSetter<T extends MutableStyleSetter<?>> extends StyleSetter<T> {

/**
* 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.
*
* <p>If a given decoration does not have a value explicitly set, the value of that particular decoration is not changed.</p>
*
* @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<TextDecoration, TextDecoration.State> decorations) {
requireNonNull(decorations, "decorations");
for (final Map.Entry<TextDecoration, TextDecoration.State> 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<TextDecoration> decorations, final boolean flag) {
final TextDecoration.State state = TextDecoration.State.byBoolean(flag);
decorations.forEach(decoration -> this.decoration(decoration, state));
return (T) this;
}
}
Loading