The raw configuration
- */ - @NotNull - public FileConfiguration getConfiguration() { - return this.configuration; - } - - /** - * Gets the string formatter to use for formatting - * - * @returnThe 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 commandNameThe name of the command
- * @param executorThe executor to bind to the command
- * @param tabCompleterThe 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 EditCommandThis 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 configurationSectionThe configuration section to get keys for
- * @param deepWhether to get keys for child elements as well
- * @returnThe configuration section's keys, with comment entries removed
- */ - public static SetA 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(); - ListThe string builder used for building YAML
- * @param currentCommentThe comment to add as a YAML string
- * @param lineThe current line
- * @param previousIndentationThe indentation of the current block comment
- * @param commentIdThe id of the comment
- */ - private void addYamlString(@NotNull StringBuilder yamlBuilder, @NotNull ListThe list to add to
- * @param commentThe comment to add
- */ - private void addComment(@NotNull ListThe string builder to add the generated YAML to
- * @param commentLinesThe lines of the comment to convert into YAML
- * @param commentIdThe unique id of the comment
- * @param indentationThe indentation to add to every line
- */ - private void generateCommentYAML(@NotNull StringBuilder yamlBuilder, @NotNull ListThe 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 yamlStringA string using the YAML format
- * @returnThe 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 builderThe string builder to write to
- * @param linesThe lines to read from
- * @param startIndexThe index to start reading from
- * @param commentIndentationThe indentation of the read comment
- * @returnThe 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 indentationSpacesThe number spaces to use for indentation
- * @returnA 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 lineThe line to get indentation of
- * @returnThe 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 SettingsThe NPC sending the message
- * @param playerThe player to send the message to
- * @param messageThe 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 { * @returnThe 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 npcThe NPC sending the message
+ * @param playerThe player to send the message to
+ * @param messageThe 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 npcThe NPC sending the message
+ * @param playerThe player to send the message to
+ * @param formatBuilderThe 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 { * @returnText 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 rawValueThe raw value to display
- * @returnThe message to display
+ * @returnThe 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 settingThe setting whose value has been changed
* @param newValueThe new value of the setting
- * @returnThe string to display to a user
+ * @returnThe 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 itemTypeThe type of item changed ("material" or "enchantment")
* @param itemThe item the setting was changed for (a material or an enchantment name)
* @param newValueThe new value of the setting
- * @returnThe string to display to a user
+ * @returnThe 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 settingThe setting whose value is shown
* @param defaultValueThe default value of the setting
- * @returnThe string to display to a user
+ * @returnThe 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 settingThe setting whose value is shown
* @param currentValueThe current value of the setting
- * @returnThe string to display to a user
+ * @returnThe 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 itemTypeThe type of item shown ("material" or "enchantment")
* @param itemThe item the setting is shown for (a material or an enchantment name)
* @param currentValueThe current value of the setting
- * @returnThe string to display to a user
+ * @returnThe 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 CustomTraitThe 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 ListThe 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 dataFolderPathThe path to this plugin's data
- * @param currentConfigurationThe 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 - MapThe configuration fields to be migrated
- * @param keyThe key/path of the property to migrate
- * @param oldConfigurationThe original pre-migration configuration
- */ - private static void migrateProperty(@NotNull Map