From ca3d29d7302d3661ff1520fdfa272e7ebde244d7 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Sat, 6 Sep 2025 14:58:55 +0200 Subject: [PATCH] Updates KnarLib --- pom.xml | 2 +- .../blacksmith/BlacksmithPlugin.java | 99 +------ .../blacksmith/command/EditCommand.java | 26 +- .../blacksmith/command/PresetCommand.java | 15 +- .../blacksmith/command/ReloadCommand.java | 5 +- .../blacksmith/BlackSmithConfigCommand.java | 23 +- .../config/StargateYamlConfiguration.java | 246 ------------------ .../blacksmith/GlobalBlacksmithSettings.java | 6 +- .../scrapper/GlobalScrapperSettings.java | 4 +- .../formatting/BlacksmithStringFormatter.java | 33 --- .../blacksmith/formatting/ItemType.java | 9 +- .../blacksmith/formatting/NPCFormatter.java | 40 +++ .../blacksmith/formatting/TimeFormatter.java | 4 +- ...slatableMessage.java => Translatable.java} | 74 +++--- .../blacksmith/listener/NPCClickListener.java | 6 +- .../blacksmith/trait/BlacksmithTrait.java | 19 +- .../blacksmith/trait/CustomTrait.java | 17 +- .../blacksmith/trait/ReforgeSession.java | 9 +- .../blacksmith/trait/SalvageSession.java | 13 +- .../blacksmith/trait/ScrapperTrait.java | 31 ++- .../blacksmith/util/ConfigCommandHelper.java | 24 +- .../blacksmith/util/ConfigHelper.java | 111 -------- .../blacksmith/util/TypeValidationHelper.java | 25 +- 23 files changed, 186 insertions(+), 655 deletions(-) delete mode 100644 src/main/java/net/knarcraft/blacksmith/config/StargateYamlConfiguration.java delete mode 100644 src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithStringFormatter.java create mode 100644 src/main/java/net/knarcraft/blacksmith/formatting/NPCFormatter.java rename src/main/java/net/knarcraft/blacksmith/formatting/{BlacksmithTranslatableMessage.java => Translatable.java} (64%) diff --git a/pom.xml b/pom.xml index d374efa..c68b0fc 100644 --- a/pom.xml +++ b/pom.xml @@ -83,7 +83,7 @@ net.knarcraft knarlib - 1.2.7 + 1.2.14 compile diff --git a/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java b/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java index ec09cd0..51c372f 100644 --- a/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java +++ b/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java @@ -13,49 +13,40 @@ import net.knarcraft.blacksmith.command.scrapper.ScrapperConfigCommand; import net.knarcraft.blacksmith.command.scrapper.ScrapperConfigTabCompleter; import net.knarcraft.blacksmith.command.scrapper.ScrapperEditCommand; import net.knarcraft.blacksmith.command.scrapper.ScrapperEditTabCompleter; -import net.knarcraft.blacksmith.config.StargateYamlConfiguration; import net.knarcraft.blacksmith.config.blacksmith.GlobalBlacksmithSettings; import net.knarcraft.blacksmith.config.scrapper.GlobalScrapperSettings; -import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage; +import net.knarcraft.blacksmith.formatting.Translatable; import net.knarcraft.blacksmith.listener.NPCClickListener; import net.knarcraft.blacksmith.listener.PlayerListener; import net.knarcraft.blacksmith.manager.EconomyManager; import net.knarcraft.blacksmith.trait.BlacksmithTrait; import net.knarcraft.blacksmith.trait.ScrapperTrait; -import net.knarcraft.blacksmith.util.ConfigHelper; +import net.knarcraft.knarlib.formatting.FormatBuilder; import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.knarlib.formatting.TranslatableTimeUnit; import net.knarcraft.knarlib.formatting.Translator; +import net.knarcraft.knarlib.plugin.ConfigCommentPlugin; +import net.knarcraft.knarlib.util.ConfigHelper; import net.knarcraft.knarlib.util.UpdateChecker; -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.PluginCommand; -import org.bukkit.command.TabCompleter; -import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.event.Event; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.PluginManager; -import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPluginLoader; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import java.io.File; -import java.io.IOException; import java.util.logging.Level; /** * Blacksmith's main class */ -public class BlacksmithPlugin extends JavaPlugin { +public class BlacksmithPlugin extends ConfigCommentPlugin { - private static final String CONFIG_FILE_NAME = "config.yml"; private static BlacksmithPlugin instance; private GlobalBlacksmithSettings blacksmithConfig; private GlobalScrapperSettings scrapperConfig; private static Translator translator; - private static StringFormatter stringFormatter; - private FileConfiguration configuration; /** * Constructor required for MockBukkit @@ -108,29 +99,10 @@ public class BlacksmithPlugin extends JavaPlugin { this.reloadConfig(); blacksmithConfig.load(); scrapperConfig.load(); - translator.loadLanguages(this.getDataFolder(), "en", this.getConfiguration().getString( + translator.loadLanguages(this.getDataFolder(), "en", this.getConfig().getString( "language", "en")); } - /** - * Gets the raw configuration - * - * @return

The raw configuration

- */ - @NotNull - public FileConfiguration getConfiguration() { - return this.configuration; - } - - /** - * Gets the string formatter to use for formatting - * - * @return

The string formatter to use

- */ - public static @NotNull StringFormatter getStringFormatter() { - return BlacksmithPlugin.stringFormatter; - } - /** * Gets the translator to use for translation * @@ -148,20 +120,13 @@ public class BlacksmithPlugin extends JavaPlugin { @Override public void onEnable() { instance = this; - - //Copy default config to disk, and add missing configuration values - this.saveDefaultConfig(); - this.getConfig().options().copyDefaults(true); - this.reloadConfig(); - this.saveConfig(); + ConfigHelper.saveDefaults(this); //Migrate from an earlier configuration file syntax if necessary - if (getConfiguration().getString("scrapper.defaults.dropItem") == null) { - ConfigHelper.migrateConfig(this.getDataFolder().getPath().replaceAll("\\\\", "/"), - getConfiguration()); - this.reloadConfig(); + if (getConfig().getString("scrapper.defaults.dropItem") == null) { + net.knarcraft.knarlib.util.ConfigHelper.migrateConfig(this); } - initializeConfigurations(getConfiguration()); + initializeConfigurations(getConfig()); //Set up Vault integration if (!setUpVault()) { @@ -185,28 +150,6 @@ public class BlacksmithPlugin extends JavaPlugin { () -> this.getDescription().getVersion(), null); } - @Override - public void reloadConfig() { - super.reloadConfig(); - this.configuration = new StargateYamlConfiguration(); - this.configuration.options().copyDefaults(true); - try { - this.configuration.load(new File(getDataFolder(), CONFIG_FILE_NAME)); - } catch (IOException | InvalidConfigurationException exception) { - error("Unable to load the configuration! Message: " + exception.getMessage()); - } - } - - @Override - public void saveConfig() { - super.saveConfig(); - try { - this.configuration.save(new File(getDataFolder(), CONFIG_FILE_NAME)); - } catch (IOException exception) { - error("Unable to save the configuration! Message: " + exception.getMessage()); - } - } - /** * Calls an event * @@ -267,7 +210,7 @@ public class BlacksmithPlugin extends JavaPlugin { //Prepare the translator translator = new Translator(); translator.registerMessageCategory(TranslatableTimeUnit.UNIT_SECOND); - translator.registerMessageCategory(BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT); + translator.registerMessageCategory(Translatable.ITEM_TYPE_ENCHANTMENT); translator.loadLanguages(this.getDataFolder(), "en", fileConfiguration.getString("language", "en")); PluginDescriptionFile description = this.getDescription(); @@ -277,7 +220,7 @@ public class BlacksmithPlugin extends JavaPlugin { } else { prefix = description.getPrefix(); } - BlacksmithPlugin.stringFormatter = new StringFormatter(prefix, translator); + FormatBuilder.setStringFormatter(new StringFormatter(prefix, translator)); // This reload is necessary to get values just added to the configuration for some reason this.reload(); @@ -319,22 +262,4 @@ public class BlacksmithPlugin extends JavaPlugin { registerCommand("preset", new PresetCommand(), new PresetTabCompleter()); } - /** - * Registers a command - * - * @param commandName

The name of the command

- * @param executor

The executor to bind to the command

- * @param tabCompleter

The tab completer to bind to the command, or null

- */ - private void registerCommand(@NotNull String commandName, @NotNull CommandExecutor executor, - @Nullable TabCompleter tabCompleter) { - PluginCommand command = this.getCommand(commandName); - if (command != null) { - command.setExecutor(executor); - if (tabCompleter != null) { - command.setTabCompleter(tabCompleter); - } - } - } - } diff --git a/src/main/java/net/knarcraft/blacksmith/command/EditCommand.java b/src/main/java/net/knarcraft/blacksmith/command/EditCommand.java index 1a0418b..dd83968 100644 --- a/src/main/java/net/knarcraft/blacksmith/command/EditCommand.java +++ b/src/main/java/net/knarcraft/blacksmith/command/EditCommand.java @@ -6,12 +6,12 @@ import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.config.Setting; import net.knarcraft.blacksmith.config.SettingValueType; import net.knarcraft.blacksmith.config.Settings; -import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage; +import net.knarcraft.blacksmith.formatting.Translatable; import net.knarcraft.blacksmith.trait.CustomTrait; import net.knarcraft.blacksmith.util.ConfigCommandHelper; import net.knarcraft.blacksmith.util.InputParsingHelper; import net.knarcraft.blacksmith.util.TypeValidationHelper; -import net.knarcraft.knarlib.formatting.StringFormatter; +import net.knarcraft.knarlib.formatting.FormatBuilder; import net.md_5.bungee.api.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -21,9 +21,9 @@ import org.jetbrains.annotations.Nullable; import java.util.Arrays; -import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getCurrentValueMessage; -import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getDefaultValueMessage; -import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getValueChangedMessage; +import static net.knarcraft.blacksmith.formatting.Translatable.getCurrentValueMessage; +import static net.knarcraft.blacksmith.formatting.Translatable.getDefaultValueMessage; +import static net.knarcraft.blacksmith.formatting.Translatable.getValueChangedMessage; /** * A generic implementation of the edit command @@ -64,8 +64,7 @@ public abstract class EditCommand, L extends Setting> i @NotNull String[] args) { NPC npc = CitizensAPI.getDefaultNPCSelector().getSelected(sender); if (npc == null || !npc.hasTrait(traitClass)) { - BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender, - BlacksmithTranslatableMessage.NO_NPC_SELECTED); + new FormatBuilder(Translatable.NO_NPC_SELECTED).error(sender); return true; } @@ -130,8 +129,7 @@ public abstract class EditCommand, L extends Setting> i return false; } settings.changeValue(setting, newValue); - BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, - getValueChangedMessage(setting.getCommandName(), String.valueOf(newValue))); + getValueChangedMessage(setting.getCommandName(), String.valueOf(newValue)).success(sender); //Save the changes immediately to prevent data loss on server crash CitizensAPI.getNPCRegistry().saveToStore(); } @@ -156,21 +154,19 @@ public abstract class EditCommand, L extends Setting> i return; } - StringFormatter formatter = BlacksmithPlugin.getStringFormatter(); - //Find the value of the specified setting String commandName = setting.getCommandName(); String currentValue = String.valueOf(settings.getRawValue(setting)); String defaultValue = String.valueOf(setting.getDefaultValue()); - String marker = formatter.getUnFormattedMessage(BlacksmithTranslatableMessage.SETTING_OVERRIDDEN_MARKER); + String marker = new FormatBuilder(Translatable.SETTING_OVERRIDDEN_MARKER).toString(); boolean isMessage = setting.isMessage(); boolean isSet = !InputParsingHelper.isEmpty(currentValue); // Display the description for how this setting is used - formatter.displaySuccessMessage(sender, setting.getDescription()); + new FormatBuilder(setting.getDescription()).success(sender); // Display the default value - formatter.displayNeutralMessage(sender, getDefaultValueMessage(commandName, defaultValue)); + getDefaultValueMessage(commandName, defaultValue).neutral(sender); if (isMessage) { ConfigCommandHelper.displayRaw(sender, defaultValue); } @@ -181,7 +177,7 @@ public abstract class EditCommand, L extends Setting> i } // Display the value with a marker if it's customized - formatter.displayNeutralMessage(sender, getCurrentValueMessage(commandName, currentValue) + (isSet ? marker : "")); + new FormatBuilder(getCurrentValueMessage(commandName, currentValue) + (isSet ? marker : "")).neutral(sender); if (isMessage) { ConfigCommandHelper.displayRaw(sender, currentValue); diff --git a/src/main/java/net/knarcraft/blacksmith/command/PresetCommand.java b/src/main/java/net/knarcraft/blacksmith/command/PresetCommand.java index fd4b264..a7117d2 100644 --- a/src/main/java/net/knarcraft/blacksmith/command/PresetCommand.java +++ b/src/main/java/net/knarcraft/blacksmith/command/PresetCommand.java @@ -1,9 +1,9 @@ package net.knarcraft.blacksmith.command; -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.blacksmith.formatting.Translatable; +import net.knarcraft.knarlib.formatting.FormatBuilder; import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -37,8 +37,7 @@ public class PresetCommand implements CommandExecutor { SmithPresetFilter filter = SmithPresetFilter.valueOf(parts[1]); if (!smithPreset.supportsFilter(filter)) { - BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender, - BlacksmithTranslatableMessage.INVALID_FILTER_FOR_PRESET); + new FormatBuilder(Translatable.INVALID_FILTER_FOR_PRESET).error(sender); return false; } includedMaterials = smithPreset.getFilteredMaterials(filter); @@ -46,8 +45,7 @@ public class PresetCommand implements CommandExecutor { includedMaterials = SmithPreset.valueOf(presetName).getMaterials(); } } catch (IllegalArgumentException exception) { - BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender, - BlacksmithTranslatableMessage.INVALID_PRESET_OR_FILTER); + new FormatBuilder(Translatable.INVALID_PRESET_OR_FILTER).error(sender); return false; } @@ -56,9 +54,8 @@ public class PresetCommand implements CommandExecutor { for (Material material : includedMaterials) { materialNames.add(material.name()); } - BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, - BlacksmithPlugin.getStringFormatter().replacePlaceholder(BlacksmithTranslatableMessage.PRESET_MATERIALS, - "{materials}", String.join(", ", materialNames))); + new FormatBuilder(Translatable.PRESET_MATERIALS).replace("{materials}", + String.join(", ", materialNames)).success(sender); return true; } diff --git a/src/main/java/net/knarcraft/blacksmith/command/ReloadCommand.java b/src/main/java/net/knarcraft/blacksmith/command/ReloadCommand.java index f3247d5..9ad9c1f 100644 --- a/src/main/java/net/knarcraft/blacksmith/command/ReloadCommand.java +++ b/src/main/java/net/knarcraft/blacksmith/command/ReloadCommand.java @@ -1,7 +1,8 @@ package net.knarcraft.blacksmith.command; import net.knarcraft.blacksmith.BlacksmithPlugin; -import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage; +import net.knarcraft.blacksmith.formatting.Translatable; +import net.knarcraft.knarlib.formatting.FormatBuilder; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabExecutor; @@ -20,7 +21,7 @@ public class ReloadCommand implements TabExecutor { public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { BlacksmithPlugin.getInstance().reload(); - BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, BlacksmithTranslatableMessage.PLUGIN_RELOADED); + new FormatBuilder(Translatable.PLUGIN_RELOADED).success(sender); return true; } diff --git a/src/main/java/net/knarcraft/blacksmith/command/blacksmith/BlackSmithConfigCommand.java b/src/main/java/net/knarcraft/blacksmith/command/blacksmith/BlackSmithConfigCommand.java index 576d95f..d3f9434 100644 --- a/src/main/java/net/knarcraft/blacksmith/command/blacksmith/BlackSmithConfigCommand.java +++ b/src/main/java/net/knarcraft/blacksmith/command/blacksmith/BlackSmithConfigCommand.java @@ -5,8 +5,8 @@ import net.knarcraft.blacksmith.command.ReloadCommand; import net.knarcraft.blacksmith.config.SettingValueType; import net.knarcraft.blacksmith.config.blacksmith.BlacksmithSetting; import net.knarcraft.blacksmith.config.blacksmith.GlobalBlacksmithSettings; -import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage; import net.knarcraft.blacksmith.formatting.ItemType; +import net.knarcraft.blacksmith.formatting.Translatable; import net.knarcraft.blacksmith.util.ConfigCommandHelper; import net.knarcraft.blacksmith.util.InputParsingHelper; import net.knarcraft.blacksmith.util.ItemHelper; @@ -92,19 +92,17 @@ public class BlackSmithConfigCommand implements CommandExecutor { } else { currentValue = String.valueOf(settings.getPricePerDurabilityPoint(material)); } - BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, - BlacksmithTranslatableMessage.getItemCurrentValueMessage(setting.getCommandName(), - ItemType.MATERIAL, material.name(), currentValue)); + Translatable.getItemCurrentValueMessage(setting.getCommandName(), + ItemType.MATERIAL, material.name(), currentValue).success(sender); return true; } else if (setting == BlacksmithSetting.ENCHANTMENT_COST) { Enchantment enchantment = InputParsingHelper.matchEnchantment(selector); if (enchantment == null) { return false; } - BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, - BlacksmithTranslatableMessage.getItemCurrentValueMessage(setting.getCommandName(), - ItemType.ENCHANTMENT, enchantment.getKey().getKey(), - String.valueOf(settings.getEnchantmentCost(enchantment)))); + Translatable.getItemCurrentValueMessage(setting.getCommandName(), + ItemType.ENCHANTMENT, enchantment.getKey().getKey(), + String.valueOf(settings.getEnchantmentCost(enchantment))).success(sender); return true; } else { return false; @@ -156,9 +154,7 @@ public class BlackSmithConfigCommand implements CommandExecutor { ItemType itemType = ItemType.ENCHANTMENT; String itemChanged = enchantment.getKey().getKey(); settings.setEnchantmentCost(enchantment, newPrice); - BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, - BlacksmithTranslatableMessage.getItemValueChangedMessage(blacksmithSetting.getCommandName(), - itemType, itemChanged, newValue)); + Translatable.getItemValueChangedMessage(blacksmithSetting.getCommandName(), itemType, itemChanged, newValue).success(sender); return true; } else { return false; @@ -198,9 +194,8 @@ public class BlackSmithConfigCommand implements CommandExecutor { } } - BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, - BlacksmithTranslatableMessage.getItemValueChangedMessage(blacksmithSetting.getCommandName(), - itemType, itemChanged, String.valueOf(newPrice))); + Translatable.getItemValueChangedMessage(blacksmithSetting.getCommandName(), + itemType, itemChanged, String.valueOf(newPrice)).success(sender); return true; } diff --git a/src/main/java/net/knarcraft/blacksmith/config/StargateYamlConfiguration.java b/src/main/java/net/knarcraft/blacksmith/config/StargateYamlConfiguration.java deleted file mode 100644 index 05aa5b0..0000000 --- a/src/main/java/net/knarcraft/blacksmith/config/StargateYamlConfiguration.java +++ /dev/null @@ -1,246 +0,0 @@ -package net.knarcraft.blacksmith.config; - -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.InvalidConfigurationException; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; -import org.jetbrains.annotations.NotNull; - -import java.io.File; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -/** - * A YAML configuration which retains all comments - * - *

This configuration converts all comments to YAML values when loaded, which all start with comment_. When saved, - * those YAML values are converted to normal text comments. This ensures that the comments aren't removed by the - * YamlConfiguration during its parsing.

- * - *

Note: When retrieving a configuration section, that will include comments that start with "comment_". You should - * filter those before parsing input.

- * - * @author Kristian Knarvik - * @author Thorin - */ -public class StargateYamlConfiguration extends YamlConfiguration { - - private static final String START_OF_COMMENT_LINE = "[HASHTAG]"; - private static final String END_OF_COMMENT = "_endOfComment_"; - private static final String START_OF_COMMENT = "comment_"; - - @Override - @NotNull - @SuppressWarnings("deprecation") - protected String buildHeader() { - return ""; - } - - @Override - @NotNull - public String saveToString() { - // Convert YAML comments to normal comments - return this.convertYAMLMappingsToComments(super.saveToString()); - } - - @Override - public void loadFromString(@NotNull String contents) throws InvalidConfigurationException { - // Convert normal comments to YAML comments to prevent them from disappearing - super.loadFromString(this.convertCommentsToYAMLMappings(contents)); - } - - /** - * Gets a configuration section's keys, without any comment entries - * - * @param configurationSection

The configuration section to get keys for

- * @param deep

Whether to get keys for child elements as well

- * @return

The configuration section's keys, with comment entries removed

- */ - public static Set getKeysWithoutComments(@NotNull ConfigurationSection configurationSection, boolean deep) { - Set keys = new HashSet<>(configurationSection.getKeys(deep)); - keys.removeIf(key -> key.matches(START_OF_COMMENT + "[0-9]+")); - return keys; - } - - /** - * Reads a file with comments, and recreates them into yaml mappings - * - *

A mapping follows this format: comment_{CommentNumber}: "The comment" - * This needs to be done as comments otherwise get removed using - * the {@link FileConfiguration#save(File)} method. The config - * needs to be saved if a config value has changed.

- */ - @NotNull - private String convertCommentsToYAMLMappings(@NotNull String configString) { - StringBuilder yamlBuilder = new StringBuilder(); - List currentComment = new ArrayList<>(); - int commentId = 0; - int previousIndentation = 0; - - for (String line : configString.split("\n")) { - String trimmed = line.trim(); - if (trimmed.startsWith("#")) { - // Store the indentation of the block - if (currentComment.isEmpty()) { - previousIndentation = getIndentation(line); - } - //Temporarily store the comment line - addComment(currentComment, trimmed); - } else { - addYamlString(yamlBuilder, currentComment, line, previousIndentation, commentId); - commentId++; - previousIndentation = 0; - } - } - return yamlBuilder.toString(); - } - - /** - * Adds a YAML string to the given string builder - * - * @param yamlBuilder

The string builder used for building YAML

- * @param currentComment

The comment to add as a YAML string

- * @param line

The current line

- * @param previousIndentation

The indentation of the current block comment

- * @param commentId

The id of the comment

- */ - private void addYamlString(@NotNull StringBuilder yamlBuilder, @NotNull List currentComment, - @NotNull String line, int previousIndentation, int commentId) { - String trimmed = line.trim(); - //Write the full formatted comment to the StringBuilder - if (!currentComment.isEmpty()) { - int indentation = trimmed.isEmpty() ? previousIndentation : getIndentation(line); - generateCommentYAML(yamlBuilder, currentComment, commentId, indentation); - currentComment.clear(); - } - //Add the non-comment line assuming it isn't empty - if (!trimmed.isEmpty()) { - yamlBuilder.append(line).append("\n"); - } - } - - /** - * Adds the given comment to the given list - * - * @param commentParts

The list to add to

- * @param comment

The comment to add

- */ - private void addComment(@NotNull List commentParts, @NotNull String comment) { - if (comment.startsWith("# ")) { - commentParts.add(comment.replaceFirst("# ", START_OF_COMMENT_LINE)); - } else { - commentParts.add(comment.replaceFirst("#", START_OF_COMMENT_LINE)); - } - } - - /** - * Generates a YAML-compatible string for one comment block - * - * @param yamlBuilder

The string builder to add the generated YAML to

- * @param commentLines

The lines of the comment to convert into YAML

- * @param commentId

The unique id of the comment

- * @param indentation

The indentation to add to every line

- */ - private void generateCommentYAML(@NotNull StringBuilder yamlBuilder, @NotNull List commentLines, - int commentId, int indentation) { - String subIndentation = this.addIndentation(indentation + 2); - //Add the comment start marker - yamlBuilder.append(this.addIndentation(indentation)).append(START_OF_COMMENT).append(commentId).append(": |\n"); - for (String commentLine : commentLines) { - //Add each comment line with the proper indentation - yamlBuilder.append(subIndentation).append(commentLine).append("\n"); - } - //Add the comment end marker - yamlBuilder.append(subIndentation).append(subIndentation).append(END_OF_COMMENT).append("\n"); - } - - /** - * Converts the internal YAML mapping format to a readable config file - * - *

The internal YAML structure is converted to a string with the same format as a standard configuration file. - * The internal structure has comments in the format: START_OF_COMMENT + id + multi-line YAML string + - * END_OF_COMMENT.

- * - * @param yamlString

A string using the YAML format

- * @return

The corresponding comment string

- */ - @NotNull - private String convertYAMLMappingsToComments(@NotNull String yamlString) { - StringBuilder finalText = new StringBuilder(); - - String[] lines = yamlString.split("\n"); - for (int currentIndex = 0; currentIndex < lines.length; currentIndex++) { - String line = lines[currentIndex]; - String possibleComment = line.trim(); - - if (possibleComment.startsWith(START_OF_COMMENT)) { - //Add an empty line before every comment block - finalText.append("\n"); - currentIndex = readComment(finalText, lines, currentIndex + 1, getIndentation(line)); - } else { - //Output the configuration key - finalText.append(line).append("\n"); - } - } - - return finalText.toString().trim(); - } - - /** - * Fully reads a comment - * - * @param builder

The string builder to write to

- * @param lines

The lines to read from

- * @param startIndex

The index to start reading from

- * @param commentIndentation

The indentation of the read comment

- * @return

The index containing the next non-comment line

- */ - private int readComment(@NotNull StringBuilder builder, @NotNull String[] lines, int startIndex, - int commentIndentation) { - for (int currentIndex = startIndex; currentIndex < lines.length; currentIndex++) { - String line = lines[currentIndex]; - String possibleComment = line.trim(); - if (!line.contains(END_OF_COMMENT)) { - possibleComment = possibleComment.replace(START_OF_COMMENT_LINE, ""); - builder.append(addIndentation(commentIndentation)).append("# ").append(possibleComment).append("\n"); - } else { - return currentIndex; - } - } - - return startIndex; - } - - /** - * Gets a string containing the given indentation - * - * @param indentationSpaces

The number spaces to use for indentation

- * @return

A string containing the number of spaces specified

- */ - @NotNull - private String addIndentation(int indentationSpaces) { - return " ".repeat(Math.max(0, indentationSpaces)); - } - - - /** - * Gets the indentation (number of spaces) of the given line - * - * @param line

The line to get indentation of

- * @return

The number of spaces in the line's indentation

- */ - private int getIndentation(@NotNull String line) { - int spacesFound = 0; - for (char aCharacter : line.toCharArray()) { - if (aCharacter == ' ') { - spacesFound++; - } else { - break; - } - } - return spacesFound; - } - -} \ No newline at end of file diff --git a/src/main/java/net/knarcraft/blacksmith/config/blacksmith/GlobalBlacksmithSettings.java b/src/main/java/net/knarcraft/blacksmith/config/blacksmith/GlobalBlacksmithSettings.java index 542e01f..25f4590 100644 --- a/src/main/java/net/knarcraft/blacksmith/config/blacksmith/GlobalBlacksmithSettings.java +++ b/src/main/java/net/knarcraft/blacksmith/config/blacksmith/GlobalBlacksmithSettings.java @@ -3,10 +3,10 @@ package net.knarcraft.blacksmith.config.blacksmith; import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.config.SettingValueType; import net.knarcraft.blacksmith.config.Settings; -import net.knarcraft.blacksmith.config.StargateYamlConfiguration; import net.knarcraft.blacksmith.util.ConfigHelper; import net.knarcraft.blacksmith.util.InputParsingHelper; import net.knarcraft.blacksmith.util.ItemHelper; +import net.knarcraft.knarlib.config.StargateYamlConfiguration; import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; @@ -310,7 +310,7 @@ public class GlobalBlacksmithSettings implements Settings { */ private void loadSettings() { instance.reloadConfig(); - FileConfiguration configuration = instance.getConfiguration(); + FileConfiguration configuration = instance.getConfig(); for (BlacksmithSetting blacksmithSetting : BlacksmithSetting.values()) { if (!configuration.contains(blacksmithSetting.getPath())) { @@ -501,7 +501,7 @@ public class GlobalBlacksmithSettings implements Settings { * Saves all current settings to the config file */ private void save() { - FileConfiguration fileConfiguration = instance.getConfiguration(); + FileConfiguration fileConfiguration = instance.getConfig(); //Save all default settings for (BlacksmithSetting setting : BlacksmithSetting.values()) { diff --git a/src/main/java/net/knarcraft/blacksmith/config/scrapper/GlobalScrapperSettings.java b/src/main/java/net/knarcraft/blacksmith/config/scrapper/GlobalScrapperSettings.java index 27ebd9e..89917fd 100644 --- a/src/main/java/net/knarcraft/blacksmith/config/scrapper/GlobalScrapperSettings.java +++ b/src/main/java/net/knarcraft/blacksmith/config/scrapper/GlobalScrapperSettings.java @@ -145,7 +145,7 @@ public class GlobalScrapperSettings implements Settings { */ private void loadSettings() { instance.reloadConfig(); - FileConfiguration configuration = instance.getConfiguration(); + FileConfiguration configuration = instance.getConfig(); for (ScrapperSetting setting : ScrapperSetting.values()) { if (!configuration.contains(setting.getPath())) { @@ -164,7 +164,7 @@ public class GlobalScrapperSettings implements Settings { * Saves all current settings to the config file */ private void save() { - FileConfiguration fileConfiguration = instance.getConfiguration(); + FileConfiguration fileConfiguration = instance.getConfig(); //Save all default settings for (ScrapperSetting setting : ScrapperSetting.values()) { diff --git a/src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithStringFormatter.java b/src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithStringFormatter.java deleted file mode 100644 index 66d7892..0000000 --- a/src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithStringFormatter.java +++ /dev/null @@ -1,33 +0,0 @@ -package net.knarcraft.blacksmith.formatting; - -import net.citizensnpcs.api.npc.NPC; -import net.knarcraft.blacksmith.BlacksmithPlugin; -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 - */ -public final class BlacksmithStringFormatter { - - private BlacksmithStringFormatter() { - - } - - /** - * Sends a message from a blacksmith NPC to a player - * - * @param npc

The NPC sending the message

- * @param player

The player to send the message to

- * @param message

The message to send

- */ - public static void sendNPCMessage(NPC npc, Player player, String message) { - player.sendMessage(BlacksmithPlugin.getStringFormatter().replacePlaceholders( - 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/ItemType.java b/src/main/java/net/knarcraft/blacksmith/formatting/ItemType.java index f5c56b1..a41a589 100644 --- a/src/main/java/net/knarcraft/blacksmith/formatting/ItemType.java +++ b/src/main/java/net/knarcraft/blacksmith/formatting/ItemType.java @@ -1,7 +1,6 @@ package net.knarcraft.blacksmith.formatting; -import net.knarcraft.blacksmith.BlacksmithPlugin; -import net.knarcraft.knarlib.formatting.StringFormatter; +import net.knarcraft.knarlib.formatting.FormatBuilder; /** * An enum representing all item types used in messages @@ -17,11 +16,9 @@ public enum ItemType { * @return

The name of this item type

*/ public String getItemTypeName() { - StringFormatter stringFormatter = BlacksmithPlugin.getStringFormatter(); return switch (this) { - case MATERIAL -> stringFormatter.getUnFormattedMessage(BlacksmithTranslatableMessage.ITEM_TYPE_MATERIAL); - case ENCHANTMENT -> - stringFormatter.getUnFormattedMessage(BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT); + case MATERIAL -> new FormatBuilder(Translatable.ITEM_TYPE_MATERIAL).toString(); + case ENCHANTMENT -> new FormatBuilder(Translatable.ITEM_TYPE_ENCHANTMENT).toString(); }; } diff --git a/src/main/java/net/knarcraft/blacksmith/formatting/NPCFormatter.java b/src/main/java/net/knarcraft/blacksmith/formatting/NPCFormatter.java new file mode 100644 index 0000000..dafa806 --- /dev/null +++ b/src/main/java/net/knarcraft/blacksmith/formatting/NPCFormatter.java @@ -0,0 +1,40 @@ +package net.knarcraft.blacksmith.formatting; + +import net.citizensnpcs.api.npc.NPC; +import net.knarcraft.knarlib.formatting.FormatBuilder; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +/** + * A formatter for formatting displayed messages + */ +public final class NPCFormatter { + + private NPCFormatter() { + + } + + /** + * Sends a message from a blacksmith NPC to a player + * + * @param npc

The NPC sending the message

+ * @param player

The player to send the message to

+ * @param message

The message to send

+ */ + public static void sendNPCMessage(@NotNull NPC npc, @NotNull Player player, @NotNull String message) { + player.sendMessage(new FormatBuilder(Translatable.NPC_TALK_FORMAT).replace("{npc}", + npc.getName()).replace("{message}", message).color().toString()); + } + + /** + * Sends a message from a blacksmith NPC to a player + * + * @param npc

The NPC sending the message

+ * @param player

The player to send the message to

+ * @param formatBuilder

The format builder to send

+ */ + public static void sendNPCMessage(@NotNull NPC npc, @NotNull Player player, @NotNull FormatBuilder formatBuilder) { + sendNPCMessage(npc, player, formatBuilder.toString()); + } + +} diff --git a/src/main/java/net/knarcraft/blacksmith/formatting/TimeFormatter.java b/src/main/java/net/knarcraft/blacksmith/formatting/TimeFormatter.java index 93308a0..e3a3acb 100644 --- a/src/main/java/net/knarcraft/blacksmith/formatting/TimeFormatter.java +++ b/src/main/java/net/knarcraft/blacksmith/formatting/TimeFormatter.java @@ -1,6 +1,7 @@ package net.knarcraft.blacksmith.formatting; import net.knarcraft.blacksmith.BlacksmithPlugin; +import net.knarcraft.knarlib.formatting.FormatBuilder; import java.util.Arrays; import java.util.List; @@ -53,8 +54,7 @@ public final class TimeFormatter { * @return

Text describing the time interval

*/ private static String getMessageFromInterval(TimeInterval interval) { - String text = BlacksmithPlugin.getStringFormatter().getUnFormattedMessage( - BlacksmithTranslatableMessage.valueOf(interval.name())); + String text = new FormatBuilder(Translatable.valueOf(interval.name())).toString(); //Choose a random entry if a comma-separated list is provided if (text.contains(",")) { diff --git a/src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithTranslatableMessage.java b/src/main/java/net/knarcraft/blacksmith/formatting/Translatable.java similarity index 64% rename from src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithTranslatableMessage.java rename to src/main/java/net/knarcraft/blacksmith/formatting/Translatable.java index dfd2844..c3c97ba 100644 --- a/src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithTranslatableMessage.java +++ b/src/main/java/net/knarcraft/blacksmith/formatting/Translatable.java @@ -1,19 +1,16 @@ package net.knarcraft.blacksmith.formatting; -import net.knarcraft.blacksmith.BlacksmithPlugin; -import net.knarcraft.knarlib.formatting.StringReplacer; +import net.knarcraft.knarlib.formatting.FormatBuilder; import net.knarcraft.knarlib.formatting.TranslatableMessage; import org.jetbrains.annotations.NotNull; -import java.util.List; - /** * An enum containing all translatable global messages * *

This does not include NPC messages as they are configurable per-npc, and can be translated by changing the * default message values in the main config file.

*/ -public enum BlacksmithTranslatableMessage implements TranslatableMessage { +public enum Translatable implements TranslatableMessage { /** * The message displayed when a configuration value has been successfully changed @@ -149,12 +146,11 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage { * Gets the message to display when displaying the raw value of messages * * @param rawValue

The raw value to display

- * @return

The message to display

+ * @return

The format builder to display

*/ @NotNull - public static String getRawValueMessage(@NotNull String rawValue) { - return BlacksmithPlugin.getStringFormatter().replacePlaceholder(BlacksmithTranslatableMessage.RAW_VALUE, - "{rawValue}", rawValue); + public static FormatBuilder getRawValueMessage(@NotNull String rawValue) { + return new FormatBuilder(Translatable.RAW_VALUE).replace("{rawValue}", rawValue); } /** @@ -162,12 +158,12 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage { * * @param setting

The setting whose value has been changed

* @param newValue

The new value of the setting

- * @return

The string to display to a user

+ * @return

The format builder to display to a user

*/ @NotNull - public static String getValueChangedMessage(@NotNull String setting, @NotNull String newValue) { - return BlacksmithPlugin.getStringFormatter().replacePlaceholders(BlacksmithTranslatableMessage.VALUE_CHANGED, - List.of("{setting}", "{newValue}"), List.of(setting, newValue)); + public static FormatBuilder getValueChangedMessage(@NotNull String setting, @NotNull String newValue) { + return new FormatBuilder(Translatable.VALUE_CHANGED).replace("{setting}", setting). + replace("{newValue}", newValue); } /** @@ -177,18 +173,14 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage { * @param itemType

The type of item changed ("material" or "enchantment")

* @param item

The item the setting was changed for (a material or an enchantment name)

* @param newValue

The new value of the setting

- * @return

The string to display to a user

+ * @return

The format builder to display to a user

*/ @NotNull - public static String getItemValueChangedMessage(@NotNull String setting, @NotNull ItemType itemType, - @NotNull String item, @NotNull String 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(); + public static FormatBuilder getItemValueChangedMessage(@NotNull String setting, @NotNull ItemType itemType, + @NotNull String item, @NotNull String newValue) { + return new FormatBuilder(Translatable.VALUE_FOR_ITEM_CHANGED).replace("{setting}", setting). + replace("{itemType}", itemType.getItemTypeName()).replace("{item}", item). + replace("{newValue}", newValue); } /** @@ -196,13 +188,12 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage { * * @param setting

The setting whose value is shown

* @param defaultValue

The default value of the setting

- * @return

The string to display to a user

+ * @return

The format builder to display to a user

*/ @NotNull - public static String getDefaultValueMessage(@NotNull String setting, @NotNull String defaultValue) { - return BlacksmithPlugin.getStringFormatter().replacePlaceholders(BlacksmithTranslatableMessage.DEFAULT_VALUE, - List.of("{setting}", "{defaultValue}"), - List.of(setting, defaultValue)); + public static FormatBuilder getDefaultValueMessage(@NotNull String setting, @NotNull String defaultValue) { + return new FormatBuilder(Translatable.DEFAULT_VALUE).replace("{setting}", setting). + replace("{defaultValue}", defaultValue); } /** @@ -210,13 +201,12 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage { * * @param setting

The setting whose value is shown

* @param currentValue

The current value of the setting

- * @return

The string to display to a user

+ * @return

The format builder to display to a user

*/ @NotNull - public static String getCurrentValueMessage(@NotNull String setting, @NotNull String currentValue) { - return BlacksmithPlugin.getStringFormatter().replacePlaceholders(BlacksmithTranslatableMessage.CURRENT_VALUE, - List.of("{setting}", "{currentValue}"), - List.of(setting, currentValue)); + public static FormatBuilder getCurrentValueMessage(@NotNull String setting, @NotNull String currentValue) { + return new FormatBuilder(Translatable.CURRENT_VALUE).replace("{setting}", setting). + replace("{currentValue}", currentValue); } /** @@ -226,24 +216,20 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage { * @param itemType

The type of item shown ("material" or "enchantment")

* @param item

The item the setting is shown for (a material or an enchantment name)

* @param currentValue

The current value of the setting

- * @return

The string to display to a user

+ * @return

The format builder to display to a user

*/ @NotNull - public static String getItemCurrentValueMessage(@NotNull String setting, @NotNull ItemType itemType, - @NotNull String item, @NotNull String 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(); + public static FormatBuilder getItemCurrentValueMessage(@NotNull String setting, @NotNull ItemType itemType, + @NotNull String item, @NotNull String currentValue) { + return new FormatBuilder(Translatable.CURRENT_VALUE_FOR_ITEM).replace("{setting}", setting). + replace("{itemType}", itemType.getItemTypeName()).replace("{item}", item). + replace("{currentValue}", currentValue); } @Override @NotNull public TranslatableMessage[] getAllMessages() { - return BlacksmithTranslatableMessage.values(); + return Translatable.values(); } } diff --git a/src/main/java/net/knarcraft/blacksmith/listener/NPCClickListener.java b/src/main/java/net/knarcraft/blacksmith/listener/NPCClickListener.java index f51bfbd..ceb7cc6 100644 --- a/src/main/java/net/knarcraft/blacksmith/listener/NPCClickListener.java +++ b/src/main/java/net/knarcraft/blacksmith/listener/NPCClickListener.java @@ -2,10 +2,11 @@ package net.knarcraft.blacksmith.listener; import net.citizensnpcs.api.event.NPCRightClickEvent; import net.knarcraft.blacksmith.BlacksmithPlugin; -import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage; +import net.knarcraft.blacksmith.formatting.Translatable; import net.knarcraft.blacksmith.trait.BlacksmithTrait; import net.knarcraft.blacksmith.trait.CustomTrait; import net.knarcraft.blacksmith.trait.ScrapperTrait; +import net.knarcraft.knarlib.formatting.FormatBuilder; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -44,8 +45,7 @@ public class NPCClickListener implements Listener { //Permission check if (!player.hasPermission("blacksmith.use")) { - BlacksmithPlugin.getStringFormatter().displayErrorMessage(player, - BlacksmithTranslatableMessage.PERMISSION_DENIED); + new FormatBuilder(Translatable.PERMISSION_DENIED).error(player); return; } diff --git a/src/main/java/net/knarcraft/blacksmith/trait/BlacksmithTrait.java b/src/main/java/net/knarcraft/blacksmith/trait/BlacksmithTrait.java index f3980e8..c47ce7b 100644 --- a/src/main/java/net/knarcraft/blacksmith/trait/BlacksmithTrait.java +++ b/src/main/java/net/knarcraft/blacksmith/trait/BlacksmithTrait.java @@ -4,9 +4,10 @@ import net.citizensnpcs.api.util.DataKey; import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.config.blacksmith.BlacksmithNPCSettings; import net.knarcraft.blacksmith.config.blacksmith.BlacksmithSetting; +import net.knarcraft.blacksmith.formatting.NPCFormatter; import net.knarcraft.blacksmith.manager.EconomyManager; import net.knarcraft.blacksmith.util.ItemHelper; -import net.knarcraft.knarlib.formatting.StringFormatter; +import net.knarcraft.knarlib.formatting.FormatBuilder; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Player; @@ -15,8 +16,6 @@ import org.jetbrains.annotations.NotNull; import java.util.List; -import static net.knarcraft.blacksmith.formatting.BlacksmithStringFormatter.sendNPCMessage; - /** * The class representing the Blacksmith NPC trait */ @@ -69,7 +68,7 @@ public class BlacksmithTrait extends CustomTrait { public void startSession(@NotNull Player player) { ItemStack hand = player.getInventory().getItemInMainHand(); if (hand.getType().isAir()) { - sendNPCMessage(this.npc, player, config.getNoItemMessage()); + NPCFormatter.sendNPCMessage(this.npc, player, config.getNoItemMessage()); return; } @@ -81,14 +80,14 @@ public class BlacksmithTrait extends CustomTrait { (!reforgeAbleItems.isEmpty() && !reforgeAbleItems.contains(hand.getType())); if (notHoldingAnvil && notHoldingRepairable) { - String invalidMessage = StringFormatter.replacePlaceholder(config.getInvalidItemMessage(), - "{title}", config.getBlacksmithTitle()); - sendNPCMessage(this.npc, player, invalidMessage); + String invalidMessage = new FormatBuilder(config.getInvalidItemMessage()).replace("{title}", + config.getBlacksmithTitle()).toString(); + NPCFormatter.sendNPCMessage(this.npc, player, invalidMessage); return; } if (ItemHelper.getDamage(hand) == 0 && !ItemHelper.isAnvil(hand.getType(), true)) { - sendNPCMessage(this.npc, player, config.getNotDamagedMessage()); + NPCFormatter.sendNPCMessage(this.npc, player, config.getNotDamagedMessage()); return; } @@ -104,8 +103,8 @@ public class BlacksmithTrait extends CustomTrait { //Tell the player the cost of repairing the item String cost = EconomyManager.formatBlacksmithCost(player); String itemName = hand.getType().name().toLowerCase().replace('_', ' '); - sendNPCMessage(this.npc, player, config.getCostMessage().replace("{cost}", cost).replace("{item}", - itemName)); + NPCFormatter.sendNPCMessage(this.npc, player, config.getCostMessage().replace("{cost}", cost). + replace("{item}", itemName)); } @Override diff --git a/src/main/java/net/knarcraft/blacksmith/trait/CustomTrait.java b/src/main/java/net/knarcraft/blacksmith/trait/CustomTrait.java index 1312ecd..d70fb80 100644 --- a/src/main/java/net/knarcraft/blacksmith/trait/CustomTrait.java +++ b/src/main/java/net/knarcraft/blacksmith/trait/CustomTrait.java @@ -6,9 +6,10 @@ import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.config.Setting; import net.knarcraft.blacksmith.config.Settings; import net.knarcraft.blacksmith.config.TraitSettings; +import net.knarcraft.blacksmith.formatting.NPCFormatter; import net.knarcraft.blacksmith.formatting.TimeFormatter; import net.knarcraft.blacksmith.manager.EconomyManager; -import net.knarcraft.knarlib.formatting.StringFormatter; +import net.knarcraft.knarlib.formatting.FormatBuilder; import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; @@ -23,8 +24,6 @@ import java.util.HashMap; import java.util.Map; import java.util.UUID; -import static net.knarcraft.blacksmith.formatting.BlacksmithStringFormatter.sendNPCMessage; - /** * A custom trait that utilizes sessions */ @@ -77,7 +76,7 @@ public abstract class CustomTrait extends Trait { public void continueSession(@NotNull Player player) { //Another player is using the blacksmith if (session.isNotInSession(player)) { - sendNPCMessage(this.npc, player, config.getBusyWithPlayerMessage()); + NPCFormatter.sendNPCMessage(this.npc, player, config.getBusyWithPlayerMessage()); return; } @@ -85,8 +84,8 @@ public abstract class CustomTrait extends Trait { if (session.isRunning()) { int timeRemaining = (int) ((session.getFinishTime() - System.currentTimeMillis()) / 1000); boolean showExactTime = showExactTime(); - sendNPCMessage(this.npc, player, StringFormatter.replacePlaceholder(config.getBusyWorkingMessage(), - "{time}", TimeFormatter.formatTime(showExactTime, timeRemaining))); + NPCFormatter.sendNPCMessage(this.npc, player, new FormatBuilder(config.getBusyWorkingMessage()). + replace("{time}", TimeFormatter.formatTime(showExactTime, timeRemaining))); return; } if (session.isSessionInvalid()) { @@ -147,8 +146,8 @@ public abstract class CustomTrait extends Trait { if (!calendar.after(coolDowns.get(playerId))) { int secondDifference = (int) (coolDowns.get(playerId).getTimeInMillis() - calendar.getTimeInMillis()) / 1000; boolean exactTime = showExactTime(); - sendNPCMessage(this.npc, player, StringFormatter.replacePlaceholder(config.getCoolDownUnexpiredMessage(), - "{time}", TimeFormatter.formatTime(exactTime, secondDifference))); + NPCFormatter.sendNPCMessage(this.npc, player, new FormatBuilder(config.getCoolDownUnexpiredMessage()). + replace("{time}", TimeFormatter.formatTime(exactTime, secondDifference))); return false; } coolDowns.remove(playerId); @@ -181,7 +180,7 @@ public abstract class CustomTrait extends Trait { * @param player

The player that initiated the reforge

*/ private void startMainAction(@NotNull NPC npc, @NotNull Player player) { - sendNPCMessage(this.npc, player, config.getStartWorkingMessage()); + NPCFormatter.sendNPCMessage(this.npc, player, config.getStartWorkingMessage()); boolean isBlacksmith = this instanceof BlacksmithTrait; diff --git a/src/main/java/net/knarcraft/blacksmith/trait/ReforgeSession.java b/src/main/java/net/knarcraft/blacksmith/trait/ReforgeSession.java index d9b2a5b..d61f569 100644 --- a/src/main/java/net/knarcraft/blacksmith/trait/ReforgeSession.java +++ b/src/main/java/net/knarcraft/blacksmith/trait/ReforgeSession.java @@ -5,6 +5,7 @@ import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.config.blacksmith.BlacksmithNPCSettings; import net.knarcraft.blacksmith.event.BlacksmithReforgeFailEvent; import net.knarcraft.blacksmith.event.BlacksmithReforgeSucceedEvent; +import net.knarcraft.blacksmith.formatting.NPCFormatter; import net.knarcraft.blacksmith.manager.EconomyManager; import net.knarcraft.blacksmith.util.InputParsingHelper; import net.knarcraft.blacksmith.util.ItemHelper; @@ -24,8 +25,6 @@ import java.util.Calendar; import java.util.List; import java.util.Random; -import static net.knarcraft.blacksmith.formatting.BlacksmithStringFormatter.sendNPCMessage; - /** * A representation of the session between a player and a blacksmith */ @@ -70,12 +69,12 @@ public class ReforgeSession extends Session implements Runnable { // Prevent player from switching items during session ItemStack itemInHand = this.player.getInventory().getItemInMainHand(); if (!itemToReforge.equals(itemInHand)) { - sendNPCMessage(this.npc, this.player, this.config.getItemChangedMessage()); + NPCFormatter.sendNPCMessage(this.npc, this.player, this.config.getItemChangedMessage()); return true; } // The player is unable to pay if (EconomyManager.cannotPayForHeldItemReforge(this.player)) { - sendNPCMessage(this.npc, this.player, this.config.getInsufficientFundsMessage()); + NPCFormatter.sendNPCMessage(this.npc, this.player, this.config.getInsufficientFundsMessage()); return true; } return false; @@ -103,7 +102,7 @@ public class ReforgeSession extends Session implements Runnable { @Override public void run() { boolean success = reforgeItem(); - sendNPCMessage(this.npc, this.player, success ? this.config.getSuccessMessage() : this.config.getFailMessage()); + NPCFormatter.sendNPCMessage(this.npc, this.player, success ? this.config.getSuccessMessage() : this.config.getFailMessage()); playSound(success ? Sound.BLOCK_ANVIL_USE : Sound.ENTITY_VILLAGER_NO); //Stop the reforged item from displaying in the blacksmith's hand diff --git a/src/main/java/net/knarcraft/blacksmith/trait/SalvageSession.java b/src/main/java/net/knarcraft/blacksmith/trait/SalvageSession.java index b262aea..49a8c49 100644 --- a/src/main/java/net/knarcraft/blacksmith/trait/SalvageSession.java +++ b/src/main/java/net/knarcraft/blacksmith/trait/SalvageSession.java @@ -5,6 +5,7 @@ import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.config.scrapper.ScrapperNPCSettings; import net.knarcraft.blacksmith.event.ScrapperSalvageFailEvent; import net.knarcraft.blacksmith.event.ScrapperSalvageSucceedEvent; +import net.knarcraft.blacksmith.formatting.NPCFormatter; import net.knarcraft.blacksmith.manager.EconomyManager; import net.knarcraft.blacksmith.property.SalvageMethod; import net.knarcraft.blacksmith.util.ItemHelper; @@ -22,8 +23,6 @@ import java.util.Calendar; import java.util.List; import java.util.Random; -import static net.knarcraft.blacksmith.formatting.BlacksmithStringFormatter.sendNPCMessage; - /** * A representation of the session between a player and a scrapper */ @@ -66,12 +65,12 @@ public class SalvageSession extends Session implements Runnable { // Prevent player from switching items during session ItemStack itemInHand = this.player.getInventory().getItemInMainHand(); if (!itemToSalvage.equals(itemInHand)) { - sendNPCMessage(this.npc, this.player, this.config.getItemChangedMessage()); + NPCFormatter.sendNPCMessage(this.npc, this.player, this.config.getItemChangedMessage()); return true; } if (EconomyManager.cannotPayForSalvage(this.player, this.salvageMethod)) { - sendNPCMessage(this.npc, this.player, this.config.getInsufficientFundsMessage()); + NPCFormatter.sendNPCMessage(this.npc, this.player, this.config.getInsufficientFundsMessage()); return true; } return false; @@ -146,7 +145,7 @@ public class SalvageSession extends Session implements Runnable { playSound(Sound.BLOCK_ANVIL_USE); giveSalvage(); BlacksmithPlugin.getInstance().callEvent(new ScrapperSalvageSucceedEvent(this.npc, this.entity, this.player)); - sendNPCMessage(this.npc, this.player, this.config.getSuccessMessage()); + NPCFormatter.sendNPCMessage(this.npc, this.player, this.config.getSuccessMessage()); } } @@ -161,14 +160,14 @@ public class SalvageSession extends Session implements Runnable { //Damage the item if possible damageItemRandomly(this.itemToSalvage); giveResultingItem(this.config.getMaxSalvageDelay() > 0, this.config.getDropItem(), this.itemToSalvage); - sendNPCMessage(this.npc, this.player, this.config.getFailSalvageMessage()); + NPCFormatter.sendNPCMessage(this.npc, this.player, this.config.getFailSalvageMessage()); } else { // Give half the salvage List halfSalvage = SalvageHelper.pickRandomSalvage(this.salvage, new ArrayList<>(), 0.5); for (ItemStack item : halfSalvage) { giveResultingItem(this.config.getMaxSalvageDelay() > 0, this.config.getDropItem(), item); } - sendNPCMessage(this.npc, this.player, this.config.getFailExtendedSalvageMessage()); + NPCFormatter.sendNPCMessage(this.npc, this.player, this.config.getFailExtendedSalvageMessage()); } } diff --git a/src/main/java/net/knarcraft/blacksmith/trait/ScrapperTrait.java b/src/main/java/net/knarcraft/blacksmith/trait/ScrapperTrait.java index e8138d9..dedc499 100644 --- a/src/main/java/net/knarcraft/blacksmith/trait/ScrapperTrait.java +++ b/src/main/java/net/knarcraft/blacksmith/trait/ScrapperTrait.java @@ -8,12 +8,13 @@ import net.knarcraft.blacksmith.config.scrapper.ScrapperNPCSettings; import net.knarcraft.blacksmith.config.scrapper.ScrapperSetting; import net.knarcraft.blacksmith.container.RecipeResult; import net.knarcraft.blacksmith.container.SalvageResult; +import net.knarcraft.blacksmith.formatting.NPCFormatter; import net.knarcraft.blacksmith.manager.EconomyManager; import net.knarcraft.blacksmith.property.SalvageMethod; import net.knarcraft.blacksmith.property.SalvageState; import net.knarcraft.blacksmith.util.ItemHelper; import net.knarcraft.blacksmith.util.SalvageHelper; -import net.knarcraft.knarlib.formatting.StringFormatter; +import net.knarcraft.knarlib.formatting.FormatBuilder; import net.knarcraft.knarlib.formatting.StringReplacer; import org.bukkit.Bukkit; import org.bukkit.Material; @@ -28,8 +29,6 @@ import java.util.HashSet; import java.util.List; import java.util.Set; -import static net.knarcraft.blacksmith.formatting.BlacksmithStringFormatter.sendNPCMessage; - /** * The class representing a scrapper NPC trait */ @@ -86,7 +85,7 @@ public class ScrapperTrait extends CustomTrait { public void startSession(@NotNull Player player) { ItemStack itemInHand = player.getInventory().getItemInMainHand().clone(); if (itemInHand.getType().isAir()) { - sendNPCMessage(this.npc, player, config.getNoItemMessage()); + NPCFormatter.sendNPCMessage(this.npc, player, config.getNoItemMessage()); return; } @@ -95,8 +94,8 @@ public class ScrapperTrait extends CustomTrait { // Check if the item can be salvaged if (!canBeSalvaged(itemInHand, salvageAbleItems, extended)) { - sendNPCMessage(this.npc, player, StringFormatter.replacePlaceholder(getSettings().getInvalidItemMessage(), - "{title}", getSettings().getScrapperTitle())); + NPCFormatter.sendNPCMessage(this.npc, player, new FormatBuilder(getSettings().getInvalidItemMessage()). + replace("{title}", getSettings().getScrapperTitle())); BlacksmithPlugin.debug("Cannot salvage provided item: " + itemInHand); return; } @@ -168,15 +167,15 @@ public class ScrapperTrait extends CustomTrait { // As there is no recipe for netherite items, the check needs to be after the netherite salvage check if (!SalvageHelper.isSalvageable(player.getServer(), itemInHand)) { - sendNPCMessage(this.npc, player, StringFormatter.replacePlaceholder(getSettings().getInvalidItemMessage(), - "{title}", getSettings().getScrapperTitle())); + NPCFormatter.sendNPCMessage(this.npc, player, new FormatBuilder(getSettings().getInvalidItemMessage()). + replace("{title}", getSettings().getScrapperTitle())); BlacksmithPlugin.debug("Provided with non-salvage-able item " + itemInHand); return new SalvageResult(SalvageMethod.SALVAGE, new ArrayList<>(), SalvageState.NO_SALVAGE, 0); } // Check if the item is enchanted, and whether this blacksmith can salvage it if (!itemInHand.getEnchantments().isEmpty() && !getSettings().salvageEnchanted()) { - sendNPCMessage(this.npc, player, getSettings().getCannotSalvageEnchantedMessage()); + NPCFormatter.sendNPCMessage(this.npc, player, getSettings().getCannotSalvageEnchantedMessage()); return new SalvageResult(SalvageMethod.SALVAGE, new ArrayList<>(), SalvageState.NO_SALVAGE, 0); } @@ -185,7 +184,7 @@ public class ScrapperTrait extends CustomTrait { SalvageMethod method = ItemHelper.isRepairable(itemInHand) ? SalvageMethod.SALVAGE : SalvageMethod.EXTENDED_SALVAGE; boolean noUsefulSalvage = recipeResult == null || recipeResult.salvage().isEmpty(); if (noUsefulSalvage) { - sendNPCMessage(this.npc, player, getSettings().getTooDamagedMessage()); + NPCFormatter.sendNPCMessage(this.npc, player, getSettings().getTooDamagedMessage()); return new SalvageResult(method, new ArrayList<>(), SalvageState.NO_SALVAGE, 0); } else { return new SalvageResult(method, recipeResult.salvage(), SalvageState.FOUND_SALVAGE, @@ -207,7 +206,7 @@ public class ScrapperTrait extends CustomTrait { } if (!getSettings().salvageNetherite()) { - sendNPCMessage(this.npc, player, getSettings().getCannotSalvageNetheriteMessage()); + NPCFormatter.sendNPCMessage(this.npc, player, getSettings().getCannotSalvageNetheriteMessage()); return new SalvageResult(SalvageMethod.NETHERITE, new ArrayList<>(), SalvageState.NO_SALVAGE, 0); } @@ -233,13 +232,13 @@ public class ScrapperTrait extends CustomTrait { } if (!getSettings().salvageArmorTrims()) { - sendNPCMessage(this.npc, player, getSettings().getCannotSalvageArmorTrimMessage()); + NPCFormatter.sendNPCMessage(this.npc, player, getSettings().getCannotSalvageArmorTrimMessage()); return new SalvageResult(SalvageMethod.ARMOR_TRIM, new ArrayList<>(), SalvageState.NO_SALVAGE, 0); } List salvage = SalvageHelper.getArmorTrimSalvage(itemInHand, armorMeta); if (salvage == null || salvage.isEmpty()) { - sendNPCMessage(this.npc, player, getSettings().getArmorTrimSalvageNotFoundMessage()); + NPCFormatter.sendNPCMessage(this.npc, player, getSettings().getArmorTrimSalvageNotFoundMessage()); return new SalvageResult(SalvageMethod.ARMOR_TRIM, new ArrayList<>(), SalvageState.NO_SALVAGE, 0); } @@ -260,7 +259,7 @@ public class ScrapperTrait extends CustomTrait { replacer.add("{cost}", cost); replacer.add("{item}", itemInHand.getType().name().toLowerCase().replace('_', ' ')); if (salvageMethod == SalvageMethod.ARMOR_TRIM) { - sendNPCMessage(this.npc, player, replacer.replace(getSettings().getArmorTrimCostMessage())); + NPCFormatter.sendNPCMessage(this.npc, player, replacer.replace(getSettings().getArmorTrimCostMessage())); } else if (salvageMethod == SalvageMethod.SALVAGE || salvageMethod == SalvageMethod.EXTENDED_SALVAGE) { String expectedYield; if (ItemHelper.getDamage(itemInHand) <= 0) { @@ -269,9 +268,9 @@ public class ScrapperTrait extends CustomTrait { expectedYield = getSettings().getPartialSalvageMessage(); } replacer.add("{yield}", expectedYield); - sendNPCMessage(this.npc, player, replacer.replace(getSettings().getCostMessage())); + NPCFormatter.sendNPCMessage(this.npc, player, replacer.replace(getSettings().getCostMessage())); } else if (salvageMethod == SalvageMethod.NETHERITE) { - sendNPCMessage(this.npc, player, replacer.replace(getSettings().getNetheriteCostMessage())); + NPCFormatter.sendNPCMessage(this.npc, player, replacer.replace(getSettings().getNetheriteCostMessage())); } else { BlacksmithPlugin.error("Unrecognized salvage method " + salvageMethod); } diff --git a/src/main/java/net/knarcraft/blacksmith/util/ConfigCommandHelper.java b/src/main/java/net/knarcraft/blacksmith/util/ConfigCommandHelper.java index 9814270..1142927 100644 --- a/src/main/java/net/knarcraft/blacksmith/util/ConfigCommandHelper.java +++ b/src/main/java/net/knarcraft/blacksmith/util/ConfigCommandHelper.java @@ -1,20 +1,19 @@ package net.knarcraft.blacksmith.util; -import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.config.Setting; import net.knarcraft.blacksmith.config.SettingValueType; import net.knarcraft.blacksmith.config.Settings; -import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage; -import net.knarcraft.knarlib.formatting.StringFormatter; +import net.knarcraft.blacksmith.formatting.Translatable; +import net.knarcraft.knarlib.formatting.FormatBuilder; import net.md_5.bungee.api.ChatColor; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; import java.util.Arrays; -import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getCurrentValueMessage; -import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getDefaultValueMessage; -import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getValueChangedMessage; +import static net.knarcraft.blacksmith.formatting.Translatable.getCurrentValueMessage; +import static net.knarcraft.blacksmith.formatting.Translatable.getDefaultValueMessage; +import static net.knarcraft.blacksmith.formatting.Translatable.getValueChangedMessage; /** * A helper class for the configuration command @@ -43,8 +42,7 @@ public final class ConfigCommandHelper { newValue = String.join(" ", Arrays.asList(args).subList(1, args.length)); } settings.changeValue(detectedSetting, newValue); - BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, - getValueChangedMessage(detectedSetting.getCommandName(), newValue)); + getValueChangedMessage(detectedSetting.getCommandName(), newValue).success(sender); } /** @@ -57,7 +55,6 @@ public final class ConfigCommandHelper { public static void displayCurrentValue(@NotNull K setting, @NotNull Settings settings, @NotNull CommandSender sender) { - StringFormatter formatter = BlacksmithPlugin.getStringFormatter(); boolean printRawValue = false; //Find the value of the specified setting @@ -70,16 +67,16 @@ public final class ConfigCommandHelper { } // Display the description of the setting - formatter.displaySuccessMessage(sender, setting.getDescription()); + new FormatBuilder(setting.getDescription()).success(sender); // Display the setting's default value - formatter.displayNeutralMessage(sender, getDefaultValueMessage(setting.getCommandName(), defaultValue)); + getDefaultValueMessage(setting.getCommandName(), defaultValue).neutral(sender); if (printRawValue) { displayRaw(sender, defaultValue); } // Display the current value of the setting - formatter.displayNeutralMessage(sender, getCurrentValueMessage(setting.getCommandName(), currentValue)); + getCurrentValueMessage(setting.getCommandName(), currentValue).neutral(sender); if (printRawValue) { displayRaw(sender, currentValue); } @@ -92,8 +89,7 @@ public final class ConfigCommandHelper { * @param value

The value to display raw

*/ public static void displayRaw(@NotNull CommandSender sender, @NotNull String value) { - sender.sendMessage(BlacksmithTranslatableMessage.getRawValueMessage( - value.replace(ChatColor.COLOR_CHAR, '&'))); + sender.sendMessage(Translatable.getRawValueMessage(value.replace(ChatColor.COLOR_CHAR, '&')).toString()); } } diff --git a/src/main/java/net/knarcraft/blacksmith/util/ConfigHelper.java b/src/main/java/net/knarcraft/blacksmith/util/ConfigHelper.java index 28bdf47..8ae38e7 100644 --- a/src/main/java/net/knarcraft/blacksmith/util/ConfigHelper.java +++ b/src/main/java/net/knarcraft/blacksmith/util/ConfigHelper.java @@ -1,22 +1,10 @@ package net.knarcraft.blacksmith.util; -import net.knarcraft.blacksmith.BlacksmithPlugin; -import net.knarcraft.blacksmith.config.StargateYamlConfiguration; -import net.knarcraft.knarlib.property.ColorConversion; -import net.knarcraft.knarlib.util.FileHelper; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.configuration.MemorySection; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.configuration.file.YamlConfiguration; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; import java.util.ArrayList; import java.util.List; -import java.util.Map; /** * A helper class for getting an object value as the correct type @@ -106,103 +94,4 @@ public final class ConfigHelper { } } - /** - * Changes all configuration values from the old name to the new name - * - * @param dataFolderPath

The path to this plugin's data

- * @param currentConfiguration

The current config to back up

- */ - public static void migrateConfig(@NotNull String dataFolderPath, @NotNull FileConfiguration currentConfiguration) { - BlacksmithPlugin instance = BlacksmithPlugin.getInstance(); - - //Save the old config just in case something goes wrong - try { - currentConfiguration.save(new File(dataFolderPath, "config.yml.old")); - } catch (IOException exception) { - BlacksmithPlugin.warn("Unable to save old backup and do migration"); - return; - } - - //Load old and new configuration - instance.reloadConfig(); - FileConfiguration oldConfiguration = instance.getConfig(); - InputStream configStream = FileHelper.getInputStreamForInternalFile("/config.yml"); - if (configStream == null) { - BlacksmithPlugin.error("Could not migrate the configuration, as the internal configuration could not be read!"); - return; - } - YamlConfiguration newConfiguration = StargateYamlConfiguration.loadConfiguration( - FileHelper.getBufferedReaderFromInputStream(configStream)); - - //Read all available config migrations - Map migrationFields; - try { - InputStream migrationStream = FileHelper.getInputStreamForInternalFile("/config-migrations.txt"); - if (migrationStream == null) { - BlacksmithPlugin.error("Could not migrate the configuration, as the internal migration paths could not be read!"); - return; - } - migrationFields = FileHelper.readKeyValuePairs(FileHelper.getBufferedReaderFromInputStream(migrationStream), - "=", ColorConversion.NORMAL); - } catch (IOException exception) { - BlacksmithPlugin.warn("Unable to load config migration file"); - return; - } - - //Replace old config names with the new ones - for (String key : migrationFields.keySet()) { - if (oldConfiguration.contains(key)) { - migrateProperty(migrationFields, key, oldConfiguration); - } - } - - // Copy all keys to the new config - for (String key : StargateYamlConfiguration.getKeysWithoutComments(oldConfiguration, true)) { - if (oldConfiguration.get(key) instanceof MemorySection) { - continue; - } - newConfiguration.set(key, oldConfiguration.get(key)); - } - - try { - newConfiguration.save(new File(dataFolderPath, "config.yml")); - } catch (IOException exception) { - BlacksmithPlugin.warn("Unable to save migrated config"); - } - - instance.reloadConfig(); - } - - /** - * Migrates one configuration property - * - * @param migrationFields

The configuration fields to be migrated

- * @param key

The key/path of the property to migrate

- * @param oldConfiguration

The original pre-migration configuration

- */ - private static void migrateProperty(@NotNull Map migrationFields, @NotNull String key, - @NotNull FileConfiguration oldConfiguration) { - String newPath = migrationFields.get(key); - Object oldValue = oldConfiguration.get(key); - if (!newPath.trim().isEmpty()) { - if (oldConfiguration.isConfigurationSection(key)) { - // Copy each value of a configuration section - ConfigurationSection sourceSection = oldConfiguration.getConfigurationSection(key); - ConfigurationSection destinationSection = oldConfiguration.createSection(newPath); - if (sourceSection == null) { - return; - } - for (String path : StargateYamlConfiguration.getKeysWithoutComments(sourceSection, true)) { - destinationSection.set(path, sourceSection.get(path)); - } - } else { - // Copy the value to the new path - oldConfiguration.set(newPath, oldValue); - } - } - - // Remove the old path's value - oldConfiguration.set(key, null); - } - } diff --git a/src/main/java/net/knarcraft/blacksmith/util/TypeValidationHelper.java b/src/main/java/net/knarcraft/blacksmith/util/TypeValidationHelper.java index 144c44d..d905e47 100644 --- a/src/main/java/net/knarcraft/blacksmith/util/TypeValidationHelper.java +++ b/src/main/java/net/knarcraft/blacksmith/util/TypeValidationHelper.java @@ -1,8 +1,8 @@ package net.knarcraft.blacksmith.util; -import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.config.SettingValueType; -import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage; +import net.knarcraft.blacksmith.formatting.Translatable; +import net.knarcraft.knarlib.formatting.FormatBuilder; import org.bukkit.Material; import org.bukkit.command.CommandSender; import org.bukkit.enchantments.Enchantment; @@ -59,8 +59,7 @@ public final class TypeValidationHelper { boolean isMaterial = value instanceof Material || (value instanceof String string && InputParsingHelper.matchMaterial(string) != null); if (!isMaterial && sender != null) { - BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender, - BlacksmithTranslatableMessage.ITEM_TYPE_MATERIAL); + new FormatBuilder(Translatable.ITEM_TYPE_MATERIAL).error(sender); } return isMaterial; } @@ -104,8 +103,7 @@ public final class TypeValidationHelper { boolean isEnchantment = value instanceof Enchantment || (value instanceof String string && InputParsingHelper.matchEnchantment(string) != null); if (!isEnchantment && sender != null) { - BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender, - BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT); + new FormatBuilder(Translatable.ITEM_TYPE_ENCHANTMENT).error(sender); } return isEnchantment; } @@ -120,8 +118,7 @@ public final class TypeValidationHelper { private static boolean isStringList(@Nullable Object value, @Nullable CommandSender sender) { boolean isStringList = value instanceof String[] || value instanceof List || value instanceof String; if (!isStringList && sender != null) { - BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender, - BlacksmithTranslatableMessage.INPUT_STRING_LIST_REQUIRED); + new FormatBuilder(Translatable.INPUT_STRING_LIST_REQUIRED).error(sender); } return isStringList; } @@ -137,8 +134,7 @@ public final class TypeValidationHelper { boolean isPercentage = value != null && isPositiveInteger(value, null) && ConfigHelper.asInt(value) >= 0 && ConfigHelper.asInt(value) <= 100; if (!isPercentage && sender != null) { - BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender, - BlacksmithTranslatableMessage.INPUT_PERCENTAGE_REQUIRED); + new FormatBuilder(Translatable.INPUT_PERCENTAGE_REQUIRED).error(sender); } return isPercentage; } @@ -153,8 +149,7 @@ public final class TypeValidationHelper { private static boolean isNonEmptyString(@Nullable Object value, @Nullable CommandSender sender) { boolean isString = value instanceof String string && !string.isBlank(); if (!isString && sender != null) { - BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender, - BlacksmithTranslatableMessage.INPUT_STRING_REQUIRED); + new FormatBuilder(Translatable.INPUT_STRING_REQUIRED).error(sender); } return isString; } @@ -171,8 +166,7 @@ public final class TypeValidationHelper { return value != null && ConfigHelper.asDouble(value) >= 0.0; } catch (NumberFormatException | NullPointerException exception) { if (sender != null) { - BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender, - BlacksmithTranslatableMessage.INPUT_POSITIVE_DOUBLE_REQUIRED); + new FormatBuilder(Translatable.INPUT_POSITIVE_DOUBLE_REQUIRED).error(sender); } return false; } @@ -190,8 +184,7 @@ public final class TypeValidationHelper { return value != null && ConfigHelper.asInt(value) >= 0; } catch (NumberFormatException | NullPointerException exception) { if (sender != null) { - BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender, - BlacksmithTranslatableMessage.INPUT_POSITIVE_INTEGER_REQUIRED); + new FormatBuilder(Translatable.INPUT_POSITIVE_INTEGER_REQUIRED).error(sender); } return false; }