diff --git a/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java b/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java
index d0136ff..dd802b1 100644
--- a/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java
+++ b/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java
@@ -20,13 +20,14 @@ 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;
+ private StringBuilder stringBuilder = null;
+ private @NotNull String stringToFormat;
/**
* Instantiates a new format builder
*/
public FormatBuilder() {
- this.toFormat = "";
+ this.stringToFormat = "";
}
/**
@@ -38,7 +39,7 @@ public final class FormatBuilder {
* @throws IllegalStateException
If the string formatter has not been set, and the input is a translatable message
*/
public FormatBuilder(@NotNull K input) throws IllegalStateException {
- this.toFormat = asString(input, LIST_DELIMITER);
+ this.stringToFormat = asString(input, LIST_DELIMITER);
}
/**
@@ -49,7 +50,7 @@ public final class FormatBuilder {
* @throws IllegalStateException If the string formatter has not been set, and the input is a translatable message
*/
public FormatBuilder(@NotNull K input, @NotNull String delimiter) throws IllegalStateException {
- this.toFormat = asString(input, delimiter);
+ this.stringToFormat = asString(input, delimiter);
}
/**
@@ -70,7 +71,7 @@ public final class FormatBuilder {
*/
@NotNull
public FormatBuilder replace(@NotNull List placeholders, @NotNull List replacements) {
- this.toFormat = StringFormatter.replacePlaceholders(this.toFormat, placeholders, replacements);
+ this.stringToFormat = StringFormatter.replacePlaceholders(getCurrentString(), placeholders, replacements);
return this;
}
@@ -83,7 +84,7 @@ public final class FormatBuilder {
*/
@NotNull
public FormatBuilder replace(@NotNull String[] placeholders, @NotNull String[] replacements) {
- this.toFormat = StringFormatter.replacePlaceholders(this.toFormat, placeholders, replacements);
+ this.stringToFormat = StringFormatter.replacePlaceholders(getCurrentString(), placeholders, replacements);
return this;
}
@@ -100,9 +101,7 @@ public final class FormatBuilder {
*/
@NotNull
public FormatBuilder replace(@NotNull K placeholder, @NotNull L replacement) throws IllegalStateException {
- this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, asString(placeholder, LIST_DELIMITER),
- asString(replacement, LIST_DELIMITER));
- return this;
+ return replace(placeholder, replacement, LIST_DELIMITER);
}
/**
@@ -118,7 +117,7 @@ public final class FormatBuilder {
@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),
+ this.stringToFormat = StringFormatter.replacePlaceholder(getCurrentString(), asString(placeholder, LIST_DELIMITER),
asString(replacement, delimiter));
return this;
}
@@ -131,7 +130,7 @@ public final class FormatBuilder {
*/
@NotNull
public FormatBuilder replace(@NotNull StringReplacer stringReplacer) {
- this.toFormat = stringReplacer.replace(this.toFormat);
+ this.stringToFormat = stringReplacer.replace(getCurrentString());
return this;
}
@@ -149,7 +148,7 @@ public final class FormatBuilder {
if (FormatBuilder.stringFormatter == null) {
throw NOT_SETUP_EXCEPTION;
}
- this.toFormat = FormatBuilder.stringFormatter.getUnFormattedColoredMessage(this.toFormat);
+ this.stringToFormat = FormatBuilder.stringFormatter.getUnFormattedColoredMessage(getCurrentString());
return this;
}
@@ -163,7 +162,7 @@ public final class FormatBuilder {
* @return This format builder
*/
public FormatBuilder color(@NotNull ColorConversion colorConversion) {
- this.toFormat = ColorHelper.translateColorCodes(this.toFormat, colorConversion);
+ this.stringToFormat = ColorHelper.translateColorCodes(getCurrentString(), colorConversion);
return this;
}
@@ -178,8 +177,7 @@ public final class FormatBuilder {
*/
@NotNull
public FormatBuilder append(@NotNull K input) throws IllegalStateException {
- this.toFormat += asString(input, LIST_DELIMITER);
- return this;
+ return append(input, LIST_DELIMITER);
}
/**
@@ -192,7 +190,10 @@ public final class FormatBuilder {
*/
@NotNull
public FormatBuilder append(@NotNull K input, @NotNull String delimiter) throws IllegalStateException {
- this.toFormat += asString(input, delimiter);
+ if (this.stringBuilder == null) {
+ this.stringBuilder = new StringBuilder();
+ }
+ this.stringBuilder.append(asString(input, delimiter));
return this;
}
@@ -206,7 +207,7 @@ public final class FormatBuilder {
if (FormatBuilder.stringFormatter == null) {
throw NOT_SETUP_EXCEPTION;
}
- FormatBuilder.stringFormatter.displaySuccessMessage(commandSender, this.toFormat);
+ FormatBuilder.stringFormatter.displaySuccessMessage(commandSender, getCurrentString());
}
/**
@@ -219,7 +220,7 @@ public final class FormatBuilder {
if (FormatBuilder.stringFormatter == null) {
throw NOT_SETUP_EXCEPTION;
}
- FormatBuilder.stringFormatter.displayErrorMessage(commandSender, this.toFormat);
+ FormatBuilder.stringFormatter.displayErrorMessage(commandSender, getCurrentString());
}
/**
@@ -232,13 +233,26 @@ public final class FormatBuilder {
if (FormatBuilder.stringFormatter == null) {
throw NOT_SETUP_EXCEPTION;
}
- FormatBuilder.stringFormatter.displayNeutralMessage(commandSender, this.toFormat);
+ FormatBuilder.stringFormatter.displayNeutralMessage(commandSender, getCurrentString());
}
@NotNull
@Override
public String toString() {
- return this.toFormat;
+ return getCurrentString();
+ }
+
+ /**
+ * Gets the currently generated string
+ *
+ * @return The current string
+ */
+ private String getCurrentString() {
+ if (this.stringBuilder != null) {
+ this.stringToFormat += this.stringBuilder.toString();
+ this.stringBuilder = null;
+ }
+ return this.stringToFormat;
}
/**
diff --git a/src/test/java/net/knarcraft/knarlib/formatting/FormatBuilderTest.java b/src/test/java/net/knarcraft/knarlib/formatting/FormatBuilderTest.java
index 5cc9f16..2ecc211 100644
--- a/src/test/java/net/knarcraft/knarlib/formatting/FormatBuilderTest.java
+++ b/src/test/java/net/knarcraft/knarlib/formatting/FormatBuilderTest.java
@@ -38,6 +38,10 @@ public class FormatBuilderTest {
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());
+ for (int i = 0; i < 10; i++) {
+ builder.append(i);
+ }
+ assertEquals("Test1,2,3,4,5abck zSINGLE,CIRCLE,CUBE0123456789", builder.toString());
}
}