From cdd2fe6821215efcc4a2c6226e4e53c330907ee0 Mon Sep 17 00:00:00 2001 From: Riley Park Date: Sun, 15 Nov 2020 04:07:36 -0800 Subject: [PATCH] api: a few minor tweaks --- .../adventure/text/AbstractComponent.java | 7 +++--- .../adventure/text/TextReplacementConfig.java | 2 +- .../text/TextReplacementConfigImpl.java | 22 ++++--------------- 3 files changed, 8 insertions(+), 23 deletions(-) diff --git a/api/src/main/java/net/kyori/adventure/text/AbstractComponent.java b/api/src/main/java/net/kyori/adventure/text/AbstractComponent.java index e54c87b26..ca9b18d28 100644 --- a/api/src/main/java/net/kyori/adventure/text/AbstractComponent.java +++ b/api/src/main/java/net/kyori/adventure/text/AbstractComponent.java @@ -30,6 +30,7 @@ import java.util.function.Consumer; import java.util.stream.Stream; import net.kyori.adventure.text.format.Style; +import net.kyori.adventure.util.Buildable; import net.kyori.examination.ExaminableProperty; import net.kyori.examination.string.StringExaminer; import org.checkerframework.checker.nullness.qual.NonNull; @@ -95,9 +96,7 @@ protected AbstractComponent(final @NonNull List childre @Override public @NonNull Component replaceText(final @NonNull Consumer configurer) { requireNonNull(configurer, "configurer"); - final TextReplacementConfigImpl.Builder builder = new TextReplacementConfigImpl.Builder(); - configurer.accept(builder); - return TextReplacementRenderer.INSTANCE.render(this, builder.toState()); + return this.replaceText(Buildable.configureAndBuild(TextReplacementConfig.builder(), configurer)); } @Override @@ -106,7 +105,7 @@ protected AbstractComponent(final @NonNull List childre if(!(config instanceof TextReplacementConfigImpl)) { throw new IllegalArgumentException("Provided replacement was a custom TextReplacementConfig implementation, which is not supported."); } - return TextReplacementRenderer.INSTANCE.render(this, ((TextReplacementConfigImpl) config).toState()); + return TextReplacementRenderer.INSTANCE.render(this, ((TextReplacementConfigImpl) config).createState()); } @Override diff --git a/api/src/main/java/net/kyori/adventure/text/TextReplacementConfig.java b/api/src/main/java/net/kyori/adventure/text/TextReplacementConfig.java index 8681737e4..e0292d230 100644 --- a/api/src/main/java/net/kyori/adventure/text/TextReplacementConfig.java +++ b/api/src/main/java/net/kyori/adventure/text/TextReplacementConfig.java @@ -131,7 +131,7 @@ default Builder matchLiteral(final String literal) { * @return this builder * @since 4.2.0 */ - default @NonNull Builder times(int times) { + default @NonNull Builder times(final int times) { return this.condition((index, replaced) -> replaced < times ? PatternReplacementResult.REPLACE : PatternReplacementResult.STOP); } diff --git a/api/src/main/java/net/kyori/adventure/text/TextReplacementConfigImpl.java b/api/src/main/java/net/kyori/adventure/text/TextReplacementConfigImpl.java index 3d43244f1..bda2f6b85 100644 --- a/api/src/main/java/net/kyori/adventure/text/TextReplacementConfigImpl.java +++ b/api/src/main/java/net/kyori/adventure/text/TextReplacementConfigImpl.java @@ -52,7 +52,7 @@ final class TextReplacementConfigImpl implements TextReplacementConfig { return this.matchPattern; } - TextReplacementRenderer.State toState() { + TextReplacementRenderer.State createState() { return new TextReplacementRenderer.State(this.matchPattern, this.replacement, this.continuer); } @@ -77,7 +77,7 @@ public String toString() { static final class Builder implements TextReplacementConfig.Builder { @MonotonicNonNull Pattern matchPattern; - BiFunction replacement; + @MonotonicNonNull BiFunction replacement; IntFunction2 continuer = (index, replacement) -> PatternReplacementResult.REPLACE; Builder() { @@ -107,24 +107,10 @@ static final class Builder implements TextReplacementConfig.Builder { return this; } - private void validate() { - if(this.matchPattern == null) { - throw new IllegalStateException("A pattern must be provided to match against"); - } - if(this.replacement == null) { - throw new IllegalStateException("A replacement action must be provided"); - } - } - - TextReplacementRenderer.State toState() { - this.validate(); - return new TextReplacementRenderer.State(this.matchPattern, this.replacement, this.continuer); - } - @Override public TextReplacementConfig build() { - this.validate(); - + if(this.matchPattern == null) throw new IllegalStateException("A pattern must be provided to match against"); + if(this.replacement == null) throw new IllegalStateException("A replacement action must be provided"); return new TextReplacementConfigImpl(this); } }