From 455db78988dd0f97d718b84a609505da9858d994 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Thu, 25 Jan 2024 16:57:10 +0100 Subject: [PATCH] Updates some KnarLib code --- pom.xml | 2 +- .../blacksmith/BlacksmithPlugin.java | 29 ++++--------- .../blacksmith/command/EditCommand.java | 3 +- .../blacksmith/command/PresetCommand.java | 7 ++-- .../config/blacksmith/BlacksmithSetting.java | 6 ++- .../formatting/BlacksmithStringFormatter.java | 7 ++-- .../BlacksmithTranslatableMessage.java | 41 +++++++++++-------- .../blacksmith/formatting/ItemType.java | 7 +++- .../blacksmith/formatting/TimeFormatter.java | 3 +- .../blacksmith/util/ConfigHelper.java | 4 +- 10 files changed, 56 insertions(+), 53 deletions(-) diff --git a/pom.xml b/pom.xml index 236636f..3c829d9 100644 --- a/pom.xml +++ b/pom.xml @@ -87,7 +87,7 @@ net.knarcraft knarlib - 1.2.4 + 1.2.5 compile diff --git a/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java b/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java index a3414f5..9dc007f 100644 --- a/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java +++ b/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java @@ -19,7 +19,6 @@ import net.knarcraft.blacksmith.listener.PlayerListener; import net.knarcraft.blacksmith.manager.EconomyManager; import net.knarcraft.blacksmith.trait.BlacksmithTrait; import net.knarcraft.knarlib.formatting.StringFormatter; -import net.knarcraft.knarlib.formatting.TranslatableMessage; import net.knarcraft.knarlib.formatting.TranslatableTimeUnit; import net.knarcraft.knarlib.formatting.Translator; import net.knarcraft.knarlib.property.ColorConversion; @@ -79,16 +78,6 @@ public class BlacksmithPlugin extends JavaPlugin { return instance; } - /** - * Gets the translation of the given translatable message - * - * @param translatableMessage

The message to translate

- * @return

The message, in the language specified in the configuration

- */ - public static @NotNull String translate(TranslatableMessage translatableMessage) { - return BlacksmithPlugin.getTranslator().getTranslatedMessage(translatableMessage); - } - /** * Gets settings for the blacksmith plugin's blacksmiths * @@ -118,15 +107,6 @@ public class BlacksmithPlugin extends JavaPlugin { this.getConfig().getString("language", "en")); } - /** - * Gets the translator to use for translation - * - * @return

The translator to use

- */ - public static @NotNull Translator getTranslator() { - return BlacksmithPlugin.translator; - } - /** * Gets the string formatter to use for formatting * @@ -136,6 +116,15 @@ public class BlacksmithPlugin extends JavaPlugin { return BlacksmithPlugin.stringFormatter; } + /** + * Gets the translator to use for translation + * + * @return

The translator to use

+ */ + public static @NotNull Translator getTranslator() { + return BlacksmithPlugin.translator; + } + @Override public void onDisable() { getLogger().log(Level.INFO, " v" + getDescription().getVersion() + " disabled."); diff --git a/src/main/java/net/knarcraft/blacksmith/command/EditCommand.java b/src/main/java/net/knarcraft/blacksmith/command/EditCommand.java index eb51c81..9ecd469 100644 --- a/src/main/java/net/knarcraft/blacksmith/command/EditCommand.java +++ b/src/main/java/net/knarcraft/blacksmith/command/EditCommand.java @@ -170,7 +170,8 @@ public abstract class EditCommand, L extends Setting> i getCurrentValueMessage(setting.getCommandName(), rawValue)); } else { //Add a marker if the value has been customized - String marker = BlacksmithPlugin.translate(BlacksmithTranslatableMessage.SETTING_OVERRIDDEN_MARKER); + String marker = BlacksmithPlugin.getStringFormatter().getUnformattedMessage( + BlacksmithTranslatableMessage.SETTING_OVERRIDDEN_MARKER); formatter.displayNeutralMessage(sender, getCurrentValueMessage(setting.getCommandName(), rawValue) + marker); } diff --git a/src/main/java/net/knarcraft/blacksmith/command/PresetCommand.java b/src/main/java/net/knarcraft/blacksmith/command/PresetCommand.java index e451e9d..8dfa3d7 100644 --- a/src/main/java/net/knarcraft/blacksmith/command/PresetCommand.java +++ b/src/main/java/net/knarcraft/blacksmith/command/PresetCommand.java @@ -4,7 +4,6 @@ import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.config.SmithPreset; import net.knarcraft.blacksmith.config.SmithPresetFilter; import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage; -import net.knarcraft.knarlib.formatting.StringFormatter; import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -56,9 +55,9 @@ public class PresetCommand implements CommandExecutor { for (Material material : includedMaterials) { materialNames.add(material.name()); } - BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, StringFormatter.replacePlaceholder( - BlacksmithPlugin.translate(BlacksmithTranslatableMessage.PRESET_MATERIALS), - "{materials}", String.join(", ", materialNames))); + BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, + BlacksmithPlugin.getStringFormatter().replacePlaceholder(BlacksmithTranslatableMessage.PRESET_MATERIALS, + "{materials}", String.join(", ", materialNames))); return true; } diff --git a/src/main/java/net/knarcraft/blacksmith/config/blacksmith/BlacksmithSetting.java b/src/main/java/net/knarcraft/blacksmith/config/blacksmith/BlacksmithSetting.java index d2f5109..88070b7 100644 --- a/src/main/java/net/knarcraft/blacksmith/config/blacksmith/BlacksmithSetting.java +++ b/src/main/java/net/knarcraft/blacksmith/config/blacksmith/BlacksmithSetting.java @@ -5,6 +5,8 @@ import net.knarcraft.blacksmith.config.SettingValueType; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.List; + /** * An enum representing all blacksmith-related settings */ @@ -88,8 +90,8 @@ public enum BlacksmithSetting implements Setting { /** * The setting for the enchantments a blacksmith cannot apply to items */ - ENCHANTMENT_BLOCKLIST("enchantmentBlocklist", SettingValueType.STRING_LIST, new String[]{"binding_curse", - "mending", "vanishing_curse"}, "The enchantments a " + + ENCHANTMENT_BLOCKLIST("enchantmentBlocklist", SettingValueType.STRING_LIST, List.of("binding_curse", + "mending", "vanishing_curse"), "The enchantments a " + "blacksmith is denied from applying to an item. Disable anything you find too op or annoying.", true, false), diff --git a/src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithStringFormatter.java b/src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithStringFormatter.java index fb85ea2..66d7892 100644 --- a/src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithStringFormatter.java +++ b/src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithStringFormatter.java @@ -6,6 +6,8 @@ import net.knarcraft.knarlib.property.ColorConversion; import net.knarcraft.knarlib.util.ColorHelper; import org.bukkit.entity.Player; +import java.util.List; + /** * A formatter for formatting displayed messages */ @@ -24,9 +26,8 @@ public final class BlacksmithStringFormatter { */ public static void sendNPCMessage(NPC npc, Player player, String message) { player.sendMessage(BlacksmithPlugin.getStringFormatter().replacePlaceholders( - BlacksmithTranslatableMessage.NPC_TALK_FORMAT, - new String[]{"{npc}", "{message}"}, - new String[]{npc.getName(), ColorHelper.translateColorCodes(message, ColorConversion.RGB)})); + BlacksmithTranslatableMessage.NPC_TALK_FORMAT, List.of("{npc}", "{message}"), + List.of(npc.getName(), ColorHelper.translateColorCodes(message, ColorConversion.RGB)))); } } diff --git a/src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithTranslatableMessage.java b/src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithTranslatableMessage.java index da903a6..4cea4e1 100644 --- a/src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithTranslatableMessage.java +++ b/src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithTranslatableMessage.java @@ -1,9 +1,11 @@ package net.knarcraft.blacksmith.formatting; import net.knarcraft.blacksmith.BlacksmithPlugin; -import net.knarcraft.knarlib.formatting.StringFormatter; +import net.knarcraft.knarlib.formatting.StringReplacer; import net.knarcraft.knarlib.formatting.TranslatableMessage; +import java.util.List; + /** * An enum containing all translatable global messages * @@ -144,8 +146,8 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage { * @return

The message to display

*/ public static String getRawValueMessage(String rawValue) { - return StringFormatter.replacePlaceholder(BlacksmithPlugin.translate( - BlacksmithTranslatableMessage.RAW_VALUE), "{rawValue}", rawValue); + return BlacksmithPlugin.getStringFormatter().replacePlaceholder(BlacksmithTranslatableMessage.RAW_VALUE, + "{rawValue}", rawValue); } /** @@ -156,9 +158,8 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage { * @return

The string to display to a user

*/ public static String getValueChangedMessage(String setting, String newValue) { - return StringFormatter.replacePlaceholders(BlacksmithPlugin.translate( - BlacksmithTranslatableMessage.VALUE_CHANGED), new String[]{"{setting}", "{newValue}"}, - new String[]{setting, newValue}); + return BlacksmithPlugin.getStringFormatter().replacePlaceholders(BlacksmithTranslatableMessage.VALUE_CHANGED, + List.of("{setting}", "{newValue}"), List.of(setting, newValue)); } /** @@ -171,10 +172,13 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage { * @return

The string to display to a user

*/ public static String getItemValueChangedMessage(String setting, ItemType itemType, String item, String newValue) { - return StringFormatter.replacePlaceholders(BlacksmithPlugin.translate( - BlacksmithTranslatableMessage.VALUE_FOR_ITEM_CHANGED), - new String[]{"{setting}", "{itemType}", "{item}", "{newValue}"}, - new String[]{setting, itemType.getItemTypeName(), item, newValue}); + StringReplacer stringReplacer = new StringReplacer(BlacksmithPlugin.getStringFormatter().getUnformattedMessage( + BlacksmithTranslatableMessage.VALUE_FOR_ITEM_CHANGED)); + stringReplacer.add("{setting}", setting); + stringReplacer.add("{itemType}", itemType.getItemTypeName()); + stringReplacer.add("{item}", item); + stringReplacer.add("{newValue}", newValue); + return stringReplacer.replace(); } /** @@ -185,9 +189,9 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage { * @return

The string to display to a user

*/ public static String getCurrentValueMessage(String setting, String currentValue) { - return StringFormatter.replacePlaceholders(BlacksmithPlugin.translate( - BlacksmithTranslatableMessage.CURRENT_VALUE), new String[]{"{setting}", "{currentValue}"}, - new String[]{setting, currentValue}); + return BlacksmithPlugin.getStringFormatter().replacePlaceholders(BlacksmithTranslatableMessage.CURRENT_VALUE, + List.of("{setting}", "{currentValue}"), + List.of(setting, currentValue)); } /** @@ -200,10 +204,13 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage { * @return

The string to display to a user

*/ public static String getItemCurrentValueMessage(String setting, ItemType itemType, String item, String currentValue) { - return StringFormatter.replacePlaceholders(BlacksmithPlugin.translate( - BlacksmithTranslatableMessage.CURRENT_VALUE_FOR_ITEM), - new String[]{"{setting}", "{itemType}", "{item}", "{currentValue}"}, - new String[]{setting, itemType.getItemTypeName(), item, currentValue}); + StringReplacer stringReplacer = new StringReplacer(BlacksmithPlugin.getStringFormatter().getUnformattedMessage( + BlacksmithTranslatableMessage.CURRENT_VALUE_FOR_ITEM)); + stringReplacer.add("{setting}", setting); + stringReplacer.add("{itemType}", itemType.getItemTypeName()); + stringReplacer.add("{item}", item); + stringReplacer.add("{currentValue}", currentValue); + return stringReplacer.replace(); } @Override diff --git a/src/main/java/net/knarcraft/blacksmith/formatting/ItemType.java b/src/main/java/net/knarcraft/blacksmith/formatting/ItemType.java index 235754a..392c471 100644 --- a/src/main/java/net/knarcraft/blacksmith/formatting/ItemType.java +++ b/src/main/java/net/knarcraft/blacksmith/formatting/ItemType.java @@ -1,6 +1,7 @@ package net.knarcraft.blacksmith.formatting; import net.knarcraft.blacksmith.BlacksmithPlugin; +import net.knarcraft.knarlib.formatting.StringFormatter; /** * An enum representing all item types used in messages @@ -16,9 +17,11 @@ public enum ItemType { * @return

The name of this item type

*/ public String getItemTypeName() { + StringFormatter stringFormatter = BlacksmithPlugin.getStringFormatter(); return switch (this) { - case MATERIAL -> BlacksmithPlugin.translate(BlacksmithTranslatableMessage.ITEM_TYPE_MATERIAL); - case ENCHANTMENT -> BlacksmithPlugin.translate(BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT); + case MATERIAL -> stringFormatter.getUnformattedMessage(BlacksmithTranslatableMessage.ITEM_TYPE_MATERIAL); + case ENCHANTMENT -> + stringFormatter.getUnformattedMessage(BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT); }; } diff --git a/src/main/java/net/knarcraft/blacksmith/formatting/TimeFormatter.java b/src/main/java/net/knarcraft/blacksmith/formatting/TimeFormatter.java index 51a625c..0d8b870 100644 --- a/src/main/java/net/knarcraft/blacksmith/formatting/TimeFormatter.java +++ b/src/main/java/net/knarcraft/blacksmith/formatting/TimeFormatter.java @@ -53,7 +53,8 @@ public final class TimeFormatter { * @return

Text describing the time interval

*/ private static String getMessageFromInterval(TimeInterval interval) { - String text = BlacksmithPlugin.translate(BlacksmithTranslatableMessage.valueOf(interval.name())); + String text = BlacksmithPlugin.getStringFormatter().getUnformattedMessage( + BlacksmithTranslatableMessage.valueOf(interval.name())); //Choose a random entry if a comma-separated list is provided if (text.contains(",")) { diff --git a/src/main/java/net/knarcraft/blacksmith/util/ConfigHelper.java b/src/main/java/net/knarcraft/blacksmith/util/ConfigHelper.java index 62369c9..8ae38e7 100644 --- a/src/main/java/net/knarcraft/blacksmith/util/ConfigHelper.java +++ b/src/main/java/net/knarcraft/blacksmith/util/ConfigHelper.java @@ -25,8 +25,8 @@ public final class ConfigHelper { if (value == null) { return new ArrayList<>(); } - if (value instanceof String) { - return List.of(((String) value).split(",")); + if (value instanceof String string) { + return List.of((string).split(",")); } else if (value instanceof List list) { List strings = new ArrayList<>(); for (Object object : list) {