From e8d62930cab51b28ab81d9f52559ee550f18b5d0 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Wed, 25 Jan 2023 17:03:39 +0100 Subject: [PATCH] Makes string formatter colors configurable --- .../knarlib/formatting/StringFormatter.java | 90 ++++++++++++++----- .../knarlib/formatting/StringReplacer.java | 2 +- 2 files changed, 70 insertions(+), 22 deletions(-) diff --git a/src/main/java/net/knarcraft/knarlib/formatting/StringFormatter.java b/src/main/java/net/knarcraft/knarlib/formatting/StringFormatter.java index dc82bc6..cacc487 100644 --- a/src/main/java/net/knarcraft/knarlib/formatting/StringFormatter.java +++ b/src/main/java/net/knarcraft/knarlib/formatting/StringFormatter.java @@ -13,6 +13,8 @@ public final class StringFormatter { private final String pluginName; private final Translator translator; + private ChatColor successColor = ChatColor.GREEN; + private ChatColor errorColor = ChatColor.DARK_RED; /** * Instantiates a new string formatter @@ -27,6 +29,52 @@ public final class StringFormatter { this.translator = translator; } + /** + * 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"); + } + } + + /** + * Sets the color to prepend when using displayErrorMessage + * + * @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"); + } + } + + /** + * Displays a formatted message + * + * @param sender

The command sender to display the message to

+ * @param message

The translatable message to display

+ */ + public void displayNeutralMessage(CommandSender sender, TranslatableMessage message) { + sender.sendMessage(getFormattedMessage(this.translator.getTranslatedMessage(message))); + } + + /** + * Displays a formatted message + * + * @param sender

The command sender to display the message to

+ * @param message

The raw message to display

+ */ + public void displayNeutralMessage(CommandSender sender, String message) { + sender.sendMessage(getFormattedMessage(message)); + } + /** * Displays a message signifying a successful action * @@ -34,7 +82,7 @@ public final class StringFormatter { * @param message

The translatable message to display

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

The raw message to display

*/ public void displaySuccessMessage(CommandSender sender, String message) { - sender.sendMessage(ChatColor.GREEN + getFormattedMessage(message)); + sender.sendMessage(successColor + getFormattedMessage(message)); } /** @@ -54,7 +102,7 @@ public final class StringFormatter { * @param message

The translatable message to display

*/ public void displayErrorMessage(CommandSender sender, TranslatableMessage message) { - sender.sendMessage(ChatColor.DARK_RED + getFormattedMessage(this.translator.getTranslatedMessage(message))); + sender.sendMessage(errorColor + getFormattedMessage(this.translator.getTranslatedMessage(message))); } /** @@ -64,26 +112,11 @@ public final class StringFormatter { * @param message

The raw message to display

*/ public void displayErrorMessage(CommandSender sender, String message) { - sender.sendMessage(ChatColor.DARK_RED + getFormattedMessage(message)); + sender.sendMessage(errorColor + getFormattedMessage(message)); } /** - * Gets the formatted version of any chat message - * - * @param message

The message to format

- * @return

The formatted message

- */ - private String getFormattedMessage(String message) { - message = ColorHelper.translateColorCodes(message, ColorConversion.NORMAL); - if (this.pluginName == null) { - return message; - } else { - return "[" + this.pluginName + "] " + ChatColor.RESET + message; - } - } - - /** - * Replaces a placeholder in a string + * Replaces a placeholder in a translatable message * * @param translatableMessage

The translatable message to replace in

* @param placeholder

The placeholder to replace

@@ -95,7 +128,7 @@ public final class StringFormatter { } /** - * Replaces placeholders in a string + * Replaces placeholders in a translatable message * * @param translatableMessage

The translatable message to replace in

* @param placeholders

The placeholders to replace

@@ -134,4 +167,19 @@ public final class StringFormatter { return input; } + /** + * Gets the formatted version of any chat message + * + * @param message

The message to format

+ * @return

The formatted message

+ */ + private String getFormattedMessage(String message) { + message = ColorHelper.translateColorCodes(message, ColorConversion.NORMAL); + if (this.pluginName == null) { + return message; + } else { + return "[" + this.pluginName + "] " + 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 2869d74..c858bb5 100644 --- a/src/main/java/net/knarcraft/knarlib/formatting/StringReplacer.java +++ b/src/main/java/net/knarcraft/knarlib/formatting/StringReplacer.java @@ -9,7 +9,7 @@ import java.util.Map; *

Add placeholder -> value pairs before running replace on the wanted string

*/ @SuppressWarnings("unused") -public class StringReplacer { +public final class StringReplacer { private final Map replacements = new HashMap<>(); private String replacementInput;