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.config.Settings; import net.knarcraft.blacksmith.util.ConfigHelper; import java.io.File; import java.util.HashMap; import java.util.Map; public class GlobalScrapperSettings implements Settings { private final Map settings = 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 settings.clear(); //Load/Save global settings loadSettings(root); //Save any modified values to disk defaultConfig.save(); } /** * Changes the value of the given setting * * @param scrapperSetting

The default NPC setting to change

* @param newValue

The new value for the setting

*/ 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 settings.put(scrapperSetting, newValue == null ? null : ConfigHelper.asStringList(newValue)); } else { settings.put(scrapperSetting, newValue); } save(); } /** * Gets the current raw value of the given global setting * * @param scrapperSetting

The setting to get

* @return

The current raw setting value

*/ public Object getRawValue(ScrapperSetting scrapperSetting) { return settings.get(scrapperSetting); } /** * 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.settings); } /** * 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(ScrapperSetting.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(ScrapperSetting 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(ScrapperSetting 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(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(); } return value; } /** * Loads all global settings * * @param root

The root node of all global settings

*/ 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(setting.getChildPath(), setting.getDefaultValue()); } else { //Set the setting to the value found in the path settings.put(setting, root.getRaw(setting.getChildPath())); } } } /** * Saves all current settings to the config file */ private void save() { DataKey root = defaultConfig.getKey(""); //Save all default settings for (ScrapperSetting setting : ScrapperSetting.values()) { root.setRaw(setting.getPath(), settings.get(setting)); } //Perform the actual save to disk defaultConfig.save(); } }