package net.knarcraft.blacksmith.config.scrapper; 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.util.ConfigHelper; import java.io.File; import java.util.HashMap; import java.util.Map; public class GlobalScrapperSettings { private final Map defaultNPCSettings = new HashMap<>(); private final Map globalSettings = new HashMap<>(); private final YamlStorage defaultConfig; /** * Instantiates a new "Settings" * * @param plugin

A reference to the blacksmith plugin

*/ public GlobalScrapperSettings(BlacksmithPlugin plugin) { defaultConfig = new YamlStorage(new File(plugin.getDataFolder() + File.separator + "config.yml"), "Scrapper Configuration\nWarning: The values under defaults are the values set for a " + "scrapper upon creation. To change any values for existing NPCs, edit the citizens NPC file."); } /** * Loads all configuration values from the config file */ public void load() { //Load the config from disk defaultConfig.load(); DataKey root = defaultConfig.getKey(""); //Just in case, clear existing values defaultNPCSettings.clear(); globalSettings.clear(); //Load/Save NPC default settings loadDefaultNPCSettings(root); //Load/Save global settings loadGlobalSettings(root); //Save any modified values to disk defaultConfig.save(); } /** * Changes the value of the given setting * * @param globalScrapperSetting

The global setting to change

* @param newValue

The new value of 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) { //Workaround to make sure it's treated as the correct type defaultNPCSettings.put(scrapperNPCSetting, newValue == null ? null : ConfigHelper.asStringList(newValue)); } else { defaultNPCSettings.put(scrapperNPCSetting, newValue); } save(); } /** * 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(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); } /** * Gets the current value of the default NPC settings * * @return

The current value of the default NPC settings

*/ public Map getDefaultNPCSettings() { return new HashMap<>(this.defaultNPCSettings); } /** * Gets whether to show exact time for reforging wait-time, and for wait-time between sessions * * @return

Whether to show exact time

*/ public boolean getShowExactTime() { return asBoolean(GlobalScrapperSetting.SHOW_EXACT_TIME); } /** * Gets the given value as a boolean * *

This will throw an exception if used for a non-boolean value

* * @param setting

The setting to get the value of

* @return

The value of the given setting as a boolean

*/ public boolean asBoolean(GlobalScrapperSetting setting) { return ConfigHelper.asBoolean(getValue(setting)); } /** * Gets the given value as a double * *

This will throw an exception if used for a non-double setting

* * @param setting

The setting to get the value of

* @return

The value of the given setting as a double

*/ public double asDouble(GlobalScrapperSetting setting) { return ConfigHelper.asDouble(getValue(setting)); } /** * Gets the value of a setting, using the default if not set * * @param setting

The setting to get the value of

* @return

The current value

*/ private Object getValue(GlobalScrapperSetting setting) { Object value = globalSettings.get(setting); //If not set in config.yml, use the default value from the enum if (value == null) { value = setting.getDefaultValue(); } return value; } /** * Loads all global settings * * @param root

The root node of all global settings

*/ private void loadGlobalSettings(DataKey root) { for (GlobalScrapperSetting globalScrapperSetting : GlobalScrapperSetting.values()) { if (!root.keyExists(globalScrapperSetting.getPath())) { //If the setting does not exist in the config file, add it root.setRaw(globalScrapperSetting.getPath(), globalScrapperSetting.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())); } } } /** * Saves all current settings to the config file */ 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)); } //Perform the actual save to disk defaultConfig.save(); } }