Allows the list join delimiter in FormatBuilder to be customized in some cases
All checks were successful
KnarCraft/KnarLib/pipeline/head This commit looks good

This commit is contained in:
2025-09-13 00:43:20 +02:00
parent 298b339ac6
commit 2252ead38a

View File

@@ -97,6 +97,8 @@ public final class FormatBuilder {
/** /**
* Replaces placeholders for the current string * Replaces placeholders for the current string
*
* <p>If the placeholder or replacement is a list, it will be joined using the default delimiter: ",".</p>
* *
* @param placeholder <p>The placeholder to replace</p> * @param placeholder <p>The placeholder to replace</p>
* @param replacement <p>The replacement</p> * @param replacement <p>The replacement</p>
@@ -106,7 +108,26 @@ public final class FormatBuilder {
*/ */
@NotNull @NotNull
public <K, L> FormatBuilder replace(@NotNull K placeholder, @NotNull L replacement) throws IllegalStateException { public <K, L> 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 <p>The placeholder to replace</p>
* @param replacement <p>The replacement</p>
* @param delimiter <p>The delimiter used for joining if the replacement is some kind of list</p>
* @return <p>This format builder</p>
* @throws IllegalStateException <p>If the string formatter has not been set, and the placeholder or replacement is
* a translatable message</p>
*/
@NotNull
public <K, L> 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; return this;
} }
@@ -157,13 +178,29 @@ public final class FormatBuilder {
/** /**
* Appends the given string to this format builder * Appends the given string to this format builder
* *
* <p>If the input is a list, it will be joined using the default delimiter: ",".</p>
*
* @param input <p>The input to append</p> * @param input <p>The input to append</p>
* @return <p>This format builder</p> * @return <p>This format builder</p>
* @throws IllegalStateException <p>If the string formatter has not been set, and the input is a translatable message</p> * @throws IllegalStateException <p>If the string formatter has not been set, and the input is a translatable message</p>
*/ */
@NotNull @NotNull
public <K> FormatBuilder append(@NotNull K input) throws IllegalStateException { public <K> 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 <p>The input to append</p>
* @param delimiter <p>The delimiter used for joining if the input is some kind of list</p>
* @return <p>This format builder</p>
* @throws IllegalStateException <p>If the string formatter has not been set, and the input is a translatable message</p>
*/
@NotNull
public <K> FormatBuilder append(@NotNull K input, @NotNull String delimiter) throws IllegalStateException {
this.toFormat += asString(input, delimiter);
return this; return this;
} }
@@ -216,11 +253,12 @@ public final class FormatBuilder {
* Converts the given input to a string * Converts the given input to a string
* *
* @param input <p>The input to convert</p> * @param input <p>The input to convert</p>
* @param delimiter <p>The delimiter to use if the input is some kind of list</p>
* @return <p>The corresponding string</p> * @return <p>The corresponding string</p>
* @throws IllegalStateException <p>If the string formatter has not been set, and the input is a translatable message.</p> * @throws IllegalStateException <p>If the string formatter has not been set, and the input is a translatable message.</p>
*/ */
@NotNull @NotNull
private <K> String asString(@NotNull K input) { private <K> String asString(@NotNull K input, @NotNull String delimiter) {
if (input instanceof String string) { if (input instanceof String string) {
return string; return string;
} else if (input instanceof TranslatableMessage translatableMessage) { } else if (input instanceof TranslatableMessage translatableMessage) {
@@ -237,14 +275,14 @@ public final class FormatBuilder {
for (Object item : collection) { for (Object item : collection) {
output.add(String.valueOf(item)); output.add(String.valueOf(item));
} }
return String.join(LIST_DELIMITER, output); return String.join(delimiter, output);
} else if (input.getClass().isArray()) { } else if (input.getClass().isArray()) {
int length = Array.getLength(input); int length = Array.getLength(input);
List<String> output = new ArrayList<>(length); List<String> output = new ArrayList<>(length);
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
output.add(String.valueOf(Array.get(input, i))); output.add(String.valueOf(Array.get(input, i)));
} }
return String.join(LIST_DELIMITER, output); return String.join(delimiter, output);
} }
return String.valueOf(input); return String.valueOf(input);
} }