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 4724b12..cbf4db1 100644 --- a/src/main/java/net/knarcraft/blacksmith/command/blacksmith/BlackSmithConfigCommand.java +++ b/src/main/java/net/knarcraft/blacksmith/command/blacksmith/BlackSmithConfigCommand.java @@ -3,15 +3,14 @@ package net.knarcraft.blacksmith.command.blacksmith; import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.command.ReloadCommand; import net.knarcraft.blacksmith.config.SettingValueType; -import net.knarcraft.blacksmith.config.blacksmith.BlacksmithNPCSetting; -import net.knarcraft.blacksmith.config.blacksmith.GlobalBlacksmithSetting; +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.util.ConfigCommandHelper; import net.knarcraft.blacksmith.util.InputParsingHelper; import net.knarcraft.blacksmith.util.ItemHelper; import net.knarcraft.blacksmith.util.TypeValidationHelper; -import net.md_5.bungee.api.ChatColor; import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -20,12 +19,8 @@ import org.bukkit.enchantments.Enchantment; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Arrays; - -import static net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage.getValueChangedMessage; - /** - * The command used for changing global configuration options + * The command used for changing global blacksmith configuration options */ public class BlackSmithConfigCommand implements CommandExecutor { @@ -42,124 +37,36 @@ public class BlackSmithConfigCommand implements CommandExecutor { } GlobalBlacksmithSettings settings = BlacksmithPlugin.getInstance().getGlobalBlacksmithSettings(); - //Find which global setting the user has specified, if any - GlobalBlacksmithSetting detectedGlobalBlacksmithSetting = null; - for (GlobalBlacksmithSetting globalBlacksmithSetting : GlobalBlacksmithSetting.values()) { - if (commandName.equalsIgnoreCase(globalBlacksmithSetting.getCommandName())) { - detectedGlobalBlacksmithSetting = globalBlacksmithSetting; - break; - } - } - - //Find which npc setting the user has specified, if any - BlacksmithNPCSetting detectedBlacksmithNPCSetting = null; - for (BlacksmithNPCSetting blacksmithNpcSetting : BlacksmithNPCSetting.values()) { - if (commandName.equalsIgnoreCase(blacksmithNpcSetting.getCommandName())) { - detectedBlacksmithNPCSetting = blacksmithNpcSetting; - break; - } + //Find which setting the user has specified, if any + BlacksmithSetting detectedSetting = BlacksmithSetting.getSetting(commandName); + if (detectedSetting == null) { + return false; } //Display the current value of a setting if (args.length == 1) { - return displayCurrentValue(detectedGlobalBlacksmithSetting, detectedBlacksmithNPCSetting, settings, sender); + ConfigCommandHelper.displayCurrentValue(detectedSetting, settings, sender); + return true; } else if (args.length == 2 && isSpecialCase(commandName)) { - if (displaySpecialCaseValue(args[1], sender, detectedGlobalBlacksmithSetting, settings)) { + if (displaySpecialCaseValue(args[1], sender, detectedSetting, settings)) { return true; } } //Update the value of a special-case setting - if (args.length == 3 && updateSpecialCase(settings, detectedGlobalBlacksmithSetting, args, sender)) { + if (args.length == 3 && updateSpecialCase(settings, detectedSetting, args, sender)) { return true; } //Change the value of the specified setting - if ((detectedGlobalBlacksmithSetting != null && - TypeValidationHelper.isValid(detectedGlobalBlacksmithSetting.getValueType(), args[1], sender)) || - (detectedBlacksmithNPCSetting != null && - TypeValidationHelper.isValid(detectedBlacksmithNPCSetting.getValueType(), args[1], sender))) { - return changeValue(args, detectedGlobalBlacksmithSetting, detectedBlacksmithNPCSetting, settings, sender); - } else { - return false; - } - } - - /** - * Changes the value of the setting defined in the user's input - * - * @param args
The arguments given by the user
- * @param detectedGlobalBlacksmithSettingThe global setting recognized from the input, if any
- * @param detectedBlacksmithNPCSettingThe NPC setting recognized from the input, if any
- * @param settingsThe global settings object to get settings from
- * @param senderThe command sender to display any output to
- * @returnTrue if the value was successfully changed
- */ - private boolean changeValue(@NotNull String[] args, @Nullable GlobalBlacksmithSetting detectedGlobalBlacksmithSetting, - @Nullable BlacksmithNPCSetting detectedBlacksmithNPCSetting, - @NotNull GlobalBlacksmithSettings settings, @NotNull CommandSender sender) { - String newValue = args[1]; - if (detectedGlobalBlacksmithSetting != null) { - settings.changeValue(detectedGlobalBlacksmithSetting, newValue); - BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, - getValueChangedMessage(detectedGlobalBlacksmithSetting.getCommandName(), newValue)); - return true; - } else if (detectedBlacksmithNPCSetting != null) { - //This makes sure all arguments are treated as a sentence - if (detectedBlacksmithNPCSetting.getValueType() == SettingValueType.STRING) { - newValue = String.join(" ", Arrays.asList(args).subList(1, args.length)); - } - settings.changeValue(detectedBlacksmithNPCSetting, newValue); - BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, - getValueChangedMessage(detectedBlacksmithNPCSetting.getCommandName(), newValue)); + if (TypeValidationHelper.isValid(detectedSetting.getValueType(), args[1], sender)) { + ConfigCommandHelper.changeValue(args, detectedSetting, settings, sender); return true; } else { return false; } } - /** - * Displays the current value of the selected setting - * - * @param detectedGlobalBlacksmithSettingThe global setting recognized from the input, if any
- * @param detectedBlacksmithNPCSettingThe NPC setting recognized from the input, if any
- * @param settingsThe global settings object to get settings from
- * @param senderThe command sender to display any output to
- * @returnTrue if a settings was successfully displayed
- */ - private boolean displayCurrentValue(@Nullable GlobalBlacksmithSetting detectedGlobalBlacksmithSetting, - @Nullable BlacksmithNPCSetting detectedBlacksmithNPCSetting, - @NotNull GlobalBlacksmithSettings settings, @NotNull CommandSender sender) { - String settingValue; - String correctCommandName; - boolean printRawValue = false; - - //Find the value of the specified setting - //TODO: See if there's a way to remove this duplication - if (detectedGlobalBlacksmithSetting != null) { - settingValue = String.valueOf(settings.getRawValue(detectedGlobalBlacksmithSetting)); - correctCommandName = detectedGlobalBlacksmithSetting.getCommandName(); - } else if (detectedBlacksmithNPCSetting != null) { - settingValue = String.valueOf(settings.getRawValue(detectedBlacksmithNPCSetting)); - correctCommandName = detectedBlacksmithNPCSetting.getCommandName(); - //For messages, print an additional raw value showing which color codes are used - if (detectedBlacksmithNPCSetting.getPath().startsWith("defaults.messages")) { - printRawValue = true; - } - } else { - return false; - } - //Display the current value of the setting - BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, - BlacksmithTranslatableMessage.getCurrentValueMessage(correctCommandName, settingValue)); - //Print the value with any colors displayed as &a-f0-9 - if (printRawValue) { - sender.sendMessage(BlacksmithTranslatableMessage.getRawValueMessage( - settingValue.replace(ChatColor.COLOR_CHAR, '&'))); - } - return true; - } - /** * Displays the current value of a special-case configuration value * @@ -170,15 +77,15 @@ public class BlackSmithConfigCommand implements CommandExecutor { * @returnTrue if the value was successfully displayed
*/ private boolean displaySpecialCaseValue(@NotNull String selector, @NotNull CommandSender sender, - @Nullable GlobalBlacksmithSetting setting, + @Nullable BlacksmithSetting setting, @NotNull GlobalBlacksmithSettings settings) { - if (setting == GlobalBlacksmithSetting.BASE_PRICE || setting == GlobalBlacksmithSetting.PRICE_PER_DURABILITY_POINT) { + if (setting == BlacksmithSetting.BASE_PRICE || setting == BlacksmithSetting.PRICE_PER_DURABILITY_POINT) { Material material = InputParsingHelper.matchMaterial(selector); if (material == null) { return false; } String currentValue; - if (setting == GlobalBlacksmithSetting.BASE_PRICE) { + if (setting == BlacksmithSetting.BASE_PRICE) { currentValue = String.valueOf(settings.getBasePrice(material)); } else { currentValue = String.valueOf(settings.getPricePerDurabilityPoint(material)); @@ -187,7 +94,7 @@ public class BlackSmithConfigCommand implements CommandExecutor { BlacksmithTranslatableMessage.getItemCurrentValueMessage(setting.getCommandName(), ItemType.MATERIAL, material.name(), currentValue)); return true; - } else if (setting == GlobalBlacksmithSetting.ENCHANTMENT_COST) { + } else if (setting == BlacksmithSetting.ENCHANTMENT_COST) { Enchantment enchantment = InputParsingHelper.matchEnchantment(selector); if (enchantment == null) { return false; @@ -209,22 +116,22 @@ public class BlackSmithConfigCommand implements CommandExecutor { * @returnTrue if the command is a special case
*/ private boolean isSpecialCase(@NotNull String commandName) { - return commandName.equalsIgnoreCase(GlobalBlacksmithSetting.BASE_PRICE.getCommandName()) || - commandName.equalsIgnoreCase(GlobalBlacksmithSetting.PRICE_PER_DURABILITY_POINT.getCommandName()) || - commandName.equalsIgnoreCase(GlobalBlacksmithSetting.ENCHANTMENT_COST.getCommandName()); + return commandName.equalsIgnoreCase(BlacksmithSetting.BASE_PRICE.getCommandName()) || + commandName.equalsIgnoreCase(BlacksmithSetting.PRICE_PER_DURABILITY_POINT.getCommandName()) || + commandName.equalsIgnoreCase(BlacksmithSetting.ENCHANTMENT_COST.getCommandName()); } /** * Updates a special-case configuration value if a special-case is encountered * - * @param settingsThe settings to modify
- * @param detectedGlobalBlacksmithSettingThe global setting specified
- * @param argsAll arguments given
- * @param senderThe command sender to notify if successful
+ * @param settingsThe settings to modify
+ * @param blacksmithSettingThe setting specified
+ * @param argsAll arguments given
+ * @param senderThe command sender to notify if successful
* @returnTrue if already handled as a special case
*/ private boolean updateSpecialCase(@NotNull GlobalBlacksmithSettings settings, - @Nullable GlobalBlacksmithSetting detectedGlobalBlacksmithSetting, + @Nullable BlacksmithSetting blacksmithSetting, @NotNull String[] args, CommandSender sender) { if (InputParsingHelper.isEmpty(args[2])) { @@ -235,10 +142,10 @@ public class BlackSmithConfigCommand implements CommandExecutor { double newPrice = Double.parseDouble(args[2]); String newValue = String.valueOf(newPrice); - if (detectedGlobalBlacksmithSetting == GlobalBlacksmithSetting.BASE_PRICE || - detectedGlobalBlacksmithSetting == GlobalBlacksmithSetting.PRICE_PER_DURABILITY_POINT) { - return updatePriceSpecialCase(settings, detectedGlobalBlacksmithSetting, args[1], newPrice, sender); - } else if (detectedGlobalBlacksmithSetting == GlobalBlacksmithSetting.ENCHANTMENT_COST) { + if (blacksmithSetting == BlacksmithSetting.BASE_PRICE || + blacksmithSetting == BlacksmithSetting.PRICE_PER_DURABILITY_POINT) { + return updatePriceSpecialCase(settings, blacksmithSetting, args[1], newPrice, sender); + } else if (blacksmithSetting == BlacksmithSetting.ENCHANTMENT_COST) { //Update enchantment cost for an item Enchantment enchantment = InputParsingHelper.matchEnchantment(args[1]); if (enchantment == null) { @@ -248,7 +155,7 @@ public class BlackSmithConfigCommand implements CommandExecutor { String itemChanged = enchantment.getKey().getKey(); settings.setEnchantmentCost(enchantment, newPrice); BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, - BlacksmithTranslatableMessage.getItemValueChangedMessage(detectedGlobalBlacksmithSetting.getCommandName(), + BlacksmithTranslatableMessage.getItemValueChangedMessage(blacksmithSetting.getCommandName(), itemType, itemChanged, newValue)); return true; } else { @@ -259,15 +166,15 @@ public class BlackSmithConfigCommand implements CommandExecutor { /** * Updates a special case price configuration value if a special case is encountered * - * @param settingsThe settings to modify
- * @param detectedGlobalBlacksmithSettingThe global setting specified
- * @param materialNameThe material name to update the price for
- * @param newPriceThe new price to update to
- * @param senderThe command sender to respond to
+ * @param settingsThe settings to modify
+ * @param blacksmithSettingThe setting specified
+ * @param materialNameThe material name to update the price for
+ * @param newPriceThe new price to update to
+ * @param senderThe command sender to respond to
* @returnTrue if the input was valid, and the item(s) was/were updated
*/ private boolean updatePriceSpecialCase(@NotNull GlobalBlacksmithSettings settings, - @NotNull GlobalBlacksmithSetting detectedGlobalBlacksmithSetting, + @NotNull BlacksmithSetting blacksmithSetting, @NotNull String materialName, double newPrice, @NotNull CommandSender sender) { ItemType itemType = ItemType.MATERIAL; @@ -275,14 +182,14 @@ public class BlackSmithConfigCommand implements CommandExecutor { //Update base price or price per durability point for an item if (materialName.contains("*")) { itemChanged = materialName; - updateAllMatchedPrices(settings, detectedGlobalBlacksmithSetting, materialName, newPrice); + updateAllMatchedPrices(settings, blacksmithSetting, materialName, newPrice); } else { Material material = InputParsingHelper.matchMaterial(materialName); if (material == null) { return false; } itemChanged = material.name(); - if (detectedGlobalBlacksmithSetting == GlobalBlacksmithSetting.BASE_PRICE) { + if (blacksmithSetting == BlacksmithSetting.BASE_PRICE) { settings.setBasePrice(material, newPrice); } else { settings.setPricePerDurabilityPoint(material, newPrice); @@ -290,7 +197,7 @@ public class BlackSmithConfigCommand implements CommandExecutor { } BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, - BlacksmithTranslatableMessage.getItemValueChangedMessage(detectedGlobalBlacksmithSetting.getCommandName(), + BlacksmithTranslatableMessage.getItemValueChangedMessage(blacksmithSetting.getCommandName(), itemType, itemChanged, String.valueOf(newPrice))); return true; } @@ -298,13 +205,13 @@ public class BlackSmithConfigCommand implements CommandExecutor { /** * Updates all materials matching the material name wildcard * - * @param settingsThe settings to modify
- * @param detectedGlobalBlacksmithSettingThe global setting specified
- * @param materialNameThe wildcard material name to update cost for
- * @param newPriceThe new cost for the matched items
+ * @param settingsThe settings to modify
+ * @param blacksmithSettingThe setting specified
+ * @param materialNameThe wildcard material name to update cost for
+ * @param newPriceThe new cost for the matched items
*/ private void updateAllMatchedPrices(@NotNull GlobalBlacksmithSettings settings, - @NotNull GlobalBlacksmithSetting detectedGlobalBlacksmithSetting, + @NotNull BlacksmithSetting blacksmithSetting, @NotNull String materialName, double newPrice) { String search = InputParsingHelper.regExIfy(materialName); for (Material material : ItemHelper.getAllReforgeAbleMaterials()) { @@ -312,7 +219,7 @@ public class BlackSmithConfigCommand implements CommandExecutor { continue; } - if (detectedGlobalBlacksmithSetting == GlobalBlacksmithSetting.BASE_PRICE) { + if (blacksmithSetting == BlacksmithSetting.BASE_PRICE) { settings.setBasePrice(material, newPrice); } else { settings.setPricePerDurabilityPoint(material, newPrice); diff --git a/src/main/java/net/knarcraft/blacksmith/command/blacksmith/BlackSmithConfigTabCompleter.java b/src/main/java/net/knarcraft/blacksmith/command/blacksmith/BlackSmithConfigTabCompleter.java index 62d2d93..c8a749a 100644 --- a/src/main/java/net/knarcraft/blacksmith/command/blacksmith/BlackSmithConfigTabCompleter.java +++ b/src/main/java/net/knarcraft/blacksmith/command/blacksmith/BlackSmithConfigTabCompleter.java @@ -1,8 +1,7 @@ package net.knarcraft.blacksmith.command.blacksmith; import net.knarcraft.blacksmith.config.SettingValueType; -import net.knarcraft.blacksmith.config.blacksmith.BlacksmithNPCSetting; -import net.knarcraft.blacksmith.config.blacksmith.GlobalBlacksmithSetting; +import net.knarcraft.blacksmith.config.blacksmith.BlacksmithSetting; import net.knarcraft.blacksmith.util.InputParsingHelper; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -17,7 +16,7 @@ import static net.knarcraft.blacksmith.util.TabCompleteValuesHelper.getTabComple import static net.knarcraft.knarlib.util.TabCompletionHelper.filterMatchingContains; /** - * The tab completer for the command used for changing global configuration options + * The tab completer for the command used for changing global blacksmith configuration options */ public class BlackSmithConfigTabCompleter implements TabCompleter { @@ -32,72 +31,61 @@ public class BlackSmithConfigTabCompleter implements TabCompleter { //Arguments:The arguments given by the user
- * @returnNull if not writing a spaced message
- */ - private ListThe command specified by the user
+ * @returnNull if not writing a spaced message
+ */ + private ListThe global setting to get tab-completions for
- * @param argsThe arguments given by the user
+ * @param blacksmithSettingThe global setting to get tab-completions for
+ * @param argsThe arguments given by the user
* @returnThe tab-completions to show to the user
*/ - private ListThe global setting to get tab-completions for
- * @param commandValueThe command value used to filter between available tab-completions
+ * @param blacksmithSettingThe global setting to get tab-completions for
+ * @param commandValueThe command value used to filter between available tab-completions
* @returnThe available tab-completions
*/ - private ListThe blacksmith trait belonging to the selected NPC
- * @param blacksmithNpcSettingThe NPC setting to change
- * @param newValueThe value to change the setting to
- * @param senderThe command sender to notify about results
+ * @param blacksmithTraitThe blacksmith trait belonging to the selected NPC
+ * @param blacksmithSettingThe NPC setting to change
+ * @param newValueThe value to change the setting to
+ * @param senderThe command sender to notify about results
* @returnTrue if everything went successfully
*/ private boolean displayOrChangeNPCSetting(@NotNull BlacksmithTrait blacksmithTrait, - @NotNull BlacksmithNPCSetting blacksmithNpcSetting, + @NotNull BlacksmithSetting blacksmithSetting, @Nullable String newValue, @NotNull CommandSender sender) { if (newValue == null) { //Display the current value of the setting - displayNPCSetting(blacksmithTrait, blacksmithNpcSetting, sender); + displayNPCSetting(blacksmithTrait, blacksmithSetting, sender); } else { //If an empty value or null, clear the value instead of changing it if (InputParsingHelper.isEmpty(newValue)) { newValue = null; } else { //Abort if an invalid value is given - boolean isValidType = TypeValidationHelper.isValid(blacksmithNpcSetting.getValueType(), newValue, sender); + boolean isValidType = TypeValidationHelper.isValid(blacksmithSetting.getValueType(), newValue, sender); if (!isValidType) { return false; } @@ -86,9 +85,9 @@ public class BlackSmithEditCommand implements CommandExecutor { } //Change the setting - blacksmithTrait.getSettings().changeSetting(blacksmithNpcSetting, newValue); + blacksmithTrait.getSettings().changeValue(blacksmithSetting, newValue); BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, - getValueChangedMessage(blacksmithNpcSetting.getCommandName(), String.valueOf(newValue))); + getValueChangedMessage(blacksmithSetting.getCommandName(), String.valueOf(newValue))); //Save the changes immediately to prevent data loss on server crash CitizensAPI.getNPCRegistry().saveToStore(); } @@ -98,27 +97,27 @@ public class BlackSmithEditCommand implements CommandExecutor { /** * Displays the current value of the given NPC setting * - * @param blacksmithTraitThe blacksmith trait of the NPC to get the value from
- * @param blacksmithNpcSettingThe NPC setting to see the value of
- * @param senderThe command sender to display the value to
+ * @param blacksmithTraitThe blacksmith trait of the NPC to get the value from
+ * @param blacksmithSettingThe NPC setting to see the value of
+ * @param senderThe command sender to display the value to
*/ private void displayNPCSetting(@NotNull BlacksmithTrait blacksmithTrait, - @NotNull BlacksmithNPCSetting blacksmithNpcSetting, @NotNull CommandSender sender) { - String rawValue = String.valueOf(blacksmithTrait.getSettings().getRawValue(blacksmithNpcSetting)); + @NotNull BlacksmithSetting blacksmithSetting, @NotNull CommandSender sender) { + String rawValue = String.valueOf(blacksmithTrait.getSettings().getRawValue(blacksmithSetting)); if (InputParsingHelper.isEmpty(rawValue)) { //Display the default value, if no custom value has been specified rawValue = String.valueOf(BlacksmithPlugin.getInstance().getGlobalBlacksmithSettings().getRawValue( - blacksmithNpcSetting)); + blacksmithSetting)); BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, - getCurrentValueMessage(blacksmithNpcSetting.getCommandName(), rawValue)); + getCurrentValueMessage(blacksmithSetting.getCommandName(), rawValue)); } else { //Add a marker if the value has been customized String marker = BlacksmithPlugin.getTranslator().getTranslatedMessage( BlacksmithTranslatableMessage.SETTING_OVERRIDDEN_MARKER); BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, - getCurrentValueMessage(blacksmithNpcSetting.getCommandName(), rawValue) + marker); + getCurrentValueMessage(blacksmithSetting.getCommandName(), rawValue) + marker); } - if (blacksmithNpcSetting.getPath().startsWith("defaults.messages")) { + if (blacksmithSetting.getPath().startsWith("defaults.messages")) { sender.sendMessage(BlacksmithTranslatableMessage.getRawValueMessage( rawValue.replace(ChatColor.COLOR_CHAR, '&'))); } diff --git a/src/main/java/net/knarcraft/blacksmith/command/blacksmith/BlackSmithEditTabCompleter.java b/src/main/java/net/knarcraft/blacksmith/command/blacksmith/BlackSmithEditTabCompleter.java index 4388ff6..fb48511 100644 --- a/src/main/java/net/knarcraft/blacksmith/command/blacksmith/BlackSmithEditTabCompleter.java +++ b/src/main/java/net/knarcraft/blacksmith/command/blacksmith/BlackSmithEditTabCompleter.java @@ -1,6 +1,6 @@ package net.knarcraft.blacksmith.command.blacksmith; -import net.knarcraft.blacksmith.config.blacksmith.BlacksmithNPCSetting; +import net.knarcraft.blacksmith.config.blacksmith.BlacksmithSetting; import net.knarcraft.blacksmith.util.TabCompleteValuesHelper; import net.knarcraft.knarlib.util.TabCompletionHelper; import org.bukkit.command.Command; @@ -25,8 +25,10 @@ public class BlackSmithEditTabCompleter implements TabCompleter { } ListSome valid options for the command's argument
*/ private @Nullable ListThe command specified by the user
+ * @returnNull if not writing a spaced message
+ */ + private boolean skipCompletionForSpacedMessage(@NotNull String command) { + Setting scrapperSetting = ScrapperSetting.getSetting(command); + return scrapperSetting != null && scrapperSetting.isMessage(); + } + + /** + * Tab completes the values available for the given command + * + * @param commandNameThe name of the used command
+ * @param commandValueThe command value used to filter tab-completions
+ * @returnSome valid options for the command's argument
+ */ + private ListThe full config path for this setting
- */ - @NotNull String getPath(); - - /** - * Gets the parent item of the defined path - * - * @returnThe parent node
- */ - @NotNull String getParent(); - - /** - * Gets the value of this setting - * - * @returnThe value of this setting
- */ - @NotNull Object getDefaultValue(); - - /** - * The name of the command used to change this setting - * - * @returnThe name of this setting's command
- */ - @NotNull String getCommandName(); - - /** - * Gets the value type for this setting - * - * @returnThe value type for this setting
- */ - @NotNull SettingValueType getValueType(); - -} diff --git a/src/main/java/net/knarcraft/blacksmith/config/NPCSetting.java b/src/main/java/net/knarcraft/blacksmith/config/Setting.java similarity index 62% rename from src/main/java/net/knarcraft/blacksmith/config/NPCSetting.java rename to src/main/java/net/knarcraft/blacksmith/config/Setting.java index eb1458c..1a01033 100644 --- a/src/main/java/net/knarcraft/blacksmith/config/NPCSetting.java +++ b/src/main/java/net/knarcraft/blacksmith/config/Setting.java @@ -3,9 +3,9 @@ package net.knarcraft.blacksmith.config; import org.jetbrains.annotations.NotNull; /** - * An interface describing an NPC setting + * An interface describing a setting */ -public interface NPCSetting { +public interface Setting { /** * Gets the full config path for this setting @@ -42,4 +42,20 @@ public interface NPCSetting { */ @NotNull SettingValueType getValueType(); + /** + * Gets whether this setting can be set per-NPC, or if it's set globally + * + * @returnTrue if this setting is set per-NPC
+ */ + boolean isPerNPC(); + + /** + * Gets whether this setting is a customizable message + * + *Messages are a special case, as you generally want to see the raw formatting, not just the result.
+ * + * @returnTrue if this setting is a customizable message
+ */ + boolean isMessage(); + } diff --git a/src/main/java/net/knarcraft/blacksmith/config/Settings.java b/src/main/java/net/knarcraft/blacksmith/config/Settings.java new file mode 100644 index 0000000..6a90e5f --- /dev/null +++ b/src/main/java/net/knarcraft/blacksmith/config/Settings.java @@ -0,0 +1,26 @@ +package net.knarcraft.blacksmith.config; + +/** + * An interface describing an object for managing settings + * + * @paramThe type of setting managed
+ */ +public interface SettingsThe setting to change
+ * @param newValueThe new value of the setting
+ */ + void changeValue(K setting, Object newValue); + + /** + * Gets the current raw value of the given global setting + * + * @param settingThe setting to get
+ * @returnThe current raw setting value
+ */ + Object getRawValue(K setting); + +} diff --git a/src/main/java/net/knarcraft/blacksmith/config/blacksmith/BlacksmithNPCSettings.java b/src/main/java/net/knarcraft/blacksmith/config/blacksmith/BlacksmithNPCSettings.java index 0ce47ba..31c244d 100644 --- a/src/main/java/net/knarcraft/blacksmith/config/blacksmith/BlacksmithNPCSettings.java +++ b/src/main/java/net/knarcraft/blacksmith/config/blacksmith/BlacksmithNPCSettings.java @@ -3,6 +3,7 @@ package net.knarcraft.blacksmith.config.blacksmith; import net.citizensnpcs.api.util.DataKey; import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.config.SettingValueType; +import net.knarcraft.blacksmith.config.Settings; import net.knarcraft.blacksmith.config.SmithPreset; import net.knarcraft.blacksmith.config.TraitSettings; import net.knarcraft.blacksmith.util.ConfigHelper; @@ -23,11 +24,11 @@ import java.util.logging.Level; /** * A class which keeps track of all Blacksmith settings/config values for one NPC */ -public class BlacksmithNPCSettings implements TraitSettings { +public class BlacksmithNPCSettings implements TraitSettings, SettingsThe data key to load variables from
*/ public void loadVariables(DataKey key) { - for (BlacksmithNPCSetting setting : BlacksmithNPCSetting.values()) { + for (BlacksmithSetting setting : BlacksmithSetting.values()) { if (key.keyExists(setting.getChildPath())) { currentValues.put(setting, key.getRaw(setting.getChildPath())); } @@ -59,18 +60,13 @@ public class BlacksmithNPCSettings implements TraitSettings { * @param keyThe data key to save variables to
*/ public void saveVariables(DataKey key) { - for (BlacksmithNPCSetting setting : BlacksmithNPCSetting.values()) { + for (BlacksmithSetting setting : BlacksmithSetting.values()) { key.setRaw(setting.getChildPath(), currentValues.get(setting)); } } - /** - * Changes one setting to the given value - * - * @param settingThe setting to change
- * @param newValueThe new value of the setting
- */ - public void changeSetting(BlacksmithNPCSetting setting, Object newValue) { + @Override + public void changeValue(BlacksmithSetting setting, Object newValue) { if (setting.getValueType() == SettingValueType.STRING_LIST || setting.getValueType() == SettingValueType.REFORGE_ABLE_ITEMS) { //Workaround to make sure it's treated as the correct type @@ -78,10 +74,10 @@ public class BlacksmithNPCSettings implements TraitSettings { } else { currentValues.put(setting, newValue); } - if (setting == BlacksmithNPCSetting.REFORGE_ABLE_ITEMS) { + if (setting == BlacksmithSetting.REFORGE_ABLE_ITEMS) { updateReforgeAbleItems(); } - if (setting == BlacksmithNPCSetting.ENCHANTMENT_BLOCKLIST) { + if (setting == BlacksmithSetting.ENCHANTMENT_BLOCKLIST) { updateEnchantmentBlocklist(); } } @@ -92,13 +88,13 @@ public class BlacksmithNPCSettings implements TraitSettings { * @param settingThe setting to get the value of
* @returnThe current value of the setting
*/ - public Object getRawValue(BlacksmithNPCSetting setting) { + public Object getRawValue(BlacksmithSetting setting) { return currentValues.get(setting); } @Override public String getBusyWithPlayerMessage() { - return asString(BlacksmithNPCSetting.BUSY_WITH_PLAYER_MESSAGE); + return asString(BlacksmithSetting.BUSY_WITH_PLAYER_MESSAGE); } /** @@ -107,7 +103,7 @@ public class BlacksmithNPCSettings implements TraitSettings { * @returnThe busy reforging message
*/ public String getBusyWorkingMessage() { - return asString(BlacksmithNPCSetting.BUSY_WITH_REFORGE_MESSAGE); + return asString(BlacksmithSetting.BUSY_WITH_REFORGE_MESSAGE); } /** @@ -116,7 +112,7 @@ public class BlacksmithNPCSettings implements TraitSettings { * @returnThe message to use for displaying item cost
*/ public String getCostMessage() { - return asString(BlacksmithNPCSetting.COST_MESSAGE); + return asString(BlacksmithSetting.COST_MESSAGE); } /** @@ -125,7 +121,7 @@ public class BlacksmithNPCSettings implements TraitSettings { * @returnThe invalid item message
*/ public String getInvalidItemMessage() { - return asString(BlacksmithNPCSetting.INVALID_ITEM_MESSAGE); + return asString(BlacksmithSetting.INVALID_ITEM_MESSAGE); } /** @@ -134,12 +130,12 @@ public class BlacksmithNPCSettings implements TraitSettings { * @returnThe not damaged message
*/ public String getNotDamagedMessage() { - return asString(BlacksmithNPCSetting.NOT_DAMAGED_MESSAGE); + return asString(BlacksmithSetting.NOT_DAMAGED_MESSAGE); } @Override public String getStartWorkingMessage() { - return asString(BlacksmithNPCSetting.START_REFORGE_MESSAGE); + return asString(BlacksmithSetting.START_REFORGE_MESSAGE); } /** @@ -148,7 +144,7 @@ public class BlacksmithNPCSettings implements TraitSettings { * @returnThe reforge success message
*/ public String getSuccessMessage() { - return asString(BlacksmithNPCSetting.SUCCESS_MESSAGE); + return asString(BlacksmithSetting.SUCCESS_MESSAGE); } /** @@ -157,7 +153,7 @@ public class BlacksmithNPCSettings implements TraitSettings { * @returnThe reforge fail message
*/ public String getFailMessage() { - return asString(BlacksmithNPCSetting.FAIL_MESSAGE); + return asString(BlacksmithSetting.FAIL_MESSAGE); } /** @@ -166,12 +162,12 @@ public class BlacksmithNPCSettings implements TraitSettings { * @returnThe insufficient funds message
*/ public String getInsufficientFundsMessage() { - return asString(BlacksmithNPCSetting.INSUFFICIENT_FUNDS_MESSAGE); + return asString(BlacksmithSetting.INSUFFICIENT_FUNDS_MESSAGE); } @Override public String getCoolDownUnexpiredMessage() { - return asString(BlacksmithNPCSetting.COOL_DOWN_UNEXPIRED_MESSAGE); + return asString(BlacksmithSetting.COOL_DOWN_UNEXPIRED_MESSAGE); } /** @@ -180,7 +176,7 @@ public class BlacksmithNPCSettings implements TraitSettings { * @returnThe item changed message
*/ public String getItemChangedMessage() { - return asString(BlacksmithNPCSetting.ITEM_UNEXPECTEDLY_CHANGED_MESSAGE); + return asString(BlacksmithSetting.ITEM_UNEXPECTEDLY_CHANGED_MESSAGE); } /** @@ -191,7 +187,7 @@ public class BlacksmithNPCSettings implements TraitSettings { * @returnAll items reforge-able by this NPC
*/ public ListThe list of blocked enchantments
*/ public ListThe minimum reforge delay
*/ public int getMinReforgeDelay() { - return asInt(BlacksmithNPCSetting.MIN_REFORGE_DELAY); + return asInt(BlacksmithSetting.MIN_REFORGE_DELAY); } /** @@ -228,7 +224,7 @@ public class BlacksmithNPCSettings implements TraitSettings { * @returnThe maximum reforge delay
*/ public int getMaxReforgeDelay() { - return asInt(BlacksmithNPCSetting.MAX_REFORGE_DELAY); + return asInt(BlacksmithSetting.MAX_REFORGE_DELAY); } /** @@ -237,7 +233,7 @@ public class BlacksmithNPCSettings implements TraitSettings { * @returnThe reforge cool-down
*/ public int getReforgeCoolDown() { - return asInt(BlacksmithNPCSetting.REFORGE_COOL_DOWN); + return asInt(BlacksmithSetting.REFORGE_COOL_DOWN); } /** @@ -246,7 +242,7 @@ public class BlacksmithNPCSettings implements TraitSettings { * @returnThe fail chance
*/ public int getFailChance() { - return asInt(BlacksmithNPCSetting.FAIL_CHANCE); + return asInt(BlacksmithSetting.FAIL_CHANCE); } /** @@ -255,7 +251,7 @@ public class BlacksmithNPCSettings implements TraitSettings { * @returnWhether enchantments should be removed
*/ public boolean getFailRemovesEnchantments() { - return asBoolean(BlacksmithNPCSetting.FAIL_REMOVE_ENCHANTMENTS); + return asBoolean(BlacksmithSetting.FAIL_REMOVE_ENCHANTMENTS); } /** @@ -264,7 +260,7 @@ public class BlacksmithNPCSettings implements TraitSettings { * @returnThe extra enchantment chance
*/ public int getExtraEnchantmentChance() { - return asInt(BlacksmithNPCSetting.EXTRA_ENCHANTMENT_CHANCE); + return asInt(BlacksmithSetting.EXTRA_ENCHANTMENT_CHANCE); } /** @@ -273,7 +269,7 @@ public class BlacksmithNPCSettings implements TraitSettings { * @returnThe maximum enchantments
*/ public int getMaxEnchantments() { - return asInt(BlacksmithNPCSetting.MAX_ENCHANTMENTS); + return asInt(BlacksmithSetting.MAX_ENCHANTMENTS); } /** @@ -282,7 +278,7 @@ public class BlacksmithNPCSettings implements TraitSettings { * @returnWhether to drop reforged items on the ground
*/ public boolean getDropItem() { - return asBoolean(BlacksmithNPCSetting.DROP_ITEM); + return asBoolean(BlacksmithSetting.DROP_ITEM); } /** @@ -291,12 +287,12 @@ public class BlacksmithNPCSettings implements TraitSettings { * @returnThe title of the blacksmith
*/ public String getBlacksmithTitle() { - return asString(BlacksmithNPCSetting.BLACKSMITH_TITLE); + return asString(BlacksmithSetting.BLACKSMITH_TITLE); } @Override public boolean getDisableCoolDown() { - return asInt(BlacksmithNPCSetting.REFORGE_COOL_DOWN) <= 0; + return asInt(BlacksmithSetting.REFORGE_COOL_DOWN) <= 0; } /** @@ -305,7 +301,7 @@ public class BlacksmithNPCSettings implements TraitSettings { * @returnTrue if this blacksmith is able to repair anvils
*/ public boolean getRepairAnvils() { - return asBoolean(BlacksmithNPCSetting.REPAIR_ANVILS); + return asBoolean(BlacksmithSetting.REPAIR_ANVILS); } /** @@ -316,7 +312,7 @@ public class BlacksmithNPCSettings implements TraitSettings { * @param settingThe setting to get the value of
* @returnThe value of the given setting as an integer
*/ - private int asInt(BlacksmithNPCSetting setting) { + private int asInt(BlacksmithSetting setting) { return ConfigHelper.asInt(getValue(setting)); } @@ -326,7 +322,7 @@ public class BlacksmithNPCSettings implements TraitSettings { * @param settingThe setting to get the value of
* @returnThe value of the given setting as a string
*/ - private String asString(BlacksmithNPCSetting setting) { + private String asString(BlacksmithSetting setting) { return getValue(setting).toString(); } @@ -336,7 +332,7 @@ public class BlacksmithNPCSettings implements TraitSettings { * @param settingThe setting to get the value of
* @returnThe value of the given setting as a boolean
*/ - private boolean asBoolean(BlacksmithNPCSetting setting) { + private boolean asBoolean(BlacksmithSetting setting) { return ConfigHelper.asBoolean(getValue(setting)); } @@ -346,11 +342,11 @@ public class BlacksmithNPCSettings implements TraitSettings { * @param settingThe setting to get the value of
* @returnThe current value
*/ - private Object getValue(BlacksmithNPCSetting setting) { + private Object getValue(BlacksmithSetting setting) { Object value = currentValues.get(setting); //If not set, use the default value from the config.yml file if (value == null) { - MapIf set to false, the item will be directly put in the player's inventory instead
*/ - DROP_ITEM("dropItem", SettingValueType.BOOLEAN, true, "dropItem"), + DROP_ITEM("dropItem", SettingValueType.BOOLEAN, true, "dropItem", true, false), /** * The setting for the chance of a reforging to fail */ - FAIL_CHANCE("failReforgeChance", SettingValueType.PERCENTAGE, 10, "failReforgeChance"), + FAIL_CHANCE("failReforgeChance", SettingValueType.PERCENTAGE, 10, "failReforgeChance", + true, false), /** * The setting for whether failing a reforging should downgrade/remove enchantments as well */ FAIL_REMOVE_ENCHANTMENTS("failReforgeRemovesEnchantments", SettingValueType.BOOLEAN, false, - "failReforgeRemovesEnchantments"), + "failReforgeRemovesEnchantments", true, false), /** * The setting for the chance of an additional enchantment being added */ EXTRA_ENCHANTMENT_CHANCE("extraEnchantmentChance", SettingValueType.PERCENTAGE, 5, - "extraEnchantmentChance"), + "extraEnchantmentChance", true, false), /** * The setting for the maximum amount of enchantments that can be added to an item */ - MAX_ENCHANTMENTS("maxEnchantments", SettingValueType.POSITIVE_INTEGER, 3, "maxEnchantments"), + MAX_ENCHANTMENTS("maxEnchantments", SettingValueType.POSITIVE_INTEGER, 3, + "maxEnchantments", true, false), /** * The maximum amount of seconds a player may need to wait for the reforging to finish */ - MAX_REFORGE_DELAY("delaysInSeconds.maximum", SettingValueType.POSITIVE_INTEGER, 30, "maxReforgeDelay"), + MAX_REFORGE_DELAY("delaysInSeconds.maximum", SettingValueType.POSITIVE_INTEGER, 30, + "maxReforgeDelay", true, false), /** * The minimum amount of seconds a player may need to wait for the reforging to finish */ - MIN_REFORGE_DELAY("delaysInSeconds.minimum", SettingValueType.POSITIVE_INTEGER, 5, "minReforgeDelay"), + MIN_REFORGE_DELAY("delaysInSeconds.minimum", SettingValueType.POSITIVE_INTEGER, 5, + "minReforgeDelay", true, false), /** * The setting for number of seconds a player has to wait between each usage of the blacksmith */ - REFORGE_COOL_DOWN("delaysInSeconds.reforgeCoolDown", SettingValueType.POSITIVE_INTEGER, 60, "reforgeCoolDown"), + REFORGE_COOL_DOWN("delaysInSeconds.reforgeCoolDown", SettingValueType.POSITIVE_INTEGER, 60, + "reforgeCoolDown", true, false), /** * The setting for which items the blacksmith is able to reforge */ - REFORGE_ABLE_ITEMS("reforgeAbleItems", SettingValueType.REFORGE_ABLE_ITEMS, "", "reforgeAbleItems"), + REFORGE_ABLE_ITEMS("reforgeAbleItems", SettingValueType.REFORGE_ABLE_ITEMS, "", + "reforgeAbleItems", true, false), /** * The setting for the title used to display which kind of blacksmith the NPC is @@ -64,18 +71,20 @@ public enum BlacksmithNPCSetting implements NPCSetting { *While this should be entirely configurable, values such as armor-smith, sword-smith and similar, which * describe the blacksmith's specialization, and thus the range of reforge-able items, is expected.
*/ - BLACKSMITH_TITLE("blacksmithTitle", SettingValueType.STRING, "blacksmith", "blacksmithTitle"), + BLACKSMITH_TITLE("blacksmithTitle", SettingValueType.STRING, "blacksmith", + "blacksmithTitle", true, false), /** * The setting for the enchantments a blacksmith cannot apply to items */ ENCHANTMENT_BLOCKLIST("enchantmentBlocklist", SettingValueType.STRING_LIST, new String[]{"binding_curse", - "mending", "vanishing_curse"}, "enchantmentBlocklist"), + "mending", "vanishing_curse"}, "enchantmentBlocklist", true, false), /** * Whether to allow this blacksmith to repair anvils */ - REPAIR_ANVILS("reforgeAnvils", SettingValueType.BOOLEAN, false, "reforgeAnvils"), + REPAIR_ANVILS("reforgeAnvils", SettingValueType.BOOLEAN, false, "reforgeAnvils", + true, false), /*----------- | Messages | @@ -85,74 +94,137 @@ public enum BlacksmithNPCSetting implements NPCSetting { * The message displayed when the blacksmith is busy with another player */ BUSY_WITH_PLAYER_MESSAGE("messages.busyPlayerMessage", SettingValueType.STRING, - "&cI'm busy at the moment. Come back later!", "busyPlayerMessage"), + "&cI'm busy at the moment. Come back later!", "busyPlayerMessage", + true, true), /** * The message displayed when the blacksmith is already reforging something for the player */ BUSY_WITH_REFORGE_MESSAGE("messages.busyReforgeMessage", SettingValueType.STRING, - "&cI'm working on it. Be patient! I'll finish {time}!", "busyReforgeMessage"), + "&cI'm working on it. Be patient! I'll finish {time}!", "busyReforgeMessage", + true, true), /** * The message displayed if the player has to wait for the cool-down to expire */ COOL_DOWN_UNEXPIRED_MESSAGE("messages.coolDownUnexpiredMessage", SettingValueType.STRING, "&cYou've already had your chance! Give me a break! I'll be ready {time}!", - "coolDownUnexpiredMessage"), + "coolDownUnexpiredMessage", true, true), /** * The message displayed when displaying the cost of reforging the held item to the player */ COST_MESSAGE("messages.costMessage", SettingValueType.STRING, - "&eIt will cost &a{cost}&e to reforge that &a{item}&e! Click again to reforge!", "costMessage"), + "&eIt will cost &a{cost}&e to reforge that &a{item}&e! Click again to reforge!", + "costMessage", true, true), /** * The message displayed if the blacksmith fails reforging an item */ FAIL_MESSAGE("messages.failReforgeMessage", SettingValueType.STRING, - "&cWhoops! Didn't mean to do that! Maybe next time?", "failReforgeMessage"), + "&cWhoops! Didn't mean to do that! Maybe next time?", "failReforgeMessage", + true, true), /** * The message displayed if a player is unable to pay the blacksmith */ INSUFFICIENT_FUNDS_MESSAGE("messages.insufficientFundsMessage", SettingValueType.STRING, - "&cYou don't have enough money to reforge that item!", "insufficientFundsMessage"), + "&cYou don't have enough money to reforge that item!", "insufficientFundsMessage", + true, true), /** * The message displayed if the blacksmith encounters an item they cannot reforge */ INVALID_ITEM_MESSAGE("messages.invalidItemMessage", SettingValueType.STRING, - "&cI'm sorry, but I'm a/an {title}, I don't know how to reforge that!", "invalidItemMessage"), + "&cI'm sorry, but I'm a/an {title}, I don't know how to reforge that!", + "invalidItemMessage", true, true), /** * The message displayed if a player presents a different item after seeing the price to reforge an item */ ITEM_UNEXPECTEDLY_CHANGED_MESSAGE("messages.itemChangedMessage", SettingValueType.STRING, - "&cThat's not the item you wanted to reforge before!", "itemChangedMessage"), + "&cThat's not the item you wanted to reforge before!", "itemChangedMessage", + true, true), /** * The message displayed when the blacksmith starts reforging an item */ START_REFORGE_MESSAGE("messages.startReforgeMessage", SettingValueType.STRING, - "&eOk, let's see what I can do...", "startReforgeMessage"), + "&eOk, let's see what I can do...", "startReforgeMessage", true, true), /** * The message displayed when the blacksmith successfully finishes reforging an item */ SUCCESS_MESSAGE("messages.successMessage", SettingValueType.STRING, - "There you go! All better!", "successMessage"), + "There you go! All better!", "successMessage", true, true), /** * The message displayed when trying to reforge an item with full durability */ NOT_DAMAGED_MESSAGE("messages.notDamagedMessage", SettingValueType.STRING, - "&cThat item is not in need of repair", "notDamagedMessage"); + "&cThat item is not in need of repair", "notDamagedMessage", true, true), + + /*------------------ + | Global settings | + ------------------*/ + + /** + * The base price for repairing, regardless of durability + * + *This allows specifying a price for each item, by setting basePrice.item_name.
+ */ + BASE_PRICE("basePrice.default", SettingValueType.POSITIVE_DOUBLE, 10.0, "basePrice", + false, false), + + /** + * The base price for each durability point + * + *If natural cost, this is the cost each missing durability point will add to the cost. If not natural cost, + * this is the cost each present durability point will add to the cost. This allows specifying a price per + * durability point value for each item, by setting pricePerDurabilityPoint.item_name
+ */ + PRICE_PER_DURABILITY_POINT("pricePerDurabilityPoint.default", SettingValueType.POSITIVE_DOUBLE, + 0.005, "pricePerDurabilityPoint", false, false), + + /** + * The price increase for each level of each present enchantment + * + *This can be specified for each possible enchantment by setting enchantment-cost.enchantment_name
+ */ + ENCHANTMENT_COST("enchantmentCost.default", SettingValueType.POSITIVE_DOUBLE, 5.0, + "enchantmentCost", false, false), + + /** + * Whether the cost should increase for damage taken, as opposed to increase for durability present + */ + NATURAL_COST("useNaturalCost", SettingValueType.BOOLEAN, true, "useNaturalCost", + false, false), + + /** + * Whether to show exact time when displaying the wait time for a reforging or the cool-down + */ + SHOW_EXACT_TIME("showExactTime", SettingValueType.BOOLEAN, false, "showExactTime", + false, false), + + /** + * The cost for repairing a chipped anvil + */ + ANVIL_CHIPPED_COST("chippedAnvilReforgingCost", SettingValueType.POSITIVE_DOUBLE, 10.0, + "chippedAnvilReforgingCost", false, false), + + /** + * The cost for repairing a damaged anvil + */ + ANVIL_DAMAGED_COST("damagedAnvilReforgingCost", SettingValueType.POSITIVE_DOUBLE, 20.0, + "damagedAnvilReforgingCost", false, false); private final String path; private final String childPath; private final Object value; private final String commandName; private final SettingValueType valueType; + private final boolean isPerNPC; + private final boolean isMessage; /** * Instantiates a new setting @@ -161,13 +233,22 @@ public enum BlacksmithNPCSetting implements NPCSetting { * @param valueTypeThe type of value used by this setting
* @param valueThe default value of this setting
* @param commandNameThe name of the command used to change this setting
+ * @param isPerNPCWhether this setting is per-NPC or global
+ * @param isMessageWhether this option is for an NPC message
*/ - BlacksmithNPCSetting(String path, SettingValueType valueType, Object value, String commandName) { - this.path = "blacksmith.defaults." + path; + BlacksmithSetting(String path, SettingValueType valueType, Object value, String commandName, boolean isPerNPC, + boolean isMessage) { + if (isPerNPC) { + this.path = "blacksmith.defaults." + path; + } else { + this.path = "blacksmith.global." + path; + } this.value = value; this.valueType = valueType; this.childPath = path; this.commandName = commandName; + this.isPerNPC = isPerNPC; + this.isMessage = isMessage; } @Override @@ -195,4 +276,29 @@ public enum BlacksmithNPCSetting implements NPCSetting { return this.valueType; } + @Override + public boolean isPerNPC() { + return this.isPerNPC; + } + + @Override + public boolean isMessage() { + return this.isMessage; + } + + /** + * Gets the blacksmith setting specified by the input string + * + * @param inputThe input to check
+ * @returnThe matching blacksmith setting, or null if not found
+ */ + public static @Nullable BlacksmithSetting getSetting(@NotNull String input) { + for (BlacksmithSetting blacksmithSetting : BlacksmithSetting.values()) { + if (input.equalsIgnoreCase(blacksmithSetting.commandName)) { + return blacksmithSetting; + } + } + return null; + } + } diff --git a/src/main/java/net/knarcraft/blacksmith/config/blacksmith/GlobalBlacksmithSetting.java b/src/main/java/net/knarcraft/blacksmith/config/blacksmith/GlobalBlacksmithSetting.java deleted file mode 100644 index 54ac046..0000000 --- a/src/main/java/net/knarcraft/blacksmith/config/blacksmith/GlobalBlacksmithSetting.java +++ /dev/null @@ -1,109 +0,0 @@ -package net.knarcraft.blacksmith.config.blacksmith; - -import net.knarcraft.blacksmith.config.GlobalSetting; -import net.knarcraft.blacksmith.config.SettingValueType; -import org.jetbrains.annotations.NotNull; - -import java.util.Arrays; - -/** - * Settings which are the same for every blacksmith - */ -public enum GlobalBlacksmithSetting implements GlobalSetting { - - /** - * The base price for repairing, regardless of durability - * - *This allows specifying a price for each item, by setting basePrice.item_name.
- */ - BASE_PRICE("basePrice.default", SettingValueType.POSITIVE_DOUBLE, 10.0, "basePrice"), - - /** - * The base price for each durability point - * - *If natural cost, this is the cost each missing durability point will add to the cost. If not natural cost, - * this is the cost each present durability point will add to the cost. This allows specifying a price per - * durability point value for each item, by setting pricePerDurabilityPoint.item_name
- */ - PRICE_PER_DURABILITY_POINT("pricePerDurabilityPoint.default", SettingValueType.POSITIVE_DOUBLE, - 0.005, "pricePerDurabilityPoint"), - - /** - * The price increase for each level of each present enchantment - * - *This can be specified for each possible enchantment by setting enchantment-cost.enchantment_name
- */ - ENCHANTMENT_COST("enchantmentCost.default", SettingValueType.POSITIVE_DOUBLE, 5.0, - "enchantmentCost"), - - /** - * Whether the cost should increase for damage taken, as opposed to increase for durability present - */ - NATURAL_COST("useNaturalCost", SettingValueType.BOOLEAN, true, "useNaturalCost"), - - /** - * Whether to show exact time when displaying the wait time for a reforging or the cool-down - */ - SHOW_EXACT_TIME("showExactTime", SettingValueType.BOOLEAN, false, "showExactTime"), - - /** - * The cost for repairing a chipped anvil - */ - ANVIL_CHIPPED_COST("chippedAnvilReforgingCost", SettingValueType.POSITIVE_DOUBLE, 10.0, - "chippedAnvilReforgingCost"), - - /** - * The cost for repairing a damaged anvil - */ - ANVIL_DAMAGED_COST("damagedAnvilReforgingCost", SettingValueType.POSITIVE_DOUBLE, 20.0, - "damagedAnvilReforgingCost"); - - private final String path; - private final String parent; - private final String commandName; - private final Object value; - private final SettingValueType valueType; - - /** - * Instantiates a new setting - * - * @param pathThe full config path for this setting
- * @param valueTypeThe type of value used by this setting
- * @param valueThe default value of this setting
- * @param commandNameThe name of the command used to change this setting
- */ - GlobalBlacksmithSetting(String path, SettingValueType valueType, Object value, String commandName) { - this.path = "blacksmith.global." + path; - this.value = value; - this.commandName = commandName; - this.valueType = valueType; - String[] pathParts = path.split("\\."); - this.parent = String.join(".", Arrays.copyOfRange(pathParts, 0, pathParts.length - 1)); - } - - @Override - public @NotNull String getPath() { - return path; - } - - @Override - public @NotNull String getParent() { - return parent; - } - - @Override - public @NotNull Object getDefaultValue() { - return value; - } - - @Override - public @NotNull String getCommandName() { - return commandName; - } - - @Override - public @NotNull SettingValueType getValueType() { - return this.valueType; - } - -} 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 776df41..923a076 100644 --- a/src/main/java/net/knarcraft/blacksmith/config/blacksmith/GlobalBlacksmithSettings.java +++ b/src/main/java/net/knarcraft/blacksmith/config/blacksmith/GlobalBlacksmithSettings.java @@ -4,6 +4,7 @@ import net.citizensnpcs.api.util.DataKey; import net.citizensnpcs.api.util.YamlStorage; import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.config.SettingValueType; +import net.knarcraft.blacksmith.config.Settings; import net.knarcraft.blacksmith.util.ConfigHelper; import net.knarcraft.blacksmith.util.InputParsingHelper; import net.knarcraft.blacksmith.util.ItemHelper; @@ -20,15 +21,14 @@ import java.util.logging.Level; /** * A class which keeps track of all default NPC settings and all global settings */ -public class GlobalBlacksmithSettings { +public class GlobalBlacksmithSettings implements SettingsThe global setting to change
- * @param newValueThe new value of the setting
+ * @param blacksmithSettingThe default NPC setting to change
+ * @param newValueThe new value for the setting
*/ - public void changeValue(GlobalBlacksmithSetting globalBlacksmithSetting, Object newValue) { - globalSettings.put(globalBlacksmithSetting, newValue); - save(); - } - - /** - * Changes the value of the given setting - * - * @param blacksmithNpcSettingThe default NPC setting to change
- * @param newValueThe new value for the setting
- */ - public void changeValue(BlacksmithNPCSetting blacksmithNpcSetting, Object newValue) { - if (blacksmithNpcSetting.getValueType() == SettingValueType.STRING_LIST || - blacksmithNpcSetting.getValueType() == SettingValueType.REFORGE_ABLE_ITEMS) { + public void changeValue(BlacksmithSetting blacksmithSetting, Object newValue) { + if (blacksmithSetting.getValueType() == SettingValueType.STRING_LIST || + blacksmithSetting.getValueType() == SettingValueType.REFORGE_ABLE_ITEMS) { //Workaround to make sure it's treated as the correct type - defaultNPCSettings.put(blacksmithNpcSetting, newValue == null ? null : ConfigHelper.asStringList(newValue)); + settings.put(blacksmithSetting, newValue == null ? null : ConfigHelper.asStringList(newValue)); } else { - defaultNPCSettings.put(blacksmithNpcSetting, newValue); + settings.put(blacksmithSetting, newValue); } save(); - if (blacksmithNpcSetting == BlacksmithNPCSetting.REFORGE_ABLE_ITEMS) { + if (blacksmithSetting == BlacksmithSetting.REFORGE_ABLE_ITEMS) { loadReforgeAbleItems(); - } else if (blacksmithNpcSetting == BlacksmithNPCSetting.ENCHANTMENT_BLOCKLIST) { + } else if (blacksmithSetting == BlacksmithSetting.ENCHANTMENT_BLOCKLIST) { loadEnchantmentBlocklist(); } } - /** - * Gets the current raw value of the given global setting - * - * @param globalBlacksmithSettingThe setting to get
- * @returnThe current raw setting value
- */ - public Object getRawValue(GlobalBlacksmithSetting globalBlacksmithSetting) { - return globalSettings.get(globalBlacksmithSetting); - } - /** * Gets the current raw value of the given default NPC setting * - * @param blacksmithNpcSettingThe setting to get
+ * @param blacksmithSettingThe setting to get
* @returnThe current raw setting value
*/ - public Object getRawValue(BlacksmithNPCSetting blacksmithNpcSetting) { - return defaultNPCSettings.get(blacksmithNpcSetting); + public Object getRawValue(BlacksmithSetting blacksmithSetting) { + return settings.get(blacksmithSetting); } /** @@ -132,7 +107,7 @@ public class GlobalBlacksmithSettings { if (newEnchantmentCost < 0) { throw new IllegalArgumentException("Enchantment cost cannot be negative!"); } - globalSettings.put(GlobalBlacksmithSetting.ENCHANTMENT_COST, newEnchantmentCost); + settings.put(BlacksmithSetting.ENCHANTMENT_COST, newEnchantmentCost); } else { if (newEnchantmentCost < 0) { enchantmentCosts.put(enchantment, null); @@ -154,7 +129,7 @@ public class GlobalBlacksmithSettings { if (newPrice < 0) { throw new IllegalArgumentException("Price per durability point cannot be negative!"); } - globalSettings.put(GlobalBlacksmithSetting.PRICE_PER_DURABILITY_POINT, newPrice); + settings.put(BlacksmithSetting.PRICE_PER_DURABILITY_POINT, newPrice); } else { //Use a negative price to unset the per-item value if (newPrice < 0) { @@ -177,7 +152,7 @@ public class GlobalBlacksmithSettings { if (newBasePrice < 0) { throw new IllegalArgumentException("Base price cannot be negative!"); } - globalSettings.put(GlobalBlacksmithSetting.BASE_PRICE, newBasePrice); + settings.put(BlacksmithSetting.BASE_PRICE, newBasePrice); } else { //Use a negative price to unset the per-item value if (newBasePrice < 0) { @@ -194,8 +169,8 @@ public class GlobalBlacksmithSettings { * * @returnThe current value of the default NPC settings
*/ - public MapWhether to use natural cost
*/ public boolean getUseNaturalCost() { - return asBoolean(GlobalBlacksmithSetting.NATURAL_COST); + return asBoolean(BlacksmithSetting.NATURAL_COST); } /** @@ -216,7 +191,7 @@ public class GlobalBlacksmithSettings { * @returnWhether to show exact time
*/ public boolean getShowExactTime() { - return asBoolean(GlobalBlacksmithSetting.SHOW_EXACT_TIME); + return asBoolean(BlacksmithSetting.SHOW_EXACT_TIME); } /** @@ -229,7 +204,7 @@ public class GlobalBlacksmithSettings { if (materialBasePrices.containsKey(material) && materialBasePrices.get(material) != null) { return materialBasePrices.get(material); } else { - return asDouble(GlobalBlacksmithSetting.BASE_PRICE); + return asDouble(BlacksmithSetting.BASE_PRICE); } } @@ -244,7 +219,7 @@ public class GlobalBlacksmithSettings { materialPricePerDurabilityPoints.get(material) != null) { return materialPricePerDurabilityPoints.get(material); } else { - return asDouble(GlobalBlacksmithSetting.PRICE_PER_DURABILITY_POINT); + return asDouble(BlacksmithSetting.PRICE_PER_DURABILITY_POINT); } } @@ -258,7 +233,7 @@ public class GlobalBlacksmithSettings { if (enchantmentCosts.containsKey(enchantment) && enchantmentCosts.get(enchantment) != null) { return enchantmentCosts.get(enchantment); } else { - return asDouble(GlobalBlacksmithSetting.ENCHANTMENT_COST); + return asDouble(BlacksmithSetting.ENCHANTMENT_COST); } } @@ -288,9 +263,9 @@ public class GlobalBlacksmithSettings { */ public double getAnvilCost(Material material) { if (material == Material.CHIPPED_ANVIL) { - return asDouble(GlobalBlacksmithSetting.ANVIL_CHIPPED_COST); + return asDouble(BlacksmithSetting.ANVIL_CHIPPED_COST); } else if (material == Material.DAMAGED_ANVIL) { - return asDouble(GlobalBlacksmithSetting.ANVIL_DAMAGED_COST); + return asDouble(BlacksmithSetting.ANVIL_DAMAGED_COST); } else { throw new IllegalArgumentException("An unexpected item was encountered!"); } @@ -304,7 +279,7 @@ public class GlobalBlacksmithSettings { * @param settingThe setting to get the value of
* @returnThe value of the given setting as a boolean
*/ - public boolean asBoolean(GlobalBlacksmithSetting setting) { + public boolean asBoolean(BlacksmithSetting setting) { return ConfigHelper.asBoolean(getValue(setting)); } @@ -316,7 +291,7 @@ public class GlobalBlacksmithSettings { * @param settingThe setting to get the value of
* @returnThe value of the given setting as a double
*/ - public double asDouble(GlobalBlacksmithSetting setting) { + public double asDouble(BlacksmithSetting setting) { return ConfigHelper.asDouble(getValue(setting)); } @@ -326,8 +301,8 @@ public class GlobalBlacksmithSettings { * @param settingThe setting to get the value of
* @returnThe current value
*/ - private Object getValue(GlobalBlacksmithSetting setting) { - Object value = globalSettings.get(setting); + private Object getValue(BlacksmithSetting setting) { + Object value = settings.get(setting); //If not set in config.yml, use the default value from the enum if (value == null) { value = setting.getDefaultValue(); @@ -341,15 +316,17 @@ public class GlobalBlacksmithSettings { * @param rootThe root node of all global settings
*/ private void loadGlobalSettings(DataKey root) { - for (GlobalBlacksmithSetting globalBlacksmithSetting : GlobalBlacksmithSetting.values()) { - if (!root.keyExists(globalBlacksmithSetting.getPath())) { + for (BlacksmithSetting blacksmithSetting : BlacksmithSetting.values()) { + if (!root.keyExists(blacksmithSetting.getPath())) { //If the setting does not exist in the config file, add it - root.setRaw(globalBlacksmithSetting.getPath(), globalBlacksmithSetting.getDefaultValue()); + root.setRaw(blacksmithSetting.getPath(), blacksmithSetting.getDefaultValue()); } else { //Set the setting to the value found in the path - globalSettings.put(globalBlacksmithSetting, root.getRaw(globalBlacksmithSetting.getPath())); + settings.put(blacksmithSetting, root.getRaw(blacksmithSetting.getPath())); } } + loadReforgeAbleItems(); + loadEnchantmentBlocklist(); //Load all base prices loadBasePrices(root); @@ -358,7 +335,7 @@ public class GlobalBlacksmithSettings { loadPricesPerDurabilityPoint(root); //Load all enchantment prices - DataKey enchantmentCostNode = root.getRelative(GlobalBlacksmithSetting.ENCHANTMENT_COST.getParent()); + DataKey enchantmentCostNode = root.getRelative(BlacksmithSetting.ENCHANTMENT_COST.getPath()); MapThe configuration root node to search from
*/ private void loadPricesPerDurabilityPoint(DataKey root) { - DataKey basePerDurabilityPriceNode = root.getRelative(GlobalBlacksmithSetting.PRICE_PER_DURABILITY_POINT.getParent()); + DataKey basePerDurabilityPriceNode = root.getRelative(BlacksmithSetting.PRICE_PER_DURABILITY_POINT.getPath()); MapThe configuration root node to search from
*/ private void loadBasePrices(DataKey root) { - DataKey basePriceNode = root.getRelative(GlobalBlacksmithSetting.BASE_PRICE.getParent()); + DataKey basePriceNode = root.getRelative(BlacksmithSetting.BASE_PRICE.getPath()); MapThe root node of all default NPC settings
- */ - private void loadDefaultNPCSettings(DataKey root) { - for (BlacksmithNPCSetting setting : BlacksmithNPCSetting.values()) { - if (!root.keyExists(setting.getPath())) { - //If the setting does not exist in the config file, add it - root.setRaw(setting.getPath(), setting.getDefaultValue()); - } else { - //Set the setting to the value found in the path - defaultNPCSettings.put(setting, root.getRaw(setting.getPath())); - } - } - loadReforgeAbleItems(); - loadEnchantmentBlocklist(); - } - /** * Loads reforgeAble items from the current value */ private void loadReforgeAbleItems() { defaultReforgeAbleMaterials.clear(); - ListThe full config path for this setting
- * @param valueTypeThe type of value used by this setting
- * @param valueThe default value of this setting
- * @param commandNameThe name of the command used to change this setting
- */ - GlobalScrapperSetting(String path, SettingValueType valueType, Object value, String commandName) { - this.path = path; - this.value = value; - this.commandName = commandName; - this.valueType = valueType; - String[] pathParts = path.split("\\."); - this.parent = String.join(".", Arrays.copyOfRange(pathParts, 0, pathParts.length - 1)); - } - - @Override - public @NotNull String getPath() { - return path; - } - - @Override - public @NotNull String getParent() { - return parent; - } - - @Override - public @NotNull Object getDefaultValue() { - return value; - } - - @Override - public @NotNull String getCommandName() { - return commandName; - } - - @Override - public @NotNull SettingValueType getValueType() { - return this.valueType; - } - -} 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 e7b6f81..7586652 100644 --- a/src/main/java/net/knarcraft/blacksmith/config/scrapper/GlobalScrapperSettings.java +++ b/src/main/java/net/knarcraft/blacksmith/config/scrapper/GlobalScrapperSettings.java @@ -4,16 +4,16 @@ import net.citizensnpcs.api.util.DataKey; import net.citizensnpcs.api.util.YamlStorage; import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.config.SettingValueType; +import net.knarcraft.blacksmith.config.Settings; import net.knarcraft.blacksmith.util.ConfigHelper; import java.io.File; import java.util.HashMap; import java.util.Map; -public class GlobalScrapperSettings { +public class GlobalScrapperSettings implements SettingsThe global setting to change
- * @param newValueThe new value of the setting
+ * @param scrapperSettingThe default NPC setting to change
+ * @param newValueThe new value for the setting
*/ - public void changeValue(GlobalScrapperSetting globalScrapperSetting, Object newValue) { - globalSettings.put(globalScrapperSetting, newValue); - save(); - } - - /** - * Changes the value of the given setting - * - * @param scrapperNPCSettingThe default NPC setting to change
- * @param newValueThe new value for the setting
- */ - public void changeValue(ScrapperNPCSetting scrapperNPCSetting, Object newValue) { - if (scrapperNPCSetting.getValueType() == SettingValueType.STRING_LIST || - scrapperNPCSetting.getValueType() == SettingValueType.REFORGE_ABLE_ITEMS) { + public void changeValue(ScrapperSetting scrapperSetting, Object newValue) { + if (scrapperSetting.getValueType() == SettingValueType.STRING_LIST || + scrapperSetting.getValueType() == SettingValueType.REFORGE_ABLE_ITEMS) { //Workaround to make sure it's treated as the correct type - defaultNPCSettings.put(scrapperNPCSetting, newValue == null ? null : ConfigHelper.asStringList(newValue)); + settings.put(scrapperSetting, newValue == null ? null : ConfigHelper.asStringList(newValue)); } else { - defaultNPCSettings.put(scrapperNPCSetting, newValue); + settings.put(scrapperSetting, newValue); } save(); } @@ -81,21 +66,11 @@ public class GlobalScrapperSettings { /** * Gets the current raw value of the given global setting * - * @param globalBlacksmithSettingThe setting to get
+ * @param scrapperSettingThe setting to get
* @returnThe current raw setting value
*/ - public Object getRawValue(GlobalScrapperSetting globalBlacksmithSetting) { - return globalSettings.get(globalBlacksmithSetting); - } - - /** - * Gets the current raw value of the given default NPC setting - * - * @param scrapperNPCSettingThe setting to get
- * @returnThe current raw setting value
- */ - public Object getRawValue(ScrapperNPCSetting scrapperNPCSetting) { - return defaultNPCSettings.get(scrapperNPCSetting); + public Object getRawValue(ScrapperSetting scrapperSetting) { + return settings.get(scrapperSetting); } /** @@ -103,8 +78,8 @@ public class GlobalScrapperSettings { * * @returnThe current value of the default NPC settings
*/ - public MapWhether to show exact time
*/ public boolean getShowExactTime() { - return asBoolean(GlobalScrapperSetting.SHOW_EXACT_TIME); + return asBoolean(ScrapperSetting.SHOW_EXACT_TIME); } /** @@ -124,7 +99,7 @@ public class GlobalScrapperSettings { * @param settingThe setting to get the value of
* @returnThe value of the given setting as a boolean
*/ - public boolean asBoolean(GlobalScrapperSetting setting) { + public boolean asBoolean(ScrapperSetting setting) { return ConfigHelper.asBoolean(getValue(setting)); } @@ -136,7 +111,7 @@ public class GlobalScrapperSettings { * @param settingThe setting to get the value of
* @returnThe value of the given setting as a double
*/ - public double asDouble(GlobalScrapperSetting setting) { + public double asDouble(ScrapperSetting setting) { return ConfigHelper.asDouble(getValue(setting)); } @@ -146,8 +121,8 @@ public class GlobalScrapperSettings { * @param settingThe setting to get the value of
* @returnThe current value
*/ - private Object getValue(GlobalScrapperSetting setting) { - Object value = globalSettings.get(setting); + private Object getValue(ScrapperSetting setting) { + Object value = settings.get(setting); //If not set in config.yml, use the default value from the enum if (value == null) { value = setting.getDefaultValue(); @@ -160,31 +135,14 @@ public class GlobalScrapperSettings { * * @param rootThe root node of all global settings
*/ - private void loadGlobalSettings(DataKey root) { - for (GlobalScrapperSetting globalScrapperSetting : GlobalScrapperSetting.values()) { - if (!root.keyExists(globalScrapperSetting.getPath())) { + private void loadSettings(DataKey root) { + for (ScrapperSetting setting : ScrapperSetting.values()) { + if (!root.keyExists(setting.getChildPath())) { //If the setting does not exist in the config file, add it - root.setRaw(globalScrapperSetting.getPath(), globalScrapperSetting.getDefaultValue()); + root.setRaw(setting.getChildPath(), setting.getDefaultValue()); } else { //Set the setting to the value found in the path - globalSettings.put(globalScrapperSetting, root.getRaw(globalScrapperSetting.getPath())); - } - } - } - - /** - * Loads all default NPC settings - * - * @param rootThe root node of all default NPC settings
- */ - private void loadDefaultNPCSettings(DataKey root) { - for (ScrapperNPCSetting setting : ScrapperNPCSetting.values()) { - if (!root.keyExists(setting.getPath())) { - //If the setting does not exist in the config file, add it - root.setRaw(setting.getPath(), setting.getDefaultValue()); - } else { - //Set the setting to the value found in the path - defaultNPCSettings.put(setting, root.getRaw(setting.getPath())); + settings.put(setting, root.getRaw(setting.getChildPath())); } } } @@ -194,14 +152,9 @@ public class GlobalScrapperSettings { */ private void save() { DataKey root = defaultConfig.getKey(""); - //Save all default NPC settings - for (ScrapperNPCSetting setting : ScrapperNPCSetting.values()) { - root.setRaw(setting.getPath(), defaultNPCSettings.get(setting)); - } - - //Save all normal global settings - for (GlobalScrapperSetting globalBlacksmithSetting : GlobalScrapperSetting.values()) { - root.setRaw(globalBlacksmithSetting.getPath(), globalSettings.get(globalBlacksmithSetting)); + //Save all default settings + for (ScrapperSetting setting : ScrapperSetting.values()) { + root.setRaw(setting.getPath(), settings.get(setting)); } //Perform the actual save to disk diff --git a/src/main/java/net/knarcraft/blacksmith/config/scrapper/ScrapperNPCSettings.java b/src/main/java/net/knarcraft/blacksmith/config/scrapper/ScrapperNPCSettings.java index 7da42b0..7f9780c 100644 --- a/src/main/java/net/knarcraft/blacksmith/config/scrapper/ScrapperNPCSettings.java +++ b/src/main/java/net/knarcraft/blacksmith/config/scrapper/ScrapperNPCSettings.java @@ -2,17 +2,15 @@ package net.knarcraft.blacksmith.config.scrapper; import net.citizensnpcs.api.util.DataKey; import net.knarcraft.blacksmith.config.SettingValueType; -import net.knarcraft.blacksmith.config.SmithPreset; +import net.knarcraft.blacksmith.config.Settings; import net.knarcraft.blacksmith.config.TraitSettings; import net.knarcraft.blacksmith.util.ConfigHelper; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; import java.util.Map; -public class ScrapperNPCSettings implements TraitSettings { - private final MapThe data key to load variables from
*/ public void loadVariables(DataKey key) { - for (ScrapperNPCSetting setting : ScrapperNPCSetting.values()) { + for (ScrapperSetting setting : ScrapperSetting.values()) { if (key.keyExists(setting.getChildPath())) { currentValues.put(setting, key.getRaw(setting.getChildPath())); } @@ -43,7 +41,7 @@ public class ScrapperNPCSettings implements TraitSettings { * @param keyThe data key to save variables to
*/ public void saveVariables(DataKey key) { - for (ScrapperNPCSetting setting : ScrapperNPCSetting.values()) { + for (ScrapperSetting setting : ScrapperSetting.values()) { key.setRaw(setting.getChildPath(), currentValues.get(setting)); } } @@ -54,7 +52,7 @@ public class ScrapperNPCSettings implements TraitSettings { * @param settingThe setting to change
* @param newValueThe new value of the setting
*/ - public void changeSetting(ScrapperNPCSetting setting, Object newValue) { + public void changeValue(ScrapperSetting setting, Object newValue) { if (setting.getValueType() == SettingValueType.STRING_LIST || setting.getValueType() == SettingValueType.REFORGE_ABLE_ITEMS) { //Workaround to make sure it's treated as the correct type @@ -70,23 +68,23 @@ public class ScrapperNPCSettings implements TraitSettings { * @param settingThe setting to get the value of
* @returnThe current value of the setting
*/ - public Object getRawValue(ScrapperNPCSetting setting) { + public Object getRawValue(ScrapperSetting setting) { return currentValues.get(setting); } @Override public String getBusyWithPlayerMessage() { - return asString(ScrapperNPCSetting.BUSY_WITH_PLAYER_MESSAGE); + return asString(ScrapperSetting.BUSY_WITH_PLAYER_MESSAGE); } @Override public String getBusyWorkingMessage() { - return asString(ScrapperNPCSetting.BUSY_WITH_SALVAGE_MESSAGE); + return asString(ScrapperSetting.BUSY_WITH_SALVAGE_MESSAGE); } @Override public String getStartWorkingMessage() { - return asString(ScrapperNPCSetting.START_SALVAGE_MESSAGE); + return asString(ScrapperSetting.START_SALVAGE_MESSAGE); } /** @@ -95,19 +93,19 @@ public class ScrapperNPCSettings implements TraitSettings { * @returnThe reforge success message
*/ public String getSuccessMessage() { - return asString(ScrapperNPCSetting.SUCCESS_SALVAGE_MESSAGE); + return asString(ScrapperSetting.SUCCESS_SALVAGE_MESSAGE); } @Override public String getCoolDownUnexpiredMessage() { - return asString(ScrapperNPCSetting.COOL_DOWN_UNEXPIRED_MESSAGE); + return asString(ScrapperSetting.COOL_DOWN_UNEXPIRED_MESSAGE); } /** * The message displayed if a player presents a different item after seeing the price to salvage an item */ public String getItemChangedMessage() { - return asString(ScrapperNPCSetting.ITEM_UNEXPECTEDLY_CHANGED_MESSAGE); + return asString(ScrapperSetting.ITEM_UNEXPECTEDLY_CHANGED_MESSAGE); } /** @@ -116,7 +114,7 @@ public class ScrapperNPCSettings implements TraitSettings { * @returnThe minimum salvage delay
*/ public int getMinSalvageDelay() { - return asInt(ScrapperNPCSetting.MIN_SALVAGE_DELAY); + return asInt(ScrapperSetting.MIN_SALVAGE_DELAY); } /** @@ -125,7 +123,7 @@ public class ScrapperNPCSettings implements TraitSettings { * @returnThe maximum salvage delay
*/ public int getMaxSalvageDelay() { - return asInt(ScrapperNPCSetting.MAX_SALVAGE_DELAY); + return asInt(ScrapperSetting.MAX_SALVAGE_DELAY); } /** @@ -134,7 +132,7 @@ public class ScrapperNPCSettings implements TraitSettings { * @returnThe salvage cool-down
*/ public int getSalvageCoolDown() { - return asInt(ScrapperNPCSetting.SALVAGE_COOL_DOWN); + return asInt(ScrapperSetting.SALVAGE_COOL_DOWN); } /** @@ -143,12 +141,12 @@ public class ScrapperNPCSettings implements TraitSettings { * @returnWhether to drop reforged items on the ground
*/ public boolean getDropItem() { - return asBoolean(ScrapperNPCSetting.DROP_ITEM); + return asBoolean(ScrapperSetting.DROP_ITEM); } @Override public boolean getDisableCoolDown() { - return asInt(ScrapperNPCSetting.SALVAGE_COOL_DOWN) <= 0; + return asInt(ScrapperSetting.SALVAGE_COOL_DOWN) <= 0; } /** @@ -157,7 +155,7 @@ public class ScrapperNPCSettings implements TraitSettings { * @returnWhether to disable the reforge delay
*/ public boolean getDisableDelay() { - return asInt(ScrapperNPCSetting.MAX_SALVAGE_DELAY) <= 0; + return asInt(ScrapperSetting.MAX_SALVAGE_DELAY) <= 0; } /** @@ -168,7 +166,7 @@ public class ScrapperNPCSettings implements TraitSettings { * @param settingThe setting to get the value of
* @returnThe value of the given setting as an integer
*/ - private int asInt(ScrapperNPCSetting setting) { + private int asInt(ScrapperSetting setting) { return ConfigHelper.asInt(getValue(setting)); } @@ -178,7 +176,7 @@ public class ScrapperNPCSettings implements TraitSettings { * @param settingThe setting to get the value of
* @returnThe value of the given setting as a string
*/ - private String asString(ScrapperNPCSetting setting) { + private String asString(ScrapperSetting setting) { return getValue(setting).toString(); } @@ -188,7 +186,7 @@ public class ScrapperNPCSettings implements TraitSettings { * @param settingThe setting to get the value of
* @returnThe value of the given setting as a boolean
*/ - private boolean asBoolean(ScrapperNPCSetting setting) { + private boolean asBoolean(ScrapperSetting setting) { return ConfigHelper.asBoolean(getValue(setting)); } @@ -198,11 +196,11 @@ public class ScrapperNPCSettings implements TraitSettings { * @param settingThe setting to get the value of
* @returnThe current value
*/ - private Object getValue(ScrapperNPCSetting setting) { + private Object getValue(ScrapperSetting setting) { Object value = currentValues.get(setting); //If not set, use the default value from the config.yml file if (value == null) { - MapThe value specified by a user
- * @returnThe value with placeholders replaced
- */ - private static ListIf set to false, the item will be directly put in the player's inventory instead
*/ - DROP_ITEM("dropItem", SettingValueType.BOOLEAN, true, "dropItem"), + DROP_ITEM("dropItem", SettingValueType.BOOLEAN, true, "dropItem", + true, false), /** * The chance of a scrapper returning no salvage, regardless of item condition */ - FAIL_SALVAGE_CHANCE("failSalvageChance", SettingValueType.POSITIVE_DOUBLE, 0, "failSalvageChance"), + FAIL_SALVAGE_CHANCE("failSalvageChance", SettingValueType.POSITIVE_DOUBLE, 0, + "failSalvageChance", true, false), /** * The setting for which items a scrapper is able to salvage */ - SALVAGE_ABLE_ITEMS("salvageAbleItems", SettingValueType.REFORGE_ABLE_ITEMS, "", "salvageAbleItems"), + SALVAGE_ABLE_ITEMS("salvageAbleItems", SettingValueType.REFORGE_ABLE_ITEMS, "", + "salvageAbleItems", true, false), /** * The maximum amount of seconds a player may need to wait for the reforging to finish */ - MAX_SALVAGE_DELAY("delaysInSeconds.maximum", SettingValueType.POSITIVE_INTEGER, 30, "maxReforgeDelay"), + MAX_SALVAGE_DELAY("delaysInSeconds.maximum", SettingValueType.POSITIVE_INTEGER, 30, + "maxReforgeDelay", true, false), /** * The minimum amount of seconds a player may need to wait for the reforging to finish */ - MIN_SALVAGE_DELAY("delaysInSeconds.minimum", SettingValueType.POSITIVE_INTEGER, 5, "minReforgeDelay"), + MIN_SALVAGE_DELAY("delaysInSeconds.minimum", SettingValueType.POSITIVE_INTEGER, 5, + "minReforgeDelay", true, false), /** * The setting for number of seconds a player has to wait between each usage of the blacksmith */ - SALVAGE_COOL_DOWN("delaysInSeconds.reforgeCoolDown", SettingValueType.POSITIVE_INTEGER, 60, "reforgeCoolDown"), + SALVAGE_COOL_DOWN("delaysInSeconds.reforgeCoolDown", SettingValueType.POSITIVE_INTEGER, 60, + "reforgeCoolDown", true, false), /*----------- | Messages | @@ -46,51 +53,70 @@ public enum ScrapperNPCSetting implements NPCSetting { * The message displayed when the scrapper is busy with another player */ BUSY_WITH_PLAYER_MESSAGE("messages.busyPlayerMessage", SettingValueType.STRING, - "&cI'm busy at the moment. Come back later!", "busyPlayerMessage"), + "&cI'm busy at the moment. Come back later!", "busyPlayerMessage", + true, true), /** * The message displayed when the scrapper is busy salvaging the player's item */ BUSY_WITH_SALVAGE_MESSAGE("messages.busySalvageMessage", SettingValueType.STRING, - "&cI'm working on it. Be patient! I'll finish {time}!", "busySalvageMessage"), + "&cI'm working on it. Be patient! I'll finish {time}!", "busySalvageMessage", + true, true), /** * The message displayed if the player needs to wait for the cool-down to expire */ COOL_DOWN_UNEXPIRED_MESSAGE("messages.coolDownUnexpiredMessage", SettingValueType.STRING, "&cYou've already had your chance! Give me a break! I'll be ready {time}!", - "coolDownUnexpiredMessage"), + "coolDownUnexpiredMessage", true, true), /** * The message displayed if presented with an item that cannot be salvaged by the NPC */ CANNOT_SALVAGE_MESSAGE("messages.cannotSalvageMessage", SettingValueType.STRING, - "&cI'm unable to salvage that item", "cannotSalvageMessage"), + "&cI'm unable to salvage that item", "cannotSalvageMessage", true, true), /** * The message displayed if salvaging an item would return in no items */ TOO_DAMAGED_FOR_SALVAGE_MESSAGE("messages.tooDamagedForSalvageMessage", SettingValueType.STRING, "&cThat item is too damaged to be salvaged into anything useful", - "tooDamagedForSalvageMessage"), + "tooDamagedForSalvageMessage", true, true), /** * The message displayed if a salvage is successful */ SUCCESS_SALVAGE_MESSAGE("messages.successSalvagedMessage", SettingValueType.STRING, "&cThere you go!", - "successSalvagedMessage"), + "successSalvagedMessage", true, true), /** * The message displayed if a player presents a different item after seeing the price to reforge an item */ ITEM_UNEXPECTEDLY_CHANGED_MESSAGE("messages.itemChangedMessage", SettingValueType.STRING, - "&cThat's not the item you wanted to reforge before!", "itemChangedMessage"), + "&cThat's not the item you wanted to reforge before!", "itemChangedMessage", + true, true), /** * The message displayed when the scrapper starts salvaging an item */ START_SALVAGE_MESSAGE("messages.startSalvageMessage", SettingValueType.STRING, - "&eOk, let's see what I can do...", "startSalvageMessage"), + "&eOk, let's see what I can do...", "startSalvageMessage", true, true), + + /*------------------ + | Global settings | + ------------------*/ + + /** + * Whether to display exact time in minutes and seconds when displaying a remaining cool-down + */ + SHOW_EXACT_TIME("scrapper.global.showExactTime", SettingValueType.BOOLEAN, "false", + "showExactTime", false, false), + + /** + * Whether to give experience back when salvaging an enchanted item + */ + GIVE_EXPERIENCE("scrapper.global.giveExperience", SettingValueType.BOOLEAN, "true", + "giveExperience", false, false), ; private final String path; @@ -98,6 +124,8 @@ public enum ScrapperNPCSetting implements NPCSetting { private final Object value; private final String commandName; private final SettingValueType valueType; + private final boolean isPerNPC; + private final boolean isMessage; /** * Instantiates a new setting @@ -106,13 +134,22 @@ public enum ScrapperNPCSetting implements NPCSetting { * @param valueTypeThe type of value used by this setting
* @param valueThe default value of this setting
* @param commandNameThe name of the command used to change this setting
+ * @param isPerNPCWhether this setting is per-NPC or global
+ * @param isMessageWhether this option is for an NPC message
*/ - ScrapperNPCSetting(String path, SettingValueType valueType, Object value, String commandName) { - this.path = "scrapper.defaults." + path; + ScrapperSetting(String path, SettingValueType valueType, Object value, String commandName, boolean isPerNPC, + boolean isMessage) { + if (isPerNPC) { + this.path = "scrapper.defaults." + path; + } else { + this.path = "scrapper.global." + path; + } this.value = value; this.valueType = valueType; this.childPath = path; this.commandName = commandName; + this.isPerNPC = isPerNPC; + this.isMessage = isMessage; } @Override @@ -140,4 +177,29 @@ public enum ScrapperNPCSetting implements NPCSetting { return this.valueType; } + @Override + public boolean isPerNPC() { + return this.isPerNPC; + } + + @Override + public boolean isMessage() { + return this.isMessage; + } + + /** + * Gets the scrapper setting specified by the input string + * + * @param inputThe input to check
+ * @returnThe matching scrapper setting, or null if not found
+ */ + public static @Nullable ScrapperSetting getSetting(@NotNull String input) { + for (ScrapperSetting scrapperSetting : ScrapperSetting.values()) { + if (input.equalsIgnoreCase(scrapperSetting.commandName)) { + return scrapperSetting; + } + } + return null; + } + } diff --git a/src/main/java/net/knarcraft/blacksmith/util/ConfigCommandHelper.java b/src/main/java/net/knarcraft/blacksmith/util/ConfigCommandHelper.java new file mode 100644 index 0000000..aaddfda --- /dev/null +++ b/src/main/java/net/knarcraft/blacksmith/util/ConfigCommandHelper.java @@ -0,0 +1,81 @@ +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.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.getValueChangedMessage; + +/** + * A helper class for the configuration command + */ +public final class ConfigCommandHelper { + + private ConfigCommandHelper() { + + } + + /** + * Changes the value of the setting defined in the user's input + * + * @param argsThe arguments given by the user
+ * @param detectedSettingThe setting recognized from the input, if any
+ * @param settingsThe global settings object to get settings from
+ * @param senderThe command sender to display any output to
+ */ + public staticThe global setting recognized from the input
+ * @param settingsThe global settings object to get settings from
+ * @param senderThe command sender to display any output to
+ */ + public static