From 0ac76f28c3a31fb02fec672bfcfbca4e6082912f Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Sat, 6 Sep 2025 14:40:02 +0200 Subject: [PATCH] Makes the format builder accept format builders for replacement and appending --- .../knarlib/formatting/FormatBuilder.java | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java b/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java index 4fbec2e..6680199 100644 --- a/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java +++ b/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java @@ -183,6 +183,81 @@ public final class FormatBuilder { return this; } + /** + * Replaces placeholders for the current string + * + * @param placeholder

The placeholder to replace

+ * @param replacement

The replacement

+ * @return

This format builder

+ */ + @NotNull + public FormatBuilder replace(@NotNull String placeholder, @NotNull FormatBuilder replacement) { + this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, placeholder, replacement.toString()); + return this; + } + + /** + * Replaces placeholders for the current string + * + * @param placeholder

The placeholder to replace

+ * @param replacement

The replacement

+ * @return

This format builder

+ */ + @NotNull + public FormatBuilder replace(@NotNull FormatBuilder placeholder, @NotNull String replacement) { + this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, placeholder.toString(), replacement); + return this; + } + + /** + * Replaces placeholders for the current string + * + * @param placeholder

The placeholder to replace

+ * @param replacement

The replacement

+ * @return

This format builder

+ */ + @NotNull + public FormatBuilder replace(@NotNull FormatBuilder placeholder, @NotNull FormatBuilder replacement) { + this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, placeholder.toString(), replacement.toString()); + return this; + } + + /** + * Replaces placeholders for the current string + * + * @param placeholder

The placeholder to replace

+ * @param replacement

The replacement

+ * @return

This format builder

+ * @throws IllegalStateException

If the string formatter has not been set

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

The placeholder to replace

+ * @param replacement

The replacement

+ * @return

This format builder

+ * @throws IllegalStateException

If the string formatter has not been set

+ */ + @NotNull + public FormatBuilder replace(@NotNull FormatBuilder placeholder, @NotNull TranslatableMessage replacement) { + if (FormatBuilder.stringFormatter == null) { + throw NOT_SETUP_EXCEPTION; + } + this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, placeholder.toString(), + FormatBuilder.stringFormatter.getUnFormattedMessage(replacement)); + return this; + } + /** * Converts color codes in the current string * @@ -235,11 +310,24 @@ public final class FormatBuilder { * @param stringBuilder

The string builder to append

* @return

This format builder

*/ + @NotNull public FormatBuilder append(@NotNull StringBuilder stringBuilder) { this.toFormat += stringBuilder.toString(); return this; } + /** + * Appends the given format builder's output to this format builder + * + * @param formatBuilder

The format builder to append

+ * @return

This format builder

+ */ + @NotNull + public FormatBuilder append(@NotNull FormatBuilder formatBuilder) { + this.toFormat += formatBuilder.toString(); + return this; + } + /** * Displays the result to the specified command sender as a success message *