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 stringBuilderThe string builder to use output from
+ */ + public FormatBuilder(@NotNull StringBuilder stringBuilder) { + this.toFormat = stringBuilder.toString(); } /** * Instantiates a new format builder * * @param translatableMessageThe translatable message to format
+ * @throws IllegalStateExceptionIf 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 stringFormatterThe 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 placeholderThe translatable message to replace
+ * @param replacementThe replacement translatable message
+ * @returnThis format builder
+ * @throws IllegalStateExceptionIf 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 placeholderThe placeholder to replace
* @param replacementThe replacement translatable message
* @returnThis format builder
+ * @throws IllegalStateExceptionIf 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 placeholderThe translatable message placeholder to replace
+ * @param replacementThe replacement string
+ * @returnThis format builder
+ * @throws IllegalStateExceptionIf 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 placeholderThe placeholder to replace
+ * @param stringBuilderThe replacement string builder
+ * @returnThis 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. * * @returnThis format builder
+ * @throws IllegalStateExceptionIf 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 translatableMessageThe translatable message to append
* @returnThis format builder
+ * @throws IllegalStateExceptionIf 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 stringBuilderThe string builder to append
+ * @returnThis format builder
+ */ + public FormatBuilder append(@NotNull StringBuilder stringBuilder) { + this.toFormat += stringBuilder.toString(); + return this; + } + /** * Builds the output of the performed replacements and formatting * * @returnThe 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 commandSenderThe recipient
+ * @throws IllegalStateExceptionIf 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 commandSenderThe recipient
+ * @throws IllegalStateExceptionIf 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 commandSenderThe recipient
+ * @throws IllegalStateExceptionIf the string formatter has not been set
*/ public void neutral(@NotNull CommandSender commandSender) { if (FormatBuilder.stringFormatter == null) {