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 detectedGlobalBlacksmithSetting

The global setting recognized from the input, if any

- * @param detectedBlacksmithNPCSetting

The NPC setting recognized from the input, if any

- * @param settings

The global settings object to get settings from

- * @param sender

The command sender to display any output to

- * @return

True 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 detectedGlobalBlacksmithSetting

The global setting recognized from the input, if any

- * @param detectedBlacksmithNPCSetting

The NPC setting recognized from the input, if any

- * @param settings

The global settings object to get settings from

- * @param sender

The command sender to display any output to

- * @return

True 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 { * @return

True 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 { * @return

True 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 settings

The settings to modify

- * @param detectedGlobalBlacksmithSetting

The global setting specified

- * @param args

All arguments given

- * @param sender

The command sender to notify if successful

+ * @param settings

The settings to modify

+ * @param blacksmithSetting

The setting specified

+ * @param args

All arguments given

+ * @param sender

The command sender to notify if successful

* @return

True 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 settings

The settings to modify

- * @param detectedGlobalBlacksmithSetting

The global setting specified

- * @param materialName

The material name to update the price for

- * @param newPrice

The new price to update to

- * @param sender

The command sender to respond to

+ * @param settings

The settings to modify

+ * @param blacksmithSetting

The setting specified

+ * @param materialName

The material name to update the price for

+ * @param newPrice

The new price to update to

+ * @param sender

The command sender to respond to

* @return

True 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 settings

The settings to modify

- * @param detectedGlobalBlacksmithSetting

The global setting specified

- * @param materialName

The wildcard material name to update cost for

- * @param newPrice

The new cost for the matched items

+ * @param settings

The settings to modify

+ * @param blacksmithSetting

The setting specified

+ * @param materialName

The wildcard material name to update cost for

+ * @param newPrice

The 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: [new value/material or enchantment] [] //Prevent tab-completion when typing messages with spaces - if (skipCompletionForSpacedMessage(args) != null) { + if (args.length > 2 && skipCompletionForSpacedMessage(args[0]) != null) { return new ArrayList<>(); } if (args.length == 1) { List availableCommands = new ArrayList<>(); availableCommands.add("reload"); - for (BlacksmithNPCSetting setting : BlacksmithNPCSetting.values()) { + for (BlacksmithSetting setting : BlacksmithSetting.values()) { availableCommands.add(setting.getCommandName()); } - for (GlobalBlacksmithSetting globalBlacksmithSetting : GlobalBlacksmithSetting.values()) { - availableCommands.add(globalBlacksmithSetting.getCommandName()); - } return filterMatchingContains(availableCommands, args[0]); } else if (args.length == 2) { return tabCompleteCommandValues(args[0], args[1]); } else if (args.length == 3) { //Get per-material tab completions, or return nothing if an invalid setting was specified - for (GlobalBlacksmithSetting globalBlacksmithSetting : GlobalBlacksmithSetting.values()) { - if (globalBlacksmithSetting.getCommandName().equalsIgnoreCase(args[0])) { - return getPerTypeTabCompletions(globalBlacksmithSetting, args); - } - } - return new ArrayList<>(); - } - return null; - } - - /** - * Checks whether to tab-complete nothing because a message containing spaces is being written - * - * @param args

The arguments given by the user

- * @return

Null if not writing a spaced message

- */ - private List skipCompletionForSpacedMessage(@NotNull String[] args) { - if (args.length > 2) { - BlacksmithNPCSetting blacksmithNpcSetting = null; - for (BlacksmithNPCSetting setting : BlacksmithNPCSetting.values()) { - if (setting.getCommandName().equalsIgnoreCase(args[0])) { - blacksmithNpcSetting = setting; - break; - } - } - if (blacksmithNpcSetting != null && blacksmithNpcSetting.getPath().startsWith("defaults.messages")) { + BlacksmithSetting blacksmithSetting = BlacksmithSetting.getSetting(args[0]); + if (blacksmithSetting != null) { + return getPerTypeTabCompletions(blacksmithSetting, args); + } else { return new ArrayList<>(); } } return null; } + /** + * Checks whether to tab-complete nothing because a message containing spaces is being written + * + * @param command

The command specified by the user

+ * @return

Null if not writing a spaced message

+ */ + private List skipCompletionForSpacedMessage(@NotNull String command) { + BlacksmithSetting blacksmithSetting = BlacksmithSetting.getSetting(command); + if (blacksmithSetting != null && blacksmithSetting.isMessage()) { + return new ArrayList<>(); + } + return null; + } + /** * Gets tab-completions for a selected material or enchantment * - * @param globalBlacksmithSetting

The global setting to get tab-completions for

- * @param args

The arguments given by the user

+ * @param blacksmithSetting

The global setting to get tab-completions for

+ * @param args

The arguments given by the user

* @return

The tab-completions to show to the user

*/ - private List getPerTypeTabCompletions(@NotNull GlobalBlacksmithSetting globalBlacksmithSetting, + private List getPerTypeTabCompletions(@NotNull BlacksmithSetting blacksmithSetting, @NotNull String[] args) { //Display possible tab-completions only if a valid enchantment or material is provided - if (((globalBlacksmithSetting == GlobalBlacksmithSetting.BASE_PRICE || - globalBlacksmithSetting == GlobalBlacksmithSetting.PRICE_PER_DURABILITY_POINT) && + if (((blacksmithSetting == BlacksmithSetting.BASE_PRICE || + blacksmithSetting == BlacksmithSetting.PRICE_PER_DURABILITY_POINT) && InputParsingHelper.matchMaterial(args[1]) != null) || - (globalBlacksmithSetting == GlobalBlacksmithSetting.ENCHANTMENT_COST && + (blacksmithSetting == BlacksmithSetting.ENCHANTMENT_COST && InputParsingHelper.matchEnchantment(args[1]) != null)) { - return filterMatchingContains(getTabCompletions(globalBlacksmithSetting.getValueType()), args[2]); + return filterMatchingContains(getTabCompletions(blacksmithSetting.getValueType()), args[2]); } else { return new ArrayList<>(); } @@ -114,35 +102,29 @@ public class BlackSmithConfigTabCompleter implements TabCompleter { if (commandName.equalsIgnoreCase("reload")) { return new ArrayList<>(); } - for (GlobalBlacksmithSetting globalBlacksmithSetting : GlobalBlacksmithSetting.values()) { - if (globalBlacksmithSetting.getCommandName().equalsIgnoreCase(commandName)) { - return getCompletions(globalBlacksmithSetting, commandValue); - } + BlacksmithSetting setting = BlacksmithSetting.getSetting(commandName); + if (setting != null) { + return getCompletions(setting, commandValue); + } else { + return null; } - for (BlacksmithNPCSetting blacksmithNpcSetting : BlacksmithNPCSetting.values()) { - if (blacksmithNpcSetting.getCommandName().equalsIgnoreCase(commandName)) { - return filterMatchingContains(getTabCompletions( - blacksmithNpcSetting.getValueType()), commandValue); - } - } - return null; } /** * Gets tab-completions for the given global setting and filters on the command value * - * @param globalBlacksmithSetting

The global setting to get tab-completions for

- * @param commandValue

The command value used to filter between available tab-completions

+ * @param blacksmithSetting

The global setting to get tab-completions for

+ * @param commandValue

The command value used to filter between available tab-completions

* @return

The available tab-completions

*/ - private List getCompletions(@NotNull GlobalBlacksmithSetting globalBlacksmithSetting, + private List getCompletions(@NotNull BlacksmithSetting blacksmithSetting, @NotNull String commandValue) { List returnValues = filterMatchingContains( - getTabCompletions(globalBlacksmithSetting.getValueType()), commandValue); - if (globalBlacksmithSetting == GlobalBlacksmithSetting.BASE_PRICE || - globalBlacksmithSetting == GlobalBlacksmithSetting.PRICE_PER_DURABILITY_POINT) { + getTabCompletions(blacksmithSetting.getValueType()), commandValue); + if (blacksmithSetting == BlacksmithSetting.BASE_PRICE || + blacksmithSetting == BlacksmithSetting.PRICE_PER_DURABILITY_POINT) { returnValues.addAll(filterMatchingContains(getTabCompletions(SettingValueType.MATERIAL), commandValue)); - } else if (globalBlacksmithSetting == GlobalBlacksmithSetting.ENCHANTMENT_COST) { + } else if (blacksmithSetting == BlacksmithSetting.ENCHANTMENT_COST) { returnValues.addAll(filterMatchingContains(getTabCompletions(SettingValueType.ENCHANTMENT), commandValue)); } return returnValues; diff --git a/src/main/java/net/knarcraft/blacksmith/command/blacksmith/BlackSmithEditCommand.java b/src/main/java/net/knarcraft/blacksmith/command/blacksmith/BlackSmithEditCommand.java index 195c8e8..8bf21e6 100644 --- a/src/main/java/net/knarcraft/blacksmith/command/blacksmith/BlackSmithEditCommand.java +++ b/src/main/java/net/knarcraft/blacksmith/command/blacksmith/BlackSmithEditCommand.java @@ -4,7 +4,7 @@ import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.npc.NPC; import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.config.SettingValueType; -import net.knarcraft.blacksmith.config.blacksmith.BlacksmithNPCSetting; +import net.knarcraft.blacksmith.config.blacksmith.BlacksmithSetting; import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage; import net.knarcraft.blacksmith.trait.BlacksmithTrait; import net.knarcraft.blacksmith.util.InputParsingHelper; @@ -42,43 +42,42 @@ public class BlackSmithEditCommand implements CommandExecutor { BlacksmithTrait blacksmithTrait = npc.getTraitNullable(BlacksmithTrait.class); - for (BlacksmithNPCSetting blacksmithNpcSetting : BlacksmithNPCSetting.values()) { - String commandName = blacksmithNpcSetting.getCommandName(); - if (commandName.equalsIgnoreCase(args[0])) { - String newValue = args.length < 2 ? null : args[1]; - //This makes sure all arguments are treated as a sentence - if (blacksmithNpcSetting.getValueType() == SettingValueType.STRING && args.length > 2) { - newValue = String.join(" ", Arrays.asList(args).subList(1, args.length)); - } - return displayOrChangeNPCSetting(blacksmithTrait, blacksmithNpcSetting, newValue, sender); + BlacksmithSetting setting = BlacksmithSetting.getSetting(args[0]); + if (setting != null) { + String newValue = args.length < 2 ? null : args[1]; + //This makes sure all arguments are treated as a sentence + if (setting.getValueType() == SettingValueType.STRING && args.length > 2) { + newValue = String.join(" ", Arrays.asList(args).subList(1, args.length)); } + return displayOrChangeNPCSetting(blacksmithTrait, setting, newValue, sender); + } else { + return false; } - return false; } /** * Changes the given NPC setting, or displays the current value if a new value isn't specified * - * @param blacksmithTrait

The blacksmith trait belonging to the selected NPC

- * @param blacksmithNpcSetting

The NPC setting to change

- * @param newValue

The value to change the setting to

- * @param sender

The command sender to notify about results

+ * @param blacksmithTrait

The blacksmith trait belonging to the selected NPC

+ * @param blacksmithSetting

The NPC setting to change

+ * @param newValue

The value to change the setting to

+ * @param sender

The command sender to notify about results

* @return

True 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 blacksmithTrait

The blacksmith trait of the NPC to get the value from

- * @param blacksmithNpcSetting

The NPC setting to see the value of

- * @param sender

The command sender to display the value to

+ * @param blacksmithTrait

The blacksmith trait of the NPC to get the value from

+ * @param blacksmithSetting

The NPC setting to see the value of

+ * @param sender

The 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 { } List npcSettings = new ArrayList<>(); - for (BlacksmithNPCSetting setting : BlacksmithNPCSetting.values()) { - npcSettings.add(setting.getCommandName()); + for (BlacksmithSetting setting : BlacksmithSetting.values()) { + if (setting.isPerNPC()) { + npcSettings.add(setting.getCommandName()); + } } if (args.length == 1) { @@ -48,13 +50,13 @@ public class BlackSmithEditTabCompleter implements TabCompleter { * @return

Some valid options for the command's argument

*/ private @Nullable List tabCompleteCommandValues(@NotNull String commandName, @NotNull String commandValue) { - for (BlacksmithNPCSetting blacksmithNpcSetting : BlacksmithNPCSetting.values()) { - if (blacksmithNpcSetting.getCommandName().equalsIgnoreCase(commandName)) { - return TabCompletionHelper.filterMatchingContains(TabCompleteValuesHelper.getTabCompletions( - blacksmithNpcSetting.getValueType()), commandValue); - } + BlacksmithSetting setting = BlacksmithSetting.getSetting(commandName); + if (setting != null) { + return TabCompletionHelper.filterMatchingContains(TabCompleteValuesHelper.getTabCompletions( + setting.getValueType()), commandValue); + } else { + return null; } - return null; } } diff --git a/src/main/java/net/knarcraft/blacksmith/command/scrapper/ScrapperConfigCommand.java b/src/main/java/net/knarcraft/blacksmith/command/scrapper/ScrapperConfigCommand.java index 446604b..93f69dc 100644 --- a/src/main/java/net/knarcraft/blacksmith/command/scrapper/ScrapperConfigCommand.java +++ b/src/main/java/net/knarcraft/blacksmith/command/scrapper/ScrapperConfigCommand.java @@ -1,17 +1,53 @@ package net.knarcraft.blacksmith.command.scrapper; -import org.apache.commons.lang.NotImplementedException; +import net.knarcraft.blacksmith.BlacksmithPlugin; +import net.knarcraft.blacksmith.command.ReloadCommand; +import net.knarcraft.blacksmith.config.scrapper.GlobalScrapperSettings; +import net.knarcraft.blacksmith.config.scrapper.ScrapperSetting; +import net.knarcraft.blacksmith.util.ConfigCommandHelper; +import net.knarcraft.blacksmith.util.TypeValidationHelper; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; +/** + * The command used for changing global scrapper configuration options + */ public class ScrapperConfigCommand implements CommandExecutor { @Override - public boolean onCommand(@NotNull CommandSender commandSender, @NotNull Command command, @NotNull String s, - @NotNull String[] strings) { - throw new NotImplementedException(); + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, + @NotNull String[] args) { + if (args.length == 0) { + return false; + } + + String commandName = args[0]; + if (commandName.equalsIgnoreCase("reload")) { + return new ReloadCommand().onCommand(sender, command, label, args); + } + GlobalScrapperSettings settings = BlacksmithPlugin.getInstance().getGlobalScrapperSettings(); + + //Find which setting the user has specified, if any + ScrapperSetting detectedSetting = ScrapperSetting.getSetting(commandName); + if (detectedSetting == null) { + return false; + } + + //Display the current value of a setting + if (args.length == 1) { + ConfigCommandHelper.displayCurrentValue(detectedSetting, settings, sender); + return true; + } + + //Change the value of the specified setting + if (TypeValidationHelper.isValid(detectedSetting.getValueType(), args[1], sender)) { + ConfigCommandHelper.changeValue(args, detectedSetting, settings, sender); + return true; + } else { + return false; + } } } diff --git a/src/main/java/net/knarcraft/blacksmith/command/scrapper/ScrapperConfigTabCompleter.java b/src/main/java/net/knarcraft/blacksmith/command/scrapper/ScrapperConfigTabCompleter.java index 24cfab1..9876700 100644 --- a/src/main/java/net/knarcraft/blacksmith/command/scrapper/ScrapperConfigTabCompleter.java +++ b/src/main/java/net/knarcraft/blacksmith/command/scrapper/ScrapperConfigTabCompleter.java @@ -1,20 +1,79 @@ package net.knarcraft.blacksmith.command.scrapper; -import org.apache.commons.lang.NotImplementedException; +import net.knarcraft.blacksmith.config.Setting; +import net.knarcraft.blacksmith.config.scrapper.ScrapperSetting; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabCompleter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; import java.util.List; +import static net.knarcraft.blacksmith.util.TabCompleteValuesHelper.getTabCompletions; +import static net.knarcraft.knarlib.util.TabCompletionHelper.filterMatchingContains; + +/** + * The tab completer for the command used for changing global scrapper configuration options + */ public class ScrapperConfigTabCompleter implements TabCompleter { @Override - public @Nullable List onTabComplete(@NotNull CommandSender commandSender, @NotNull Command command, - @NotNull String s, @NotNull String[] strings) { - throw new NotImplementedException(); + public @Nullable List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, + @NotNull String s, @NotNull String[] args) { + if (!sender.hasPermission("blacksmith.admin")) { + return new ArrayList<>(); + } + + //Arguments: [new value/material or enchantment] [] + + //Prevent tab-completion when typing messages with spaces + if (args.length > 2 && skipCompletionForSpacedMessage(args[0])) { + return new ArrayList<>(); + } + + if (args.length == 1) { + List availableCommands = new ArrayList<>(); + availableCommands.add("reload"); + for (Setting setting : ScrapperSetting.values()) { + availableCommands.add(setting.getCommandName()); + } + return filterMatchingContains(availableCommands, args[0]); + } else if (args.length == 2) { + return tabCompleteCommandValues(args[0], args[1]); + } + return null; + } + + /** + * Checks whether to tab-complete nothing because a message containing spaces is being written + * + * @param command

The command specified by the user

+ * @return

Null 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 commandName

The name of the used command

+ * @param commandValue

The command value used to filter tab-completions

+ * @return

Some valid options for the command's argument

+ */ + private List tabCompleteCommandValues(@NotNull String commandName, @NotNull String commandValue) { + if (commandName.equalsIgnoreCase("reload")) { + return new ArrayList<>(); + } + Setting scrapperSetting = ScrapperSetting.getSetting(commandName); + if (scrapperSetting != null) { + return filterMatchingContains(getTabCompletions(scrapperSetting.getValueType()), commandValue); + } else { + return null; + } } } diff --git a/src/main/java/net/knarcraft/blacksmith/config/GlobalSetting.java b/src/main/java/net/knarcraft/blacksmith/config/GlobalSetting.java deleted file mode 100644 index df9d8e2..0000000 --- a/src/main/java/net/knarcraft/blacksmith/config/GlobalSetting.java +++ /dev/null @@ -1,45 +0,0 @@ -package net.knarcraft.blacksmith.config; - -import org.jetbrains.annotations.NotNull; - -/** - * An interface describing a global setting - */ -public interface GlobalSetting { - - /** - * Gets the full config path for this setting - * - * @return

The full config path for this setting

- */ - @NotNull String getPath(); - - /** - * Gets the parent item of the defined path - * - * @return

The parent node

- */ - @NotNull String getParent(); - - /** - * Gets the value of this setting - * - * @return

The value of this setting

- */ - @NotNull Object getDefaultValue(); - - /** - * The name of the command used to change this setting - * - * @return

The name of this setting's command

- */ - @NotNull String getCommandName(); - - /** - * Gets the value type for this setting - * - * @return

The 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 + * + * @return

True 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.

+ * + * @return

True 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 + * + * @param

The type of setting managed

+ */ +public interface Settings { + + /** + * Changes the value of the given setting + * + * @param setting

The setting to change

+ * @param newValue

The new value of the setting

+ */ + void changeValue(K setting, Object newValue); + + /** + * Gets the current raw value of the given global setting + * + * @param setting

The setting to get

+ * @return

The 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, Settings { private final List reforgeAbleItems = new ArrayList<>(); private final List enchantmentBlocklist = new ArrayList<>(); - private final Map currentValues = new HashMap<>(); + private final Map currentValues = new HashMap<>(); private final GlobalBlacksmithSettings globalBlacksmithSettings; /** @@ -43,7 +44,7 @@ public class BlacksmithNPCSettings implements TraitSettings { * @param key

The 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 key

The 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 setting

The setting to change

- * @param newValue

The 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 setting

The setting to get the value of

* @return

The 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 { * @return

The 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 { * @return

The 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 { * @return

The 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 { * @return

The 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 { * @return

The 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 { * @return

The 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 { * @return

The 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 { * @return

The 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 { * @return

All items reforge-able by this NPC

*/ public List getReforgeAbleItems() { - Object currentValue = currentValues.get(BlacksmithNPCSetting.REFORGE_ABLE_ITEMS); + Object currentValue = currentValues.get(BlacksmithSetting.REFORGE_ABLE_ITEMS); if (currentValue == null || String.valueOf(currentValue).isEmpty()) { return globalBlacksmithSettings.getReforgeAbleItems(); } else { @@ -205,7 +201,7 @@ public class BlacksmithNPCSettings implements TraitSettings { * @return

The list of blocked enchantments

*/ public List getEnchantmentBlocklist() { - Object currentValue = currentValues.get(BlacksmithNPCSetting.ENCHANTMENT_BLOCKLIST); + Object currentValue = currentValues.get(BlacksmithSetting.ENCHANTMENT_BLOCKLIST); if (currentValue == null || String.valueOf(currentValue).isEmpty()) { return globalBlacksmithSettings.getEnchantmentBlocklist(); } else { @@ -219,7 +215,7 @@ public class BlacksmithNPCSettings implements TraitSettings { * @return

The 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 { * @return

The 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 { * @return

The 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 { * @return

The fail chance

*/ public int getFailChance() { - return asInt(BlacksmithNPCSetting.FAIL_CHANCE); + return asInt(BlacksmithSetting.FAIL_CHANCE); } /** @@ -255,7 +251,7 @@ public class BlacksmithNPCSettings implements TraitSettings { * @return

Whether 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 { * @return

The 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 { * @return

The maximum enchantments

*/ public int getMaxEnchantments() { - return asInt(BlacksmithNPCSetting.MAX_ENCHANTMENTS); + return asInt(BlacksmithSetting.MAX_ENCHANTMENTS); } /** @@ -282,7 +278,7 @@ public class BlacksmithNPCSettings implements TraitSettings { * @return

Whether 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 { * @return

The 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 { * @return

True 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 setting

The setting to get the value of

* @return

The 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 setting

The setting to get the value of

* @return

The 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 setting

The setting to get the value of

* @return

The 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 setting

The setting to get the value of

* @return

The 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) { - Map defaultNPCSettings = globalBlacksmithSettings.getDefaultNPCSettings(); + Map defaultNPCSettings = globalBlacksmithSettings.getDefaultNPCSettings(); if (defaultNPCSettings.containsKey(setting)) { value = defaultNPCSettings.get(setting); } @@ -389,7 +385,7 @@ public class BlacksmithNPCSettings implements TraitSettings { */ private void updateEnchantmentBlocklist() { this.enchantmentBlocklist.clear(); - List enchantments = ConfigHelper.asStringList(getValue(BlacksmithNPCSetting.ENCHANTMENT_BLOCKLIST)); + List enchantments = ConfigHelper.asStringList(getValue(BlacksmithSetting.ENCHANTMENT_BLOCKLIST)); if (enchantments != null) { this.enchantmentBlocklist.addAll(getEnchantmentBlocklist(enchantments)); } @@ -425,7 +421,7 @@ public class BlacksmithNPCSettings implements TraitSettings { */ private void updateReforgeAbleItems() { this.reforgeAbleItems.clear(); - List materialStrings = ConfigHelper.asStringList(getValue(BlacksmithNPCSetting.REFORGE_ABLE_ITEMS)); + List materialStrings = ConfigHelper.asStringList(getValue(BlacksmithSetting.REFORGE_ABLE_ITEMS)); if (materialStrings != null) { this.reforgeAbleItems.addAll(getReforgeAbleItems(materialStrings)); } diff --git a/src/main/java/net/knarcraft/blacksmith/config/blacksmith/BlacksmithNPCSetting.java b/src/main/java/net/knarcraft/blacksmith/config/blacksmith/BlacksmithSetting.java similarity index 57% rename from src/main/java/net/knarcraft/blacksmith/config/blacksmith/BlacksmithNPCSetting.java rename to src/main/java/net/knarcraft/blacksmith/config/blacksmith/BlacksmithSetting.java index 0e0075c..e10a89a 100644 --- a/src/main/java/net/knarcraft/blacksmith/config/blacksmith/BlacksmithNPCSetting.java +++ b/src/main/java/net/knarcraft/blacksmith/config/blacksmith/BlacksmithSetting.java @@ -1,62 +1,69 @@ package net.knarcraft.blacksmith.config.blacksmith; -import net.knarcraft.blacksmith.config.NPCSetting; +import net.knarcraft.blacksmith.config.Setting; import net.knarcraft.blacksmith.config.SettingValueType; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** * An enum representing all of Blacksmith's settings */ -public enum BlacksmithNPCSetting implements NPCSetting { +public enum BlacksmithSetting implements Setting { /** * The setting for whether the NPC should drop an item to the ground when finished * *

If 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 valueType

The type of value used by this setting

* @param value

The default value of this setting

* @param commandName

The name of the command used to change this setting

+ * @param isPerNPC

Whether this setting is per-NPC or global

+ * @param isMessage

Whether 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 input

The input to check

+ * @return

The 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 path

The full config path for this setting

- * @param valueType

The type of value used by this setting

- * @param value

The default value of this setting

- * @param commandName

The 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 Settings { private final Map materialBasePrices = new HashMap<>(); private final Map materialPricePerDurabilityPoints = new HashMap<>(); private final Map enchantmentCosts = new HashMap<>(); - private final Map defaultNPCSettings = new HashMap<>(); + private final Map settings = new HashMap<>(); private final List defaultReforgeAbleMaterials = new ArrayList<>(); private final List defaultEnchantmentBlocklist = new ArrayList<>(); - private final Map globalSettings = new HashMap<>(); private final YamlStorage defaultConfig; @@ -47,78 +47,53 @@ public class GlobalBlacksmithSettings { * Loads all configuration values from the config file */ public void load() { - //Load the config from disk + // Load the config from disk defaultConfig.load(); DataKey root = defaultConfig.getKey(""); - //Just in case, clear existing values - defaultNPCSettings.clear(); - globalSettings.clear(); + // Just in case, clear existing values + settings.clear(); materialBasePrices.clear(); materialPricePerDurabilityPoints.clear(); enchantmentCosts.clear(); - //Load/Save NPC default settings - loadDefaultNPCSettings(root); - - //Load/Save global settings + // Load/Save settings loadGlobalSettings(root); - //Save any modified values to disk + // Save any modified values to disk defaultConfig.save(); } /** * Changes the value of the given setting * - * @param globalBlacksmithSetting

The global setting to change

- * @param newValue

The new value of the setting

+ * @param blacksmithSetting

The default NPC setting to change

+ * @param newValue

The 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 blacksmithNpcSetting

The default NPC setting to change

- * @param newValue

The 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 globalBlacksmithSetting

The setting to get

- * @return

The 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 blacksmithNpcSetting

The setting to get

+ * @param blacksmithSetting

The setting to get

* @return

The 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 { * * @return

The current value of the default NPC settings

*/ - public Map getDefaultNPCSettings() { - return new HashMap<>(this.defaultNPCSettings); + public Map getDefaultNPCSettings() { + return new HashMap<>(this.settings); } /** @@ -207,7 +182,7 @@ public class GlobalBlacksmithSettings { * @return

Whether to use natural cost

*/ public boolean getUseNaturalCost() { - return asBoolean(GlobalBlacksmithSetting.NATURAL_COST); + return asBoolean(BlacksmithSetting.NATURAL_COST); } /** @@ -216,7 +191,7 @@ public class GlobalBlacksmithSettings { * @return

Whether 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 setting

The setting to get the value of

* @return

The 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 setting

The setting to get the value of

* @return

The 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 setting

The setting to get the value of

* @return

The 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 root

The 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()); Map relevantKeys = getRelevantKeys(enchantmentCostNode); for (String key : relevantKeys.keySet()) { String enchantmentName = relevantKeys.get(key); @@ -373,7 +350,7 @@ public class GlobalBlacksmithSettings { * @param root

The 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()); Map relevantKeys = getRelevantKeys(basePerDurabilityPriceNode); for (String key : relevantKeys.keySet()) { @@ -395,7 +372,7 @@ public class GlobalBlacksmithSettings { * @param root

The 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()); Map relevantKeys = getRelevantKeys(basePriceNode); for (String key : relevantKeys.keySet()) { @@ -474,32 +451,13 @@ public class GlobalBlacksmithSettings { return normalizedName.toLowerCase().replace("_", "-"); } - /** - * Loads all default NPC settings - * - * @param root

The 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(); - List materialNames = ConfigHelper.asStringList(defaultNPCSettings.get( - BlacksmithNPCSetting.REFORGE_ABLE_ITEMS)); + List materialNames = ConfigHelper.asStringList(settings.get( + BlacksmithSetting.REFORGE_ABLE_ITEMS)); if (materialNames != null) { defaultReforgeAbleMaterials.addAll(BlacksmithNPCSettings.getReforgeAbleItems(materialNames)); } @@ -510,8 +468,8 @@ public class GlobalBlacksmithSettings { */ private void loadEnchantmentBlocklist() { defaultEnchantmentBlocklist.clear(); - List enchantmentNames = ConfigHelper.asStringList(defaultNPCSettings.get( - BlacksmithNPCSetting.ENCHANTMENT_BLOCKLIST)); + List enchantmentNames = ConfigHelper.asStringList(settings.get( + BlacksmithSetting.ENCHANTMENT_BLOCKLIST)); if (enchantmentNames != null) { defaultEnchantmentBlocklist.addAll(BlacksmithNPCSettings.getEnchantmentBlocklist(enchantmentNames)); } @@ -522,30 +480,25 @@ public class GlobalBlacksmithSettings { */ private void save() { DataKey root = defaultConfig.getKey(""); - //Save all default NPC settings - for (BlacksmithNPCSetting setting : BlacksmithNPCSetting.values()) { - root.setRaw(setting.getPath(), defaultNPCSettings.get(setting)); - } - - //Save all normal global settings - for (GlobalBlacksmithSetting globalBlacksmithSetting : GlobalBlacksmithSetting.values()) { - root.setRaw(globalBlacksmithSetting.getPath(), globalSettings.get(globalBlacksmithSetting)); + //Save all default settings + for (BlacksmithSetting setting : BlacksmithSetting.values()) { + root.setRaw(setting.getPath(), settings.get(setting)); } //Save all base prices - DataKey basePriceNode = root.getRelative(GlobalBlacksmithSetting.BASE_PRICE.getParent()); + DataKey basePriceNode = root.getRelative(BlacksmithSetting.BASE_PRICE.getPath()); for (Material material : materialBasePrices.keySet()) { basePriceNode.setRaw(unNormalizeName(material.name()), materialBasePrices.get(material)); } //Save all per-durability-point prices - DataKey basePerDurabilityPriceNode = root.getRelative(GlobalBlacksmithSetting.PRICE_PER_DURABILITY_POINT.getParent()); + DataKey basePerDurabilityPriceNode = root.getRelative(BlacksmithSetting.PRICE_PER_DURABILITY_POINT.getPath()); for (Material material : materialPricePerDurabilityPoints.keySet()) { basePerDurabilityPriceNode.setRaw(unNormalizeName(material.name()), materialPricePerDurabilityPoints.get(material)); } //Load all enchantment prices - DataKey enchantmentCostNode = root.getRelative(GlobalBlacksmithSetting.ENCHANTMENT_COST.getParent()); + DataKey enchantmentCostNode = root.getRelative(BlacksmithSetting.ENCHANTMENT_COST.getPath()); for (Enchantment enchantment : enchantmentCosts.keySet()) { enchantmentCostNode.setRaw(unNormalizeName(enchantment.getKey().getKey()), enchantmentCosts.get(enchantment)); } diff --git a/src/main/java/net/knarcraft/blacksmith/config/scrapper/GlobalScrapperSetting.java b/src/main/java/net/knarcraft/blacksmith/config/scrapper/GlobalScrapperSetting.java deleted file mode 100644 index b336631..0000000 --- a/src/main/java/net/knarcraft/blacksmith/config/scrapper/GlobalScrapperSetting.java +++ /dev/null @@ -1,75 +0,0 @@ -package net.knarcraft.blacksmith.config.scrapper; - -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 scrapper - */ -public enum GlobalScrapperSetting implements GlobalSetting { - - /** - * 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"), - - /** - * Whether to give experience back when salvaging an enchanted item - */ - GIVE_EXPERIENCE("scrapper.global.giveExperience", SettingValueType.BOOLEAN, "true", - "giveExperience"), - ; - - 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 path

The full config path for this setting

- * @param valueType

The type of value used by this setting

- * @param value

The default value of this setting

- * @param commandName

The 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 Settings { - private final Map defaultNPCSettings = new HashMap<>(); - private final Map globalSettings = new HashMap<>(); + private final Map settings = new HashMap<>(); private final YamlStorage defaultConfig; @@ -37,14 +37,10 @@ public class GlobalScrapperSettings { DataKey root = defaultConfig.getKey(""); //Just in case, clear existing values - defaultNPCSettings.clear(); - globalSettings.clear(); - - //Load/Save NPC default settings - loadDefaultNPCSettings(root); + settings.clear(); //Load/Save global settings - loadGlobalSettings(root); + loadSettings(root); //Save any modified values to disk defaultConfig.save(); @@ -53,27 +49,16 @@ public class GlobalScrapperSettings { /** * Changes the value of the given setting * - * @param globalScrapperSetting

The global setting to change

- * @param newValue

The new value of the setting

+ * @param scrapperSetting

The default NPC setting to change

+ * @param newValue

The 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 scrapperNPCSetting

The default NPC setting to change

- * @param newValue

The 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 globalBlacksmithSetting

The setting to get

+ * @param scrapperSetting

The setting to get

* @return

The 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 scrapperNPCSetting

The setting to get

- * @return

The 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 { * * @return

The current value of the default NPC settings

*/ - public Map getDefaultNPCSettings() { - return new HashMap<>(this.defaultNPCSettings); + public Map getDefaultNPCSettings() { + return new HashMap<>(this.settings); } /** @@ -113,7 +88,7 @@ public class GlobalScrapperSettings { * @return

Whether 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 setting

The setting to get the value of

* @return

The 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 setting

The setting to get the value of

* @return

The 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 setting

The setting to get the value of

* @return

The 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 root

The 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 root

The 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 Map currentValues = new HashMap<>(); +public class ScrapperNPCSettings implements TraitSettings, Settings { + private final Map currentValues = new HashMap<>(); private final GlobalScrapperSettings globalScrapperSettings; /** @@ -30,7 +28,7 @@ public class ScrapperNPCSettings implements TraitSettings { * @param key

The 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 key

The 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 setting

The setting to change

* @param newValue

The 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 setting

The setting to get the value of

* @return

The 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 { * @return

The 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 { * @return

The 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 { * @return

The 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 { * @return

The 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 { * @return

Whether 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 { * @return

Whether 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 setting

The setting to get the value of

* @return

The 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 setting

The setting to get the value of

* @return

The 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 setting

The setting to get the value of

* @return

The 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 setting

The setting to get the value of

* @return

The 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) { - Map defaultNPCSettings = globalScrapperSettings.getDefaultNPCSettings(); + Map defaultNPCSettings = globalScrapperSettings.getDefaultNPCSettings(); if (defaultNPCSettings.containsKey(setting)) { value = defaultNPCSettings.get(setting); } @@ -214,26 +212,4 @@ public class ScrapperNPCSettings implements TraitSettings { return value; } - /** - * Replaces placeholders in the given reforge-able value - * - * @param stringList

The value specified by a user

- * @return

The value with placeholders replaced

- */ - private static List replaceReforgeAblePresets(List stringList) { - List newStrings = new ArrayList<>(); - for (String item : stringList) { - if (item == null) { - continue; - } - String replaced = SmithPreset.replacePreset(item); - if (!replaced.equals(item)) { - newStrings.addAll(List.of(replaced.split(","))); - } else { - newStrings.add(item); - } - } - return newStrings; - } - } diff --git a/src/main/java/net/knarcraft/blacksmith/config/scrapper/ScrapperNPCSetting.java b/src/main/java/net/knarcraft/blacksmith/config/scrapper/ScrapperSetting.java similarity index 62% rename from src/main/java/net/knarcraft/blacksmith/config/scrapper/ScrapperNPCSetting.java rename to src/main/java/net/knarcraft/blacksmith/config/scrapper/ScrapperSetting.java index 9db0c6b..1fabc6c 100644 --- a/src/main/java/net/knarcraft/blacksmith/config/scrapper/ScrapperNPCSetting.java +++ b/src/main/java/net/knarcraft/blacksmith/config/scrapper/ScrapperSetting.java @@ -1,42 +1,49 @@ package net.knarcraft.blacksmith.config.scrapper; -import net.knarcraft.blacksmith.config.NPCSetting; +import net.knarcraft.blacksmith.config.Setting; import net.knarcraft.blacksmith.config.SettingValueType; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; -public enum ScrapperNPCSetting implements NPCSetting { +public enum ScrapperSetting implements Setting { /** * The setting for whether the NPC should drop an item to the ground when finished * *

If 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 valueType

The type of value used by this setting

* @param value

The default value of this setting

* @param commandName

The name of the command used to change this setting

+ * @param isPerNPC

Whether this setting is per-NPC or global

+ * @param isMessage

Whether 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 input

The input to check

+ * @return

The 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 args

The arguments given by the user

+ * @param detectedSetting

The setting recognized from the input, if any

+ * @param settings

The global settings object to get settings from

+ * @param sender

The command sender to display any output to

+ */ + public static void changeValue(@NotNull String[] args, + @NotNull K detectedSetting, + @NotNull Settings settings, + @NotNull CommandSender sender) { + String newValue = args[1]; + //This makes sure all arguments are treated as a sentence + if (detectedSetting.getValueType() == SettingValueType.STRING) { + newValue = String.join(" ", Arrays.asList(args).subList(1, args.length)); + } + settings.changeValue(detectedSetting, newValue); + BlacksmithPlugin.getStringFormatter().displaySuccessMessage(sender, + getValueChangedMessage(detectedSetting.getCommandName(), newValue)); + } + + /** + * Displays the current value of the selected setting + * + * @param setting

The global setting recognized from the input

+ * @param settings

The global settings object to get settings from

+ * @param sender

The command sender to display any output to

+ */ + public static void displayCurrentValue(@NotNull K setting, + @NotNull Settings settings, + @NotNull CommandSender sender) { + String settingValue; + String correctCommandName; + boolean printRawValue = false; + + //Find the value of the specified setting + settingValue = String.valueOf(settings.getRawValue(setting)); + correctCommandName = setting.getCommandName(); + + //For messages, print an additional raw value showing which color codes are used + if (setting.isPerNPC() && setting.isMessage()) { + printRawValue = true; + } + + //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, '&'))); + } + } + +}