Adds a FormatBuilder for cleaner advanced formatting operations
All checks were successful
KnarCraft/KnarLib/pipeline/head This commit looks good
All checks were successful
KnarCraft/KnarLib/pipeline/head This commit looks good
This commit is contained in:
@@ -0,0 +1,151 @@
|
|||||||
|
package net.knarcraft.knarlib.formatting;
|
||||||
|
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A builder for making a replaced and formatted string
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public final class FormatBuilder {
|
||||||
|
|
||||||
|
private static StringFormatter stringFormatter;
|
||||||
|
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>
|
||||||
|
*/
|
||||||
|
public static void setStringFormatter(@NotNull StringFormatter stringFormatter) {
|
||||||
|
FormatBuilder.stringFormatter = stringFormatter;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiates a new format builder
|
||||||
|
*
|
||||||
|
* @param translatableMessage <p>The translatable message to format</p>
|
||||||
|
*/
|
||||||
|
public FormatBuilder(@NotNull TranslatableMessage translatableMessage) {
|
||||||
|
this.toFormat = stringFormatter.getUnFormattedMessage(translatableMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiates a new format builder
|
||||||
|
*
|
||||||
|
* @param toFormat <p>The string to format</p>
|
||||||
|
*/
|
||||||
|
public FormatBuilder(@NotNull String toFormat) {
|
||||||
|
this.toFormat = toFormat;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replaces placeholders for the current string
|
||||||
|
*
|
||||||
|
* @param placeholders <p>The placeholders to replace</p>
|
||||||
|
* @param replacements <p>The replacements</p>
|
||||||
|
* @return <p>This format builder</p>
|
||||||
|
*/
|
||||||
|
public FormatBuilder replace(@NotNull List<String> placeholders, @NotNull List<String> replacements) {
|
||||||
|
this.toFormat = StringFormatter.replacePlaceholders(this.toFormat, placeholders, replacements);
|
||||||
|
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>
|
||||||
|
*/
|
||||||
|
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 placeholders <p>The placeholders to replace</p>
|
||||||
|
* @param replacements <p>The replacements</p>
|
||||||
|
* @return <p>This format builder</p>
|
||||||
|
*/
|
||||||
|
public FormatBuilder replace(@NotNull String[] placeholders, @NotNull String[] replacements) {
|
||||||
|
this.toFormat = StringFormatter.replacePlaceholders(this.toFormat, placeholders, replacements);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replaces placeholders for the current string
|
||||||
|
*
|
||||||
|
* @param stringReplacer <p>The string replacer used for replacement</p>
|
||||||
|
* @return <p>This format builder</p>
|
||||||
|
*/
|
||||||
|
public FormatBuilder replace(@NotNull StringReplacer stringReplacer) {
|
||||||
|
this.toFormat = stringReplacer.replace(this.toFormat);
|
||||||
|
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>
|
||||||
|
*
|
||||||
|
* @return <p>This format builder</p>
|
||||||
|
*/
|
||||||
|
public FormatBuilder color() {
|
||||||
|
this.toFormat = stringFormatter.getUnFormattedColoredMessage(this.toFormat);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds the output of the performed replacements and formatting
|
||||||
|
*
|
||||||
|
* @return <p>The result</p>
|
||||||
|
*/
|
||||||
|
@NotNull
|
||||||
|
public String build() {
|
||||||
|
return this.toFormat;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Displays the result to the specified command sender as a success message
|
||||||
|
*
|
||||||
|
* @param commandSender <p>The recipient</p>
|
||||||
|
*/
|
||||||
|
public void success(@NotNull CommandSender commandSender) {
|
||||||
|
if (FormatBuilder.stringFormatter == null) {
|
||||||
|
throw NOT_SETUP_EXCEPTION;
|
||||||
|
}
|
||||||
|
FormatBuilder.stringFormatter.displaySuccessMessage(commandSender, this.toFormat);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Displays the result to the specified command sender as an error
|
||||||
|
*
|
||||||
|
* @param commandSender <p>The recipient</p>
|
||||||
|
*/
|
||||||
|
public void error(@NotNull CommandSender commandSender) {
|
||||||
|
if (FormatBuilder.stringFormatter == null) {
|
||||||
|
throw NOT_SETUP_EXCEPTION;
|
||||||
|
}
|
||||||
|
FormatBuilder.stringFormatter.displayErrorMessage(commandSender, this.toFormat);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Displays the result to the specified command sender
|
||||||
|
*
|
||||||
|
* @param commandSender <p>The recipient</p>
|
||||||
|
*/
|
||||||
|
public void neutral(@NotNull CommandSender commandSender) {
|
||||||
|
if (FormatBuilder.stringFormatter == null) {
|
||||||
|
throw NOT_SETUP_EXCEPTION;
|
||||||
|
}
|
||||||
|
FormatBuilder.stringFormatter.displayNeutralMessage(commandSender, this.toFormat);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -169,6 +169,16 @@ public final class StringFormatter {
|
|||||||
this.colorConversion);
|
this.colorConversion);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the message with translated color codes
|
||||||
|
*
|
||||||
|
* @param message <p>The message to get the colored version of</p>
|
||||||
|
* @return <p>The colored raw message</p>
|
||||||
|
*/
|
||||||
|
public @NotNull String getUnFormattedColoredMessage(@NotNull String message) {
|
||||||
|
return ColorHelper.translateColorCodes(message, this.colorConversion);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Replaces a placeholder in a translatable message
|
* Replaces a placeholder in a translatable message
|
||||||
*
|
*
|
||||||
@@ -259,8 +269,8 @@ public final class StringFormatter {
|
|||||||
*/
|
*/
|
||||||
private String getFormattedMessage(@NotNull String message) {
|
private String getFormattedMessage(@NotNull String message) {
|
||||||
message = ColorHelper.translateColorCodes(message, this.colorConversion);
|
message = ColorHelper.translateColorCodes(message, this.colorConversion);
|
||||||
String coloredPrefix = ColorHelper.translateColorCodes(namePrefix + this.pluginName +
|
String coloredPrefix = ColorHelper.translateColorCodes(this.namePrefix + this.pluginName +
|
||||||
nameSuffix, this.colorConversion);
|
this.nameSuffix, this.colorConversion);
|
||||||
return coloredPrefix + " " + ChatColor.RESET + message;
|
return coloredPrefix + " " + ChatColor.RESET + message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -23,6 +23,16 @@ public final class StringReplacer {
|
|||||||
public StringReplacer() {
|
public StringReplacer() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiates a new string replacer
|
||||||
|
*
|
||||||
|
* @param stringFormatter <p>The string formatter to use for translation</p>
|
||||||
|
* @param translatableMessage <p>The translatable message to replace for</p>
|
||||||
|
*/
|
||||||
|
public StringReplacer(@NotNull StringFormatter stringFormatter, @NotNull TranslatableMessage translatableMessage) {
|
||||||
|
this.replacementInput = stringFormatter.getUnFormattedMessage(translatableMessage);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiates a new string replacer
|
* Instantiates a new string replacer
|
||||||
*
|
*
|
||||||
|
@@ -15,7 +15,8 @@ public interface TranslatableMessage {
|
|||||||
*
|
*
|
||||||
* @return <p>The name of this translatable message</p>
|
* @return <p>The name of this translatable message</p>
|
||||||
*/
|
*/
|
||||||
@NotNull String name();
|
@NotNull
|
||||||
|
String name();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets all translatable messages
|
* Gets all translatable messages
|
||||||
@@ -24,6 +25,7 @@ public interface TranslatableMessage {
|
|||||||
*
|
*
|
||||||
* @return <p>All translatable messages</p>
|
* @return <p>All translatable messages</p>
|
||||||
*/
|
*/
|
||||||
@NotNull TranslatableMessage[] getAllMessages();
|
@NotNull
|
||||||
|
TranslatableMessage[] getAllMessages();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user