From 5aa16bd528ef41fa82c802e00aa36f1d46a6042f Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Fri, 22 Aug 2025 15:39:21 +0200 Subject: [PATCH] Adds a FormatBuilder for cleaner advanced formatting operations --- .../knarlib/formatting/FormatBuilder.java | 151 ++++++++++++++++++ .../knarlib/formatting/StringFormatter.java | 14 +- .../knarlib/formatting/StringReplacer.java | 10 ++ .../formatting/TranslatableMessage.java | 6 +- 4 files changed, 177 insertions(+), 4 deletions(-) create mode 100644 src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java diff --git a/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java b/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java new file mode 100644 index 0000000..11816c4 --- /dev/null +++ b/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java @@ -0,0 +1,151 @@ +package net.knarcraft.knarlib.formatting; + +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +/** + * A builder for making a replaced and formatted string + */ +@SuppressWarnings("unused") +public final class FormatBuilder { + + private static StringFormatter stringFormatter; + 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

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

The translatable message to format

+ */ + public FormatBuilder(@NotNull TranslatableMessage translatableMessage) { + this.toFormat = stringFormatter.getUnFormattedMessage(translatableMessage); + } + + /** + * Instantiates a new format builder + * + * @param toFormat

The string to format

+ */ + public FormatBuilder(@NotNull String toFormat) { + this.toFormat = toFormat; + } + + /** + * Replaces placeholders for the current string + * + * @param placeholders

The placeholders to replace

+ * @param replacements

The replacements

+ * @return

This format builder

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

The placeholder to replace

+ * @param replacement

The replacement

+ * @return

This format builder

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

The placeholders to replace

+ * @param replacements

The replacements

+ * @return

This format builder

+ */ + public FormatBuilder replace(@NotNull String[] placeholders, @NotNull String[] replacements) { + this.toFormat = StringFormatter.replacePlaceholders(this.toFormat, placeholders, replacements); + return this; + } + + /** + * Replaces placeholders for the current string + * + * @param stringReplacer

The string replacer used for replacement

+ * @return

This format builder

+ */ + public FormatBuilder replace(@NotNull StringReplacer stringReplacer) { + this.toFormat = stringReplacer.replace(this.toFormat); + return this; + } + + /** + * Converts color codes in the current string + * + *

This is performed as part of the success, error and neutral methods, so it's only necessary when displaying + * the message through other means.

+ * + * @return

This format builder

+ */ + public FormatBuilder color() { + this.toFormat = stringFormatter.getUnFormattedColoredMessage(this.toFormat); + return this; + } + + /** + * Builds the output of the performed replacements and formatting + * + * @return

The result

+ */ + @NotNull + public String build() { + return this.toFormat; + } + + /** + * Displays the result to the specified command sender as a success message + * + * @param commandSender

The recipient

+ */ + public void success(@NotNull CommandSender commandSender) { + if (FormatBuilder.stringFormatter == null) { + throw NOT_SETUP_EXCEPTION; + } + FormatBuilder.stringFormatter.displaySuccessMessage(commandSender, this.toFormat); + } + + /** + * Displays the result to the specified command sender as an error + * + * @param commandSender

The recipient

+ */ + public void error(@NotNull CommandSender commandSender) { + if (FormatBuilder.stringFormatter == null) { + throw NOT_SETUP_EXCEPTION; + } + FormatBuilder.stringFormatter.displayErrorMessage(commandSender, this.toFormat); + } + + /** + * Displays the result to the specified command sender + * + * @param commandSender

The recipient

+ */ + public void neutral(@NotNull CommandSender commandSender) { + if (FormatBuilder.stringFormatter == null) { + throw NOT_SETUP_EXCEPTION; + } + FormatBuilder.stringFormatter.displayNeutralMessage(commandSender, this.toFormat); + } + +} diff --git a/src/main/java/net/knarcraft/knarlib/formatting/StringFormatter.java b/src/main/java/net/knarcraft/knarlib/formatting/StringFormatter.java index 3462196..0691b81 100644 --- a/src/main/java/net/knarcraft/knarlib/formatting/StringFormatter.java +++ b/src/main/java/net/knarcraft/knarlib/formatting/StringFormatter.java @@ -169,6 +169,16 @@ public final class StringFormatter { this.colorConversion); } + /** + * Gets the message with translated color codes + * + * @param message

The message to get the colored version of

+ * @return

The colored raw message

+ */ + public @NotNull String getUnFormattedColoredMessage(@NotNull String message) { + return ColorHelper.translateColorCodes(message, this.colorConversion); + } + /** * Replaces a placeholder in a translatable message * @@ -259,8 +269,8 @@ public final class StringFormatter { */ private String getFormattedMessage(@NotNull String message) { message = ColorHelper.translateColorCodes(message, this.colorConversion); - String coloredPrefix = ColorHelper.translateColorCodes(namePrefix + this.pluginName + - nameSuffix, this.colorConversion); + String coloredPrefix = ColorHelper.translateColorCodes(this.namePrefix + this.pluginName + + this.nameSuffix, this.colorConversion); return coloredPrefix + " " + ChatColor.RESET + message; } diff --git a/src/main/java/net/knarcraft/knarlib/formatting/StringReplacer.java b/src/main/java/net/knarcraft/knarlib/formatting/StringReplacer.java index 4384fe2..c703d6e 100644 --- a/src/main/java/net/knarcraft/knarlib/formatting/StringReplacer.java +++ b/src/main/java/net/knarcraft/knarlib/formatting/StringReplacer.java @@ -23,6 +23,16 @@ public final class StringReplacer { public StringReplacer() { } + /** + * Instantiates a new string replacer + * + * @param stringFormatter

The string formatter to use for translation

+ * @param translatableMessage

The translatable message to replace for

+ */ + public StringReplacer(@NotNull StringFormatter stringFormatter, @NotNull TranslatableMessage translatableMessage) { + this.replacementInput = stringFormatter.getUnFormattedMessage(translatableMessage); + } + /** * Instantiates a new string replacer * diff --git a/src/main/java/net/knarcraft/knarlib/formatting/TranslatableMessage.java b/src/main/java/net/knarcraft/knarlib/formatting/TranslatableMessage.java index 803f492..08bd283 100644 --- a/src/main/java/net/knarcraft/knarlib/formatting/TranslatableMessage.java +++ b/src/main/java/net/knarcraft/knarlib/formatting/TranslatableMessage.java @@ -15,7 +15,8 @@ public interface TranslatableMessage { * * @return

The name of this translatable message

*/ - @NotNull String name(); + @NotNull + String name(); /** * Gets all translatable messages @@ -24,6 +25,7 @@ public interface TranslatableMessage { * * @return

All translatable messages

*/ - @NotNull TranslatableMessage[] getAllMessages(); + @NotNull + TranslatableMessage[] getAllMessages(); }