From 2252ead38af17ee7985d40473757c60da0efbd82 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Sat, 13 Sep 2025 00:43:20 +0200 Subject: [PATCH] Allows the list join delimiter in FormatBuilder to be customized in some cases --- .../knarlib/formatting/FormatBuilder.java | 48 +++++++++++++++++-- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java b/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java index c99b69a..41c8193 100644 --- a/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java +++ b/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java @@ -97,6 +97,8 @@ public final class FormatBuilder { /** * Replaces placeholders for the current string + * + *

If the placeholder or replacement is a list, it will be joined using the default delimiter: ",".

* * @param placeholder

The placeholder to replace

* @param replacement

The replacement

@@ -106,7 +108,26 @@ public final class FormatBuilder { */ @NotNull public FormatBuilder replace(@NotNull K placeholder, @NotNull L replacement) throws IllegalStateException { - this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, asString(placeholder), asString(replacement)); + this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, asString(placeholder, LIST_DELIMITER), + asString(replacement, LIST_DELIMITER)); + return this; + } + + /** + * Replaces placeholders for the current string + * + * @param placeholder

The placeholder to replace

+ * @param replacement

The replacement

+ * @param delimiter

The delimiter used for joining if the replacement is some kind of list

+ * @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 K placeholder, @NotNull L replacement, + @NotNull String delimiter) throws IllegalStateException { + this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, asString(placeholder, LIST_DELIMITER), + asString(replacement, delimiter)); return this; } @@ -157,13 +178,29 @@ public final class FormatBuilder { /** * Appends the given string to this format builder * + *

If the input is a list, it will be joined using the default delimiter: ",".

+ * * @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 K input) throws IllegalStateException { - this.toFormat += asString(input); + this.toFormat += asString(input, LIST_DELIMITER); + return this; + } + + /** + * Appends the given string to this format builder + * + * @param input

The input to append

+ * @param delimiter

The delimiter used for joining if the input is some kind of list

+ * @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 K input, @NotNull String delimiter) throws IllegalStateException { + this.toFormat += asString(input, delimiter); return this; } @@ -216,11 +253,12 @@ public final class FormatBuilder { * Converts the given input to a string * * @param input

The input to convert

+ * @param delimiter

The delimiter to use if the input is some kind of list

* @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) { + private String asString(@NotNull K input, @NotNull String delimiter) { if (input instanceof String string) { return string; } else if (input instanceof TranslatableMessage translatableMessage) { @@ -237,14 +275,14 @@ public final class FormatBuilder { for (Object item : collection) { output.add(String.valueOf(item)); } - return String.join(LIST_DELIMITER, output); + return String.join(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.join(delimiter, output); } return String.valueOf(input); }