From 298b339ac675659cbfcefe9574312efd9666fdd1 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Fri, 12 Sep 2025 02:36:07 +0200 Subject: [PATCH] Increases flexibility and reduces number of methods for the format builder Adds some tests for the FormatBuilder Replaces all the different methods with a few methods that accept any object Adds a method for converting any non-null object to a string Adds throws to the methods that might throw an IllegalStateException --- .../knarlib/formatting/FormatBuilder.java | 277 ++++-------------- .../knarlib/formatting/FormatBuilderTest.java | 43 +++ 2 files changed, 96 insertions(+), 224 deletions(-) create mode 100644 src/test/java/net/knarcraft/knarlib/formatting/FormatBuilderTest.java diff --git a/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java b/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java index 4f3fcaf..c99b69a 100644 --- a/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java +++ b/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java @@ -6,7 +6,9 @@ import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Arrays; +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Collection; import java.util.List; /** @@ -17,6 +19,7 @@ public final class FormatBuilder { private static @Nullable StringFormatter stringFormatter = null; private static final IllegalStateException NOT_SETUP_EXCEPTION = new IllegalStateException("String formatter has not been set!"); + private static final String LIST_DELIMITER = ","; private @NotNull String toFormat; /** @@ -41,7 +44,7 @@ public final class FormatBuilder { * @param translatableMessage

The translatable message to format

* @throws IllegalStateException

If the string formatter has not been set

*/ - public FormatBuilder(@NotNull TranslatableMessage translatableMessage) { + public FormatBuilder(@NotNull TranslatableMessage translatableMessage) throws IllegalStateException { if (FormatBuilder.stringFormatter == null) { throw NOT_SETUP_EXCEPTION; } @@ -98,10 +101,12 @@ public final class FormatBuilder { * @param placeholder

The placeholder to replace

* @param replacement

The replacement

* @return

This format builder

+ * @throws IllegalStateException

If the string formatter has not been set, and the placeholder or replacement is + * a translatable message

*/ @NotNull - public FormatBuilder replace(@NotNull String placeholder, @NotNull String replacement) { - this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, placeholder, replacement); + public FormatBuilder replace(@NotNull K placeholder, @NotNull L replacement) throws IllegalStateException { + this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, asString(placeholder), asString(replacement)); return this; } @@ -117,183 +122,6 @@ 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; - } - return replace(FormatBuilder.stringFormatter.getUnFormattedMessage(placeholder), - FormatBuilder.stringFormatter.getUnFormattedMessage(replacement)); - } - - /** - * 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; - } - return replace(placeholder, FormatBuilder.stringFormatter.getUnFormattedMessage(replacement)); - } - - /** - * 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; - } - return replace(FormatBuilder.stringFormatter.getUnFormattedMessage(placeholder), replacement); - } - - /** - * 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) { - return replace(placeholder, stringBuilder.toString()); - } - - /** - * 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) { - return replace(placeholder, replacement.toString()); - } - - /** - * 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) { - return replace(placeholder.toString(), replacement); - } - - /** - * 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) { - return replace(placeholder.toString(), replacement.toString()); - } - - /** - * 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) { - return replace(placeholder, replacement.toString()); - } - - /** - * 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) { - return replace(placeholder.toString(), replacement); - } - - /** - * Replaces a placeholder for the current string - * - * @param placeholder

The translatable message placeholder to replace

- * @param replacement

The object to get the string value of

- * @return

This format builder

- * @throws IllegalStateException

If the string formatter has not been set

- */ - @NotNull - public FormatBuilder replace(@NotNull TranslatableMessage placeholder, @NotNull Object replacement) { - return replace(placeholder, String.valueOf(replacement)); - } - - /** - * Replaces a placeholder for the current string - * - * @param placeholder

The placeholder to replace

- * @param replacement

The object to get the string value of

- * @return

This format builder

- */ - @NotNull - public FormatBuilder replace(@NotNull String placeholder, @NotNull Object replacement) { - return replace(placeholder, String.valueOf(replacement)); - } - - /** - * Replaces a placeholder for the current string - * - *

This method turns the given array into a string, and replaces the placeholder with that string.

- * - * @param placeholder

The placeholder to replace

- * @param replacement

The list of strings to replace with

- * @return

This format builder

- */ - @NotNull - public FormatBuilder replace(@NotNull String placeholder, @NotNull List replacement) { - return replace(placeholder, replacement.toArray()); - } - - /** - * Replaces a placeholder for the current string - * - *

This method turns the given array into a string, and replaces the placeholder with that string.

- * - * @param placeholder

The placeholder to replace

- * @param replacement

The list of strings to replace with

- * @return

This format builder

- */ - @NotNull - public FormatBuilder replace(@NotNull String placeholder, @NotNull String[] replacement) { - return replace(placeholder, Arrays.toString(replacement)); - } - /** * Converts color codes in the current string * @@ -304,7 +132,7 @@ public final class FormatBuilder { * @throws IllegalStateException

If the string formatter has not been set

*/ @NotNull - public FormatBuilder color() { + public FormatBuilder color() throws IllegalStateException { if (FormatBuilder.stringFormatter == null) { throw NOT_SETUP_EXCEPTION; } @@ -331,57 +159,21 @@ public final class FormatBuilder { * * @param input

The input to append

* @return

This format builder

+ * @throws IllegalStateException

If the string formatter has not been set, and the input is a translatable message

*/ @NotNull - public FormatBuilder append(@NotNull String input) { - this.toFormat += input; + public FormatBuilder append(@NotNull K input) throws IllegalStateException { + this.toFormat += asString(input); return this; } - /** - * Appends the given translatable message to this format builder - * - * @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; - } - return append(FormatBuilder.stringFormatter.getUnFormattedMessage(translatableMessage)); - } - - /** - * Appends the given string builder's output to this format builder - * - * @param stringBuilder

The string builder to append

- * @return

This format builder

- */ - @NotNull - public FormatBuilder append(@NotNull StringBuilder stringBuilder) { - return append(stringBuilder.toString()); - } - - /** - * 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) { - return append(formatBuilder.toString()); - } - /** * 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) { + public void success(@NotNull CommandSender commandSender) throws IllegalStateException { if (FormatBuilder.stringFormatter == null) { throw NOT_SETUP_EXCEPTION; } @@ -394,7 +186,7 @@ public final class FormatBuilder { * @param commandSender

The recipient

* @throws IllegalStateException

If the string formatter has not been set

*/ - public void error(@NotNull CommandSender commandSender) { + public void error(@NotNull CommandSender commandSender) throws IllegalStateException { if (FormatBuilder.stringFormatter == null) { throw NOT_SETUP_EXCEPTION; } @@ -407,7 +199,7 @@ public final class FormatBuilder { * @param commandSender

The recipient

* @throws IllegalStateException

If the string formatter has not been set

*/ - public void neutral(@NotNull CommandSender commandSender) { + public void neutral(@NotNull CommandSender commandSender) throws IllegalStateException { if (FormatBuilder.stringFormatter == null) { throw NOT_SETUP_EXCEPTION; } @@ -420,4 +212,41 @@ public final class FormatBuilder { return this.toFormat; } + /** + * Converts the given input to a string + * + * @param input

The input to convert

+ * @return

The corresponding string

+ * @throws IllegalStateException

If the string formatter has not been set, and the input is a translatable message.

+ */ + @NotNull + private String asString(@NotNull K input) { + if (input instanceof String string) { + return string; + } else if (input instanceof TranslatableMessage translatableMessage) { + if (FormatBuilder.stringFormatter == null) { + throw NOT_SETUP_EXCEPTION; + } + return FormatBuilder.stringFormatter.getUnFormattedMessage(translatableMessage); + } else if (input instanceof FormatBuilder builder) { + return builder.toString(); + } else if (input instanceof StringBuilder builder) { + return builder.toString(); + } else if (input instanceof Collection collection) { + List output = new ArrayList<>(collection.size()); + for (Object item : collection) { + output.add(String.valueOf(item)); + } + return String.join(LIST_DELIMITER, output); + } else if (input.getClass().isArray()) { + int length = Array.getLength(input); + List output = new ArrayList<>(length); + for (int i = 0; i < length; i++) { + output.add(String.valueOf(Array.get(input, i))); + } + return String.join(LIST_DELIMITER, output); + } + return String.valueOf(input); + } + } diff --git a/src/test/java/net/knarcraft/knarlib/formatting/FormatBuilderTest.java b/src/test/java/net/knarcraft/knarlib/formatting/FormatBuilderTest.java new file mode 100644 index 0000000..5cc9f16 --- /dev/null +++ b/src/test/java/net/knarcraft/knarlib/formatting/FormatBuilderTest.java @@ -0,0 +1,43 @@ +package net.knarcraft.knarlib.formatting; + +import net.knarcraft.knarlib.particle.ParticleMode; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@SuppressWarnings("SpellCheckingInspection") +public class FormatBuilderTest { + + @Test + public void replaceTest() { + FormatBuilder builder = new FormatBuilder("{1}{2}{3}{4}{5}"); + builder.replace("{1}", List.of(1, 2, 3, 4, 5)); + assertEquals("1,2,3,4,5{2}{3}{4}{5}", builder.toString()); + builder.replace("{2}", "StringTest"); + assertEquals("1,2,3,4,5StringTest{3}{4}{5}", builder.toString()); + StringBuilder testBuilder = new StringBuilder().append("This").append(" ").append("is").append(" ").append("some").append(" ").append("text"); + builder.replace("{3}", testBuilder); + assertEquals("1,2,3,4,5StringTestThis is some text{4}{5}", builder.toString()); + builder.replace("{4}", new FormatBuilder("JustTesting").replace("Testing", "Fishing")); + assertEquals("1,2,3,4,5StringTestThis is some textJustFishing{5}", builder.toString()); + builder.replace("{5}", new String[]{"a", "b", "c"}); + assertEquals("1,2,3,4,5StringTestThis is some textJustFishinga,b,c", builder.toString()); + } + + @Test + public void appendTest() { + FormatBuilder builder = new FormatBuilder(); + assertEquals("", builder.toString()); + builder.append("Test"); + assertEquals("Test", builder.toString()); + builder.append(new int[]{1, 2, 3, 4, 5}); + assertEquals("Test1,2,3,4,5", builder.toString()); + builder.append(new FormatBuilder("abc")).append(new StringBuilder("k").append(" ").append("z")); + assertEquals("Test1,2,3,4,5abck z", builder.toString()); + builder.append(List.of(ParticleMode.SINGLE, ParticleMode.CIRCLE, ParticleMode.CUBE)); + assertEquals("Test1,2,3,4,5abck zSINGLE,CIRCLE,CUBE", builder.toString()); + } + +}