diff --git a/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java b/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java new file mode 100644 index 0000000..11816c4 --- /dev/null +++ b/src/main/java/net/knarcraft/knarlib/formatting/FormatBuilder.java @@ -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
The string formatter to use
+ */ + public static void setStringFormatter(@NotNull StringFormatter stringFormatter) { + FormatBuilder.stringFormatter = stringFormatter; + } + + /** + * Instantiates a new format builder + * + * @param translatableMessageThe translatable message to format
+ */ + public FormatBuilder(@NotNull TranslatableMessage translatableMessage) { + this.toFormat = stringFormatter.getUnFormattedMessage(translatableMessage); + } + + /** + * Instantiates a new format builder + * + * @param toFormatThe string to format
+ */ + public FormatBuilder(@NotNull String toFormat) { + this.toFormat = toFormat; + } + + /** + * Replaces placeholders for the current string + * + * @param placeholdersThe placeholders to replace
+ * @param replacementsThe replacements
+ * @returnThis format builder
+ */ + public FormatBuilder replace(@NotNull ListThe placeholder to replace
+ * @param replacementThe replacement
+ * @returnThis format builder
+ */ + 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 placeholdersThe placeholders to replace
+ * @param replacementsThe replacements
+ * @returnThis format builder
+ */ + 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 stringReplacerThe string replacer used for replacement
+ * @returnThis format builder
+ */ + public FormatBuilder replace(@NotNull StringReplacer stringReplacer) { + this.toFormat = stringReplacer.replace(this.toFormat); + return this; + } + + /** + * Converts color codes in the current string + * + *This is performed as part of the success, error and neutral methods, so it's only necessary when displaying + * the message through other means.
+ * + * @returnThis format builder
+ */ + public FormatBuilder color() { + this.toFormat = stringFormatter.getUnFormattedColoredMessage(this.toFormat); + return this; + } + + /** + * Builds the output of the performed replacements and formatting + * + * @returnThe result
+ */ + @NotNull + public String build() { + return this.toFormat; + } + + /** + * Displays the result to the specified command sender as a success message + * + * @param commandSenderThe recipient
+ */ + 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 commandSenderThe recipient
+ */ + 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 commandSenderThe recipient
+ */ + public void neutral(@NotNull CommandSender commandSender) { + if (FormatBuilder.stringFormatter == null) { + throw NOT_SETUP_EXCEPTION; + } + FormatBuilder.stringFormatter.displayNeutralMessage(commandSender, this.toFormat); + } + +} diff --git a/src/main/java/net/knarcraft/knarlib/formatting/StringFormatter.java b/src/main/java/net/knarcraft/knarlib/formatting/StringFormatter.java index 3462196..0691b81 100644 --- a/src/main/java/net/knarcraft/knarlib/formatting/StringFormatter.java +++ b/src/main/java/net/knarcraft/knarlib/formatting/StringFormatter.java @@ -169,6 +169,16 @@ public final class StringFormatter { this.colorConversion); } + /** + * Gets the message with translated color codes + * + * @param messageThe message to get the colored version of
+ * @returnThe colored raw message
+ */ + public @NotNull String getUnFormattedColoredMessage(@NotNull String message) { + return ColorHelper.translateColorCodes(message, this.colorConversion); + } + /** * Replaces a placeholder in a translatable message * @@ -259,8 +269,8 @@ public final class StringFormatter { */ private String getFormattedMessage(@NotNull String message) { message = ColorHelper.translateColorCodes(message, this.colorConversion); - String coloredPrefix = ColorHelper.translateColorCodes(namePrefix + this.pluginName + - nameSuffix, this.colorConversion); + String coloredPrefix = ColorHelper.translateColorCodes(this.namePrefix + this.pluginName + + this.nameSuffix, this.colorConversion); return coloredPrefix + " " + ChatColor.RESET + message; } diff --git a/src/main/java/net/knarcraft/knarlib/formatting/StringReplacer.java b/src/main/java/net/knarcraft/knarlib/formatting/StringReplacer.java index 4384fe2..c703d6e 100644 --- a/src/main/java/net/knarcraft/knarlib/formatting/StringReplacer.java +++ b/src/main/java/net/knarcraft/knarlib/formatting/StringReplacer.java @@ -23,6 +23,16 @@ public final class StringReplacer { public StringReplacer() { } + /** + * Instantiates a new string replacer + * + * @param stringFormatterThe string formatter to use for translation
+ * @param translatableMessageThe translatable message to replace for
+ */ + public StringReplacer(@NotNull StringFormatter stringFormatter, @NotNull TranslatableMessage translatableMessage) { + this.replacementInput = stringFormatter.getUnFormattedMessage(translatableMessage); + } + /** * Instantiates a new string replacer * diff --git a/src/main/java/net/knarcraft/knarlib/formatting/TranslatableMessage.java b/src/main/java/net/knarcraft/knarlib/formatting/TranslatableMessage.java index 803f492..08bd283 100644 --- a/src/main/java/net/knarcraft/knarlib/formatting/TranslatableMessage.java +++ b/src/main/java/net/knarcraft/knarlib/formatting/TranslatableMessage.java @@ -15,7 +15,8 @@ public interface TranslatableMessage { * * @returnThe name of this translatable message
*/ - @NotNull String name(); + @NotNull + String name(); /** * Gets all translatable messages @@ -24,6 +25,7 @@ public interface TranslatableMessage { * * @returnAll translatable messages
*/ - @NotNull TranslatableMessage[] getAllMessages(); + @NotNull + TranslatableMessage[] getAllMessages(); }