Makes the format builder more flexible
All checks were successful
KnarCraft/KnarLib/pipeline/head This commit looks good
All checks were successful
KnarCraft/KnarLib/pipeline/head This commit looks good
Adds new no-argument constructor Adds methods for constructing, appending and replacing with string builders Adds ability to use translatable messages as placeholders Adds missing handling of an uninitialized string formatter for some methods
This commit is contained in:
@@ -2,6 +2,7 @@ package net.knarcraft.knarlib.formatting;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -11,25 +12,36 @@ import java.util.List;
|
||||
@SuppressWarnings("unused")
|
||||
public final class FormatBuilder {
|
||||
|
||||
private static StringFormatter stringFormatter;
|
||||
private static @Nullable StringFormatter stringFormatter = null;
|
||||
private static final IllegalStateException NOT_SETUP_EXCEPTION = new IllegalStateException("String formatter has not been set!");
|
||||
private @NotNull String toFormat;
|
||||
|
||||
/**
|
||||
* Sets the string formatter to use for translation and formatting
|
||||
*
|
||||
* @param stringFormatter <p>The string formatter to use</p>
|
||||
* Instantiates a new format builder
|
||||
*/
|
||||
public static void setStringFormatter(@NotNull StringFormatter stringFormatter) {
|
||||
FormatBuilder.stringFormatter = stringFormatter;
|
||||
public FormatBuilder() {
|
||||
this.toFormat = "";
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new format builder
|
||||
*
|
||||
* @param stringBuilder <p>The string builder to use output from</p>
|
||||
*/
|
||||
public FormatBuilder(@NotNull StringBuilder stringBuilder) {
|
||||
this.toFormat = stringBuilder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new format builder
|
||||
*
|
||||
* @param translatableMessage <p>The translatable message to format</p>
|
||||
* @throws IllegalStateException <p>If the string formatter has not been set</p>
|
||||
*/
|
||||
public FormatBuilder(@NotNull TranslatableMessage translatableMessage) {
|
||||
if (FormatBuilder.stringFormatter == null) {
|
||||
throw NOT_SETUP_EXCEPTION;
|
||||
}
|
||||
this.toFormat = stringFormatter.getUnFormattedMessage(translatableMessage);
|
||||
}
|
||||
|
||||
@@ -42,6 +54,15 @@ public final class FormatBuilder {
|
||||
this.toFormat = toFormat;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the string formatter to use for translation and formatting
|
||||
*
|
||||
* @param stringFormatter <p>The string formatter to use</p>
|
||||
*/
|
||||
public static void setStringFormatter(@NotNull StringFormatter stringFormatter) {
|
||||
FormatBuilder.stringFormatter = stringFormatter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces placeholders for the current string
|
||||
*
|
||||
@@ -68,20 +89,74 @@ public final class FormatBuilder {
|
||||
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,
|
||||
stringFormatter.getUnFormattedMessage(placeholder), 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,
|
||||
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,
|
||||
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
|
||||
*
|
||||
@@ -114,9 +189,13 @@ public final class FormatBuilder {
|
||||
* the message through other means.</p>
|
||||
*
|
||||
* @return <p>This format builder</p>
|
||||
* @throws IllegalStateException <p>If the string formatter has not been set</p>
|
||||
*/
|
||||
@NotNull
|
||||
public FormatBuilder color() {
|
||||
if (FormatBuilder.stringFormatter == null) {
|
||||
throw NOT_SETUP_EXCEPTION;
|
||||
}
|
||||
this.toFormat = stringFormatter.getUnFormattedColoredMessage(this.toFormat);
|
||||
return this;
|
||||
}
|
||||
@@ -138,20 +217,35 @@ public final class FormatBuilder {
|
||||
*
|
||||
* @param translatableMessage <p>The translatable message to append</p>
|
||||
* @return <p>This format builder</p>
|
||||
* @throws IllegalStateException <p>If the string formatter has not been set</p>
|
||||
*/
|
||||
@NotNull
|
||||
public FormatBuilder append(@NotNull TranslatableMessage translatableMessage) {
|
||||
if (FormatBuilder.stringFormatter == null) {
|
||||
throw NOT_SETUP_EXCEPTION;
|
||||
}
|
||||
this.toFormat += stringFormatter.getUnFormattedMessage(translatableMessage);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the given string builder's output to this format builder
|
||||
*
|
||||
* @param stringBuilder <p>The string builder to append</p>
|
||||
* @return <p>This format builder</p>
|
||||
*/
|
||||
public FormatBuilder append(@NotNull StringBuilder stringBuilder) {
|
||||
this.toFormat += stringBuilder.toString();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the output of the performed replacements and formatting
|
||||
*
|
||||
* @return <p>The result</p>
|
||||
*/
|
||||
@NotNull
|
||||
public String build() {
|
||||
public String toString() {
|
||||
return this.toFormat;
|
||||
}
|
||||
|
||||
@@ -159,6 +253,7 @@ public final class FormatBuilder {
|
||||
* Displays the result to the specified command sender as a success message
|
||||
*
|
||||
* @param commandSender <p>The recipient</p>
|
||||
* @throws IllegalStateException <p>If the string formatter has not been set</p>
|
||||
*/
|
||||
public void success(@NotNull CommandSender commandSender) {
|
||||
if (FormatBuilder.stringFormatter == null) {
|
||||
@@ -171,6 +266,7 @@ public final class FormatBuilder {
|
||||
* Displays the result to the specified command sender as an error
|
||||
*
|
||||
* @param commandSender <p>The recipient</p>
|
||||
* @throws IllegalStateException <p>If the string formatter has not been set</p>
|
||||
*/
|
||||
public void error(@NotNull CommandSender commandSender) {
|
||||
if (FormatBuilder.stringFormatter == null) {
|
||||
@@ -183,6 +279,7 @@ public final class FormatBuilder {
|
||||
* Displays the result to the specified command sender
|
||||
*
|
||||
* @param commandSender <p>The recipient</p>
|
||||
* @throws IllegalStateException <p>If the string formatter has not been set</p>
|
||||
*/
|
||||
public void neutral(@NotNull CommandSender commandSender) {
|
||||
if (FormatBuilder.stringFormatter == null) {
|
||||
|
Reference in New Issue
Block a user