diff --git a/pom.xml b/pom.xml index 75e596a..8e3782b 100644 --- a/pom.xml +++ b/pom.xml @@ -90,5 +90,11 @@ 5.9.2 test + + org.jetbrains + annotations + 24.0.1 + compile + diff --git a/src/main/java/net/knarcraft/knarlib/formatting/StringFormatter.java b/src/main/java/net/knarcraft/knarlib/formatting/StringFormatter.java index cacc487..c9daa01 100644 --- a/src/main/java/net/knarcraft/knarlib/formatting/StringFormatter.java +++ b/src/main/java/net/knarcraft/knarlib/formatting/StringFormatter.java @@ -4,6 +4,7 @@ import net.knarcraft.knarlib.property.ColorConversion; import net.knarcraft.knarlib.util.ColorHelper; import net.md_5.bungee.api.ChatColor; import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; /** * A formatter for formatting displayed messages @@ -15,6 +16,9 @@ public final class StringFormatter { private final Translator translator; private ChatColor successColor = ChatColor.GREEN; private ChatColor errorColor = ChatColor.DARK_RED; + private String namePrefix = "["; + private String nameSuffix = "]"; + private ColorConversion colorConversion = ColorConversion.NORMAL; /** * Instantiates a new string formatter @@ -29,17 +33,40 @@ public final class StringFormatter { this.translator = translator; } + /** + * Sets the type of color conversion to use for translated messages + * + * @param colorConversion

The color conversion to use

+ */ + public void setColorConversion(@NotNull ColorConversion colorConversion) { + this.colorConversion = colorConversion; + } + + /** + * Sets the prefix used in front of the plugin name. Normally just "[" + * + * @param namePrefix

The new name prefix

+ */ + public void setNamePrefix(@NotNull String namePrefix) { + this.namePrefix = namePrefix; + } + + /** + * Sets the suffix used after the plugin name. Normally just "]" + * + * @param nameSuffix

The new name suffix

+ */ + public void setNameSuffix(@NotNull String nameSuffix) { + this.nameSuffix = nameSuffix; + } + /** * Sets the color to prepend when using displaySuccessMessage * * @param color

The color to use

*/ - public void setSuccessColor(ChatColor color) { - if (color != null) { - this.successColor = color; - } else { - throw new IllegalArgumentException("Success color cannot be null"); - } + public void setSuccessColor(@NotNull ChatColor color) { + this.successColor = color; } /** @@ -47,12 +74,8 @@ public final class StringFormatter { * * @param color

The color to use

*/ - public void setErrorColor(ChatColor color) { - if (color != null) { - this.errorColor = color; - } else { - throw new IllegalArgumentException("Error color cannot be null"); - } + public void setErrorColor(@NotNull ChatColor color) { + this.errorColor = color; } /** @@ -61,7 +84,7 @@ public final class StringFormatter { * @param sender

The command sender to display the message to

* @param message

The translatable message to display

*/ - public void displayNeutralMessage(CommandSender sender, TranslatableMessage message) { + public void displayNeutralMessage(@NotNull CommandSender sender, @NotNull TranslatableMessage message) { sender.sendMessage(getFormattedMessage(this.translator.getTranslatedMessage(message))); } @@ -71,7 +94,7 @@ public final class StringFormatter { * @param sender

The command sender to display the message to

* @param message

The raw message to display

*/ - public void displayNeutralMessage(CommandSender sender, String message) { + public void displayNeutralMessage(@NotNull CommandSender sender, @NotNull String message) { sender.sendMessage(getFormattedMessage(message)); } @@ -81,7 +104,7 @@ public final class StringFormatter { * @param sender

The command sender to display the message to

* @param message

The translatable message to display

*/ - public void displaySuccessMessage(CommandSender sender, TranslatableMessage message) { + public void displaySuccessMessage(@NotNull CommandSender sender, @NotNull TranslatableMessage message) { sender.sendMessage(successColor + getFormattedMessage(this.translator.getTranslatedMessage(message))); } @@ -91,7 +114,7 @@ public final class StringFormatter { * @param sender

The command sender to display the message to

* @param message

The raw message to display

*/ - public void displaySuccessMessage(CommandSender sender, String message) { + public void displaySuccessMessage(@NotNull CommandSender sender, @NotNull String message) { sender.sendMessage(successColor + getFormattedMessage(message)); } @@ -101,7 +124,7 @@ public final class StringFormatter { * @param sender

The command sender to display the message to

* @param message

The translatable message to display

*/ - public void displayErrorMessage(CommandSender sender, TranslatableMessage message) { + public void displayErrorMessage(@NotNull CommandSender sender, @NotNull TranslatableMessage message) { sender.sendMessage(errorColor + getFormattedMessage(this.translator.getTranslatedMessage(message))); } @@ -111,7 +134,7 @@ public final class StringFormatter { * @param sender

The command sender to display the message to

* @param message

The raw message to display

*/ - public void displayErrorMessage(CommandSender sender, String message) { + public void displayErrorMessage(@NotNull CommandSender sender, @NotNull String message) { sender.sendMessage(errorColor + getFormattedMessage(message)); } @@ -123,7 +146,8 @@ public final class StringFormatter { * @param replacement

The replacement value

* @return

The input string with the placeholder replaced

*/ - public String replacePlaceholder(TranslatableMessage translatableMessage, String placeholder, String replacement) { + public String replacePlaceholder(@NotNull TranslatableMessage translatableMessage, @NotNull String placeholder, + @NotNull String replacement) { return replacePlaceholder(this.translator.getTranslatedMessage(translatableMessage), placeholder, replacement); } @@ -135,8 +159,8 @@ public final class StringFormatter { * @param replacements

The replacement values

* @return

The input string with placeholders replaced

*/ - public String replacePlaceholders(TranslatableMessage translatableMessage, String[] placeholders, - String[] replacements) { + public String replacePlaceholders(@NotNull TranslatableMessage translatableMessage, @NotNull String[] placeholders, + @NotNull String[] replacements) { return replacePlaceholders(this.translator.getTranslatedMessage(translatableMessage), placeholders, replacements); } @@ -148,7 +172,8 @@ public final class StringFormatter { * @param replacement

The replacement value

* @return

The input string with the placeholder replaced

*/ - public static String replacePlaceholder(String input, String placeholder, String replacement) { + public static String replacePlaceholder(@NotNull String input, @NotNull String placeholder, + @NotNull String replacement) { return input.replace(placeholder, replacement); } @@ -160,7 +185,8 @@ public final class StringFormatter { * @param replacements

The replacement values

* @return

The input string with placeholders replaced

*/ - public static String replacePlaceholders(String input, String[] placeholders, String[] replacements) { + public static String replacePlaceholders(@NotNull String input, @NotNull String[] placeholders, + @NotNull String[] replacements) { for (int i = 0; i < Math.min(placeholders.length, replacements.length); i++) { input = replacePlaceholder(input, placeholders[i], replacements[i]); } @@ -173,12 +199,14 @@ public final class StringFormatter { * @param message

The message to format

* @return

The formatted message

*/ - private String getFormattedMessage(String message) { - message = ColorHelper.translateColorCodes(message, ColorConversion.NORMAL); + private String getFormattedMessage(@NotNull String message) { + message = ColorHelper.translateColorCodes(message, this.colorConversion); if (this.pluginName == null) { return message; } else { - return "[" + this.pluginName + "] " + ChatColor.RESET + message; + String coloredPrefix = ColorHelper.translateColorCodes(namePrefix + ChatColor.RESET + + this.pluginName + ChatColor.RESET + nameSuffix, this.colorConversion); + return coloredPrefix + " " + ChatColor.RESET + message; } }