Makes string formatter colors configurable
All checks were successful
EpicKnarvik97/KnarLib/pipeline/head This commit looks good

This commit is contained in:
Kristian Knarvik 2023-01-25 17:03:39 +01:00
parent 5e8b13785d
commit e8d62930ca
2 changed files with 70 additions and 22 deletions

View File

@ -13,6 +13,8 @@ public final class StringFormatter {
private final String pluginName; private final String pluginName;
private final Translator translator; private final Translator translator;
private ChatColor successColor = ChatColor.GREEN;
private ChatColor errorColor = ChatColor.DARK_RED;
/** /**
* Instantiates a new string formatter * Instantiates a new string formatter
@ -27,6 +29,52 @@ public final class StringFormatter {
this.translator = translator; this.translator = translator;
} }
/**
* Sets the color to prepend when using displaySuccessMessage
*
* @param color <p>The color to use</p>
*/
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 <p>The color to use</p>
*/
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 <p>The command sender to display the message to</p>
* @param message <p>The translatable message to display</p>
*/
public void displayNeutralMessage(CommandSender sender, TranslatableMessage message) {
sender.sendMessage(getFormattedMessage(this.translator.getTranslatedMessage(message)));
}
/**
* Displays a formatted message
*
* @param sender <p>The command sender to display the message to</p>
* @param message <p>The raw message to display</p>
*/
public void displayNeutralMessage(CommandSender sender, String message) {
sender.sendMessage(getFormattedMessage(message));
}
/** /**
* Displays a message signifying a successful action * Displays a message signifying a successful action
* *
@ -34,7 +82,7 @@ public final class StringFormatter {
* @param message <p>The translatable message to display</p> * @param message <p>The translatable message to display</p>
*/ */
public void displaySuccessMessage(CommandSender sender, TranslatableMessage message) { 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 <p>The raw message to display</p> * @param message <p>The raw message to display</p>
*/ */
public void displaySuccessMessage(CommandSender sender, String message) { 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 <p>The translatable message to display</p> * @param message <p>The translatable message to display</p>
*/ */
public void displayErrorMessage(CommandSender sender, TranslatableMessage message) { 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 <p>The raw message to display</p> * @param message <p>The raw message to display</p>
*/ */
public void displayErrorMessage(CommandSender sender, String message) { 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 * Replaces a placeholder in a translatable message
*
* @param message <p>The message to format</p>
* @return <p>The formatted message</p>
*/
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
* *
* @param translatableMessage <p>The translatable message to replace in</p> * @param translatableMessage <p>The translatable message to replace in</p>
* @param placeholder <p>The placeholder to replace</p> * @param placeholder <p>The placeholder to replace</p>
@ -95,7 +128,7 @@ public final class StringFormatter {
} }
/** /**
* Replaces placeholders in a string * Replaces placeholders in a translatable message
* *
* @param translatableMessage <p>The translatable message to replace in</p> * @param translatableMessage <p>The translatable message to replace in</p>
* @param placeholders <p>The placeholders to replace</p> * @param placeholders <p>The placeholders to replace</p>
@ -134,4 +167,19 @@ public final class StringFormatter {
return input; return input;
} }
/**
* Gets the formatted version of any chat message
*
* @param message <p>The message to format</p>
* @return <p>The formatted message</p>
*/
private String getFormattedMessage(String message) {
message = ColorHelper.translateColorCodes(message, ColorConversion.NORMAL);
if (this.pluginName == null) {
return message;
} else {
return "[" + this.pluginName + "] " + ChatColor.RESET + message;
}
}
} }

View File

@ -9,7 +9,7 @@ import java.util.Map;
* <p>Add placeholder -> value pairs before running replace on the wanted string</p> * <p>Add placeholder -> value pairs before running replace on the wanted string</p>
*/ */
@SuppressWarnings("unused") @SuppressWarnings("unused")
public class StringReplacer { public final class StringReplacer {
private final Map<String, String> replacements = new HashMap<>(); private final Map<String, String> replacements = new HashMap<>();
private String replacementInput; private String replacementInput;