From 51ea6be94a9eae8e595795fdcd47c54e308e319e Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Wed, 3 Sep 2025 02:03:06 +0200 Subject: [PATCH] Makes the format builder more flexible Adds new no-argument constructor Adds methods for constructing, appending and replacing with string builders Adds ability to use translatable messages as placeholders Adds missing handling of an uninitialized string formatter for some methods --- .../knarlib/formatting/FormatBuilder.java | 111 ++++++++++++++++-- 1 file changed, 104 insertions(+), 7 deletions(-) diff --git a/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java b/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java index 8230ba5..1cdc440 100644 --- a/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java +++ b/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java @@ -2,6 +2,7 @@ package net.knarcraft.knarlib.formatting; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.List; @@ -11,25 +12,36 @@ import java.util.List; @SuppressWarnings("unused") public final class FormatBuilder { - private static StringFormatter stringFormatter; + private static @Nullable StringFormatter stringFormatter = null; private static final IllegalStateException NOT_SETUP_EXCEPTION = new IllegalStateException("String formatter has not been set!"); private @NotNull String toFormat; /** - * Sets the string formatter to use for translation and formatting - * - * @param stringFormatter

The string formatter to use

+ * Instantiates a new format builder */ - public static void setStringFormatter(@NotNull StringFormatter stringFormatter) { - FormatBuilder.stringFormatter = stringFormatter; + public FormatBuilder() { + this.toFormat = ""; + } + + /** + * Instantiates a new format builder + * + * @param stringBuilder

The string builder to use output from

+ */ + public FormatBuilder(@NotNull StringBuilder stringBuilder) { + this.toFormat = stringBuilder.toString(); } /** * Instantiates a new format builder * * @param translatableMessage

The translatable message to format

+ * @throws IllegalStateException

If the string formatter has not been set

*/ public FormatBuilder(@NotNull TranslatableMessage translatableMessage) { + if (FormatBuilder.stringFormatter == null) { + throw NOT_SETUP_EXCEPTION; + } this.toFormat = stringFormatter.getUnFormattedMessage(translatableMessage); } @@ -42,6 +54,15 @@ public final class FormatBuilder { this.toFormat = toFormat; } + /** + * Sets the string formatter to use for translation and formatting + * + * @param stringFormatter

The string formatter to use

+ */ + public static void setStringFormatter(@NotNull StringFormatter stringFormatter) { + FormatBuilder.stringFormatter = stringFormatter; + } + /** * Replaces placeholders for the current string * @@ -68,20 +89,74 @@ public final class FormatBuilder { return this; } + /** + * Replaces placeholders for the current string + * + * @param placeholder

The translatable message to replace

+ * @param replacement

The replacement translatable message

+ * @return

This format builder

+ * @throws IllegalStateException

If the string formatter has not been set

+ */ + @NotNull + public FormatBuilder replace(@NotNull TranslatableMessage placeholder, + @NotNull TranslatableMessage replacement) { + if (FormatBuilder.stringFormatter == null) { + throw NOT_SETUP_EXCEPTION; + } + this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, + stringFormatter.getUnFormattedMessage(placeholder), stringFormatter.getUnFormattedMessage(replacement)); + return this; + } + /** * Replaces placeholders for the current string * * @param placeholder

The placeholder to replace

* @param replacement

The replacement translatable message

* @return

This format builder

+ * @throws IllegalStateException

If the string formatter has not been set

*/ @NotNull public FormatBuilder replace(@NotNull String placeholder, @NotNull TranslatableMessage replacement) { + if (FormatBuilder.stringFormatter == null) { + throw NOT_SETUP_EXCEPTION; + } this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, placeholder, stringFormatter.getUnFormattedMessage(replacement)); return this; } + /** + * Replaces placeholders for the current string + * + * @param placeholder

The translatable message placeholder to replace

+ * @param replacement

The replacement string

+ * @return

This format builder

+ * @throws IllegalStateException

If the string formatter has not been set

+ */ + @NotNull + public FormatBuilder replace(@NotNull TranslatableMessage placeholder, @NotNull String replacement) { + if (FormatBuilder.stringFormatter == null) { + throw NOT_SETUP_EXCEPTION; + } + this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, + stringFormatter.getUnFormattedMessage(placeholder), replacement); + return this; + } + + /** + * Replaces placeholders for the current string + * + * @param placeholder

The placeholder to replace

+ * @param stringBuilder

The replacement string builder

+ * @return

This format builder

+ */ + @NotNull + public FormatBuilder replace(@NotNull String placeholder, @NotNull StringBuilder stringBuilder) { + this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, placeholder, stringBuilder.toString()); + return this; + } + /** * Replaces placeholders for the current string * @@ -114,9 +189,13 @@ public final class FormatBuilder { * the message through other means.

* * @return

This format builder

+ * @throws IllegalStateException

If the string formatter has not been set

*/ @NotNull public FormatBuilder color() { + if (FormatBuilder.stringFormatter == null) { + throw NOT_SETUP_EXCEPTION; + } this.toFormat = stringFormatter.getUnFormattedColoredMessage(this.toFormat); return this; } @@ -138,20 +217,35 @@ public final class FormatBuilder { * * @param translatableMessage

The translatable message to append

* @return

This format builder

+ * @throws IllegalStateException

If the string formatter has not been set

*/ @NotNull public FormatBuilder append(@NotNull TranslatableMessage translatableMessage) { + if (FormatBuilder.stringFormatter == null) { + throw NOT_SETUP_EXCEPTION; + } this.toFormat += stringFormatter.getUnFormattedMessage(translatableMessage); return this; } + /** + * Appends the given string builder's output to this format builder + * + * @param stringBuilder

The string builder to append

+ * @return

This format builder

+ */ + public FormatBuilder append(@NotNull StringBuilder stringBuilder) { + this.toFormat += stringBuilder.toString(); + return this; + } + /** * Builds the output of the performed replacements and formatting * * @return

The result

*/ @NotNull - public String build() { + public String toString() { return this.toFormat; } @@ -159,6 +253,7 @@ public final class FormatBuilder { * Displays the result to the specified command sender as a success message * * @param commandSender

The recipient

+ * @throws IllegalStateException

If the string formatter has not been set

*/ public void success(@NotNull CommandSender commandSender) { if (FormatBuilder.stringFormatter == null) { @@ -171,6 +266,7 @@ public final class FormatBuilder { * Displays the result to the specified command sender as an error * * @param commandSender

The recipient

+ * @throws IllegalStateException

If the string formatter has not been set

*/ public void error(@NotNull CommandSender commandSender) { if (FormatBuilder.stringFormatter == null) { @@ -183,6 +279,7 @@ public final class FormatBuilder { * Displays the result to the specified command sender * * @param commandSender

The recipient

+ * @throws IllegalStateException

If the string formatter has not been set

*/ public void neutral(@NotNull CommandSender commandSender) { if (FormatBuilder.stringFormatter == null) {