Adds more options to StringFormatter
All checks were successful
EpicKnarvik97/KnarLib/pipeline/head This commit looks good

Makes it possible to set the prefix before the plugin name
Makes it possible to set the suffix after the plugin name
Makes it possible to set the ColorConversion for translated messages
This commit is contained in:
Kristian Knarvik 2023-07-08 03:01:15 +02:00
parent 58ee5a0756
commit 4fb4429f8b
2 changed files with 60 additions and 26 deletions

View File

@ -90,5 +90,11 @@
<version>5.9.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>24.0.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

View File

@ -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 <p>The color conversion to use</p>
*/
public void setColorConversion(@NotNull ColorConversion colorConversion) {
this.colorConversion = colorConversion;
}
/**
* Sets the prefix used in front of the plugin name. Normally just "["
*
* @param namePrefix <p>The new name prefix</p>
*/
public void setNamePrefix(@NotNull String namePrefix) {
this.namePrefix = namePrefix;
}
/**
* Sets the suffix used after the plugin name. Normally just "]"
*
* @param nameSuffix <p>The new name suffix</p>
*/
public void setNameSuffix(@NotNull String nameSuffix) {
this.nameSuffix = nameSuffix;
}
/**
* Sets the color to prepend when using displaySuccessMessage
*
* @param color <p>The color to use</p>
*/
public void setSuccessColor(ChatColor color) {
if (color != null) {
public void setSuccessColor(@NotNull ChatColor color) {
this.successColor = color;
} else {
throw new IllegalArgumentException("Success color cannot be null");
}
}
/**
@ -47,12 +74,8 @@ public final class StringFormatter {
*
* @param color <p>The color to use</p>
*/
public void setErrorColor(ChatColor color) {
if (color != null) {
public void setErrorColor(@NotNull ChatColor color) {
this.errorColor = color;
} else {
throw new IllegalArgumentException("Error color cannot be null");
}
}
/**
@ -61,7 +84,7 @@ public final class StringFormatter {
* @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) {
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 <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) {
public void displayNeutralMessage(@NotNull CommandSender sender, @NotNull String message) {
sender.sendMessage(getFormattedMessage(message));
}
@ -81,7 +104,7 @@ public final class StringFormatter {
* @param sender <p>The command sender to display the message to</p>
* @param message <p>The translatable message to display</p>
*/
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 <p>The command sender to display the message to</p>
* @param message <p>The raw message to display</p>
*/
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 <p>The command sender to display the message to</p>
* @param message <p>The translatable message to display</p>
*/
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 <p>The command sender to display the message to</p>
* @param message <p>The raw message to display</p>
*/
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 <p>The replacement value</p>
* @return <p>The input string with the placeholder replaced</p>
*/
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 <p>The replacement values</p>
* @return <p>The input string with placeholders replaced</p>
*/
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 <p>The replacement value</p>
* @return <p>The input string with the placeholder replaced</p>
*/
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 <p>The replacement values</p>
* @return <p>The input string with placeholders replaced</p>
*/
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 <p>The message to format</p>
* @return <p>The formatted message</p>
*/
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;
}
}