Adds more options for replacing with the format builder
All checks were successful
KnarCraft/KnarLib/pipeline/head This commit looks good

Adds methods for auto conversion of arrays to string
Adds methods for auto conversion of objects to string
Adds method for alternate color conversion
Removes redundancy for a lot of FormatBuilder methods
This commit is contained in:
2025-09-12 00:01:39 +02:00
parent 9e04d72449
commit 1e24fef874

View File

@@ -1,9 +1,12 @@
package net.knarcraft.knarlib.formatting; package net.knarcraft.knarlib.formatting;
import net.knarcraft.knarlib.property.ColorConversion;
import net.knarcraft.knarlib.util.ColorHelper;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.List; import java.util.List;
/** /**
@@ -76,88 +79,6 @@ public final class FormatBuilder {
return this; return this;
} }
/**
* Replaces placeholders for the current string
*
* @param placeholder <p>The placeholder to replace</p>
* @param replacement <p>The replacement</p>
* @return <p>This format builder</p>
*/
@NotNull
public FormatBuilder replace(@NotNull String placeholder, @NotNull String replacement) {
this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, placeholder, replacement);
return this;
}
/**
* Replaces placeholders for the current string
*
* @param placeholder <p>The translatable message to replace</p>
* @param replacement <p>The replacement translatable message</p>
* @return <p>This format builder</p>
* @throws IllegalStateException <p>If the string formatter has not been set</p>
*/
@NotNull
public FormatBuilder replace(@NotNull TranslatableMessage placeholder,
@NotNull TranslatableMessage replacement) {
if (FormatBuilder.stringFormatter == null) {
throw NOT_SETUP_EXCEPTION;
}
this.toFormat = StringFormatter.replacePlaceholder(this.toFormat,
FormatBuilder.stringFormatter.getUnFormattedMessage(placeholder),
FormatBuilder.stringFormatter.getUnFormattedMessage(replacement));
return this;
}
/**
* Replaces placeholders for the current string
*
* @param placeholder <p>The placeholder to replace</p>
* @param replacement <p>The replacement translatable message</p>
* @return <p>This format builder</p>
* @throws IllegalStateException <p>If the string formatter has not been set</p>
*/
@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,
FormatBuilder.stringFormatter.getUnFormattedMessage(replacement));
return this;
}
/**
* Replaces placeholders for the current string
*
* @param placeholder <p>The translatable message placeholder to replace</p>
* @param replacement <p>The replacement string</p>
* @return <p>This format builder</p>
* @throws IllegalStateException <p>If the string formatter has not been set</p>
*/
@NotNull
public FormatBuilder replace(@NotNull TranslatableMessage placeholder, @NotNull String replacement) {
if (FormatBuilder.stringFormatter == null) {
throw NOT_SETUP_EXCEPTION;
}
this.toFormat = StringFormatter.replacePlaceholder(this.toFormat,
FormatBuilder.stringFormatter.getUnFormattedMessage(placeholder), replacement);
return this;
}
/**
* Replaces placeholders for the current string
*
* @param placeholder <p>The placeholder to replace</p>
* @param stringBuilder <p>The replacement string builder</p>
* @return <p>This format builder</p>
*/
@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 * Replaces placeholders for the current string
* *
@@ -171,6 +92,19 @@ public final class FormatBuilder {
return this; return this;
} }
/**
* Replaces placeholders for the current string
*
* @param placeholder <p>The placeholder to replace</p>
* @param replacement <p>The replacement</p>
* @return <p>This format builder</p>
*/
@NotNull
public FormatBuilder replace(@NotNull String placeholder, @NotNull String replacement) {
this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, placeholder, replacement);
return this;
}
/** /**
* Replaces placeholders for the current string * Replaces placeholders for the current string
* *
@@ -183,6 +117,68 @@ public final class FormatBuilder {
return this; return this;
} }
/**
* Replaces placeholders for the current string
*
* @param placeholder <p>The translatable message to replace</p>
* @param replacement <p>The replacement translatable message</p>
* @return <p>This format builder</p>
* @throws IllegalStateException <p>If the string formatter has not been set</p>
*/
@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 <p>The placeholder to replace</p>
* @param replacement <p>The replacement translatable message</p>
* @return <p>This format builder</p>
* @throws IllegalStateException <p>If the string formatter has not been set</p>
*/
@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 <p>The translatable message placeholder to replace</p>
* @param replacement <p>The replacement string</p>
* @return <p>This format builder</p>
* @throws IllegalStateException <p>If the string formatter has not been set</p>
*/
@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 <p>The placeholder to replace</p>
* @param stringBuilder <p>The replacement string builder</p>
* @return <p>This format builder</p>
*/
@NotNull
public FormatBuilder replace(@NotNull String placeholder, @NotNull StringBuilder stringBuilder) {
return replace(placeholder, stringBuilder.toString());
}
/** /**
* Replaces placeholders for the current string * Replaces placeholders for the current string
* *
@@ -192,8 +188,7 @@ public final class FormatBuilder {
*/ */
@NotNull @NotNull
public FormatBuilder replace(@NotNull String placeholder, @NotNull FormatBuilder replacement) { public FormatBuilder replace(@NotNull String placeholder, @NotNull FormatBuilder replacement) {
this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, placeholder, replacement.toString()); return replace(placeholder, replacement.toString());
return this;
} }
/** /**
@@ -205,8 +200,7 @@ public final class FormatBuilder {
*/ */
@NotNull @NotNull
public FormatBuilder replace(@NotNull FormatBuilder placeholder, @NotNull String replacement) { public FormatBuilder replace(@NotNull FormatBuilder placeholder, @NotNull String replacement) {
this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, placeholder.toString(), replacement); return replace(placeholder.toString(), replacement);
return this;
} }
/** /**
@@ -218,8 +212,7 @@ public final class FormatBuilder {
*/ */
@NotNull @NotNull
public FormatBuilder replace(@NotNull FormatBuilder placeholder, @NotNull FormatBuilder replacement) { public FormatBuilder replace(@NotNull FormatBuilder placeholder, @NotNull FormatBuilder replacement) {
this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, placeholder.toString(), replacement.toString()); return replace(placeholder.toString(), replacement.toString());
return this;
} }
/** /**
@@ -232,12 +225,7 @@ public final class FormatBuilder {
*/ */
@NotNull @NotNull
public FormatBuilder replace(@NotNull TranslatableMessage placeholder, @NotNull FormatBuilder replacement) { public FormatBuilder replace(@NotNull TranslatableMessage placeholder, @NotNull FormatBuilder replacement) {
if (FormatBuilder.stringFormatter == null) { return replace(placeholder, replacement.toString());
throw NOT_SETUP_EXCEPTION;
}
this.toFormat = StringFormatter.replacePlaceholder(this.toFormat,
FormatBuilder.stringFormatter.getUnFormattedMessage(placeholder), replacement.toString());
return this;
} }
/** /**
@@ -250,12 +238,60 @@ public final class FormatBuilder {
*/ */
@NotNull @NotNull
public FormatBuilder replace(@NotNull FormatBuilder placeholder, @NotNull TranslatableMessage replacement) { public FormatBuilder replace(@NotNull FormatBuilder placeholder, @NotNull TranslatableMessage replacement) {
if (FormatBuilder.stringFormatter == null) { return replace(placeholder.toString(), replacement);
throw NOT_SETUP_EXCEPTION; }
}
this.toFormat = StringFormatter.replacePlaceholder(this.toFormat, placeholder.toString(), /**
FormatBuilder.stringFormatter.getUnFormattedMessage(replacement)); * Replaces a placeholder for the current string
return this; *
* @param placeholder <p>The translatable message placeholder to replace</p>
* @param replacement <p>The object to get the string value of</p>
* @return <p>This format builder</p>
* @throws IllegalStateException <p>If the string formatter has not been set</p>
*/
@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 <p>The placeholder to replace</p>
* @param replacement <p>The object to get the string value of</p>
* @return <p>This format builder</p>
*/
@NotNull
public FormatBuilder replace(@NotNull String placeholder, @NotNull Object replacement) {
return replace(placeholder, String.valueOf(replacement));
}
/**
* Replaces a placeholder for the current string
*
* <p>This method turns the given array into a string, and replaces the placeholder with that string.</p>
*
* @param placeholder <p>The placeholder to replace</p>
* @param replacement <p>The list of strings to replace with</p>
* @return <p>This format builder</p>
*/
@NotNull
public FormatBuilder replace(@NotNull String placeholder, @NotNull List<String> replacement) {
return replace(placeholder, replacement.toArray());
}
/**
* Replaces a placeholder for the current string
*
* <p>This method turns the given array into a string, and replaces the placeholder with that string.</p>
*
* @param placeholder <p>The placeholder to replace</p>
* @param replacement <p>The list of strings to replace with</p>
* @return <p>This format builder</p>
*/
@NotNull
public FormatBuilder replace(@NotNull String placeholder, @NotNull String[] replacement) {
return replace(placeholder, Arrays.toString(replacement));
} }
/** /**
@@ -276,6 +312,20 @@ public final class FormatBuilder {
return this; return this;
} }
/**
* Converts color codes in the current string
*
* <p>This is performed as part of the success, error and neutral methods, so it's only necessary when displaying
* the message through other means.</p>
*
* @param colorConversion <p>The type of color conversion to use</p>
* @return <p>This format builder</p>
*/
public FormatBuilder color(@NotNull ColorConversion colorConversion) {
this.toFormat = ColorHelper.translateColorCodes(this.toFormat, colorConversion);
return this;
}
/** /**
* Appends the given string to this format builder * Appends the given string to this format builder
* *
@@ -300,8 +350,7 @@ public final class FormatBuilder {
if (FormatBuilder.stringFormatter == null) { if (FormatBuilder.stringFormatter == null) {
throw NOT_SETUP_EXCEPTION; throw NOT_SETUP_EXCEPTION;
} }
this.toFormat += FormatBuilder.stringFormatter.getUnFormattedMessage(translatableMessage); return append(FormatBuilder.stringFormatter.getUnFormattedMessage(translatableMessage));
return this;
} }
/** /**
@@ -312,8 +361,7 @@ public final class FormatBuilder {
*/ */
@NotNull @NotNull
public FormatBuilder append(@NotNull StringBuilder stringBuilder) { public FormatBuilder append(@NotNull StringBuilder stringBuilder) {
this.toFormat += stringBuilder.toString(); return append(stringBuilder.toString());
return this;
} }
/** /**
@@ -324,8 +372,7 @@ public final class FormatBuilder {
*/ */
@NotNull @NotNull
public FormatBuilder append(@NotNull FormatBuilder formatBuilder) { public FormatBuilder append(@NotNull FormatBuilder formatBuilder) {
this.toFormat += formatBuilder.toString(); return append(formatBuilder.toString());
return this;
} }
/** /**