Blacksmith/src/main/java/net/knarcraft/blacksmith/config/scrapper/GlobalScrapperSettings.java

212 lines
7.3 KiB
Java
Raw Normal View History

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<ScrapperNPCSetting, Object> defaultNPCSettings = new HashMap<>();
private final Map<GlobalScrapperSetting, Object> globalSettings = new HashMap<>();
private final YamlStorage defaultConfig;
/**
* Instantiates a new "Settings"
*
* @param plugin <p>A reference to the blacksmith plugin</p>
*/
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 <p>The global setting to change</p>
* @param newValue <p>The new value of the setting</p>
*/
public void changeValue(GlobalScrapperSetting globalScrapperSetting, Object newValue) {
globalSettings.put(globalScrapperSetting, newValue);
save();
}
/**
* Changes the value of the given setting
*
* @param scrapperNPCSetting <p>The default NPC setting to change</p>
* @param newValue <p>The new value for the setting</p>
*/
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 <p>The setting to get</p>
* @return <p>The current raw setting value</p>
*/
public Object getRawValue(GlobalScrapperSetting globalBlacksmithSetting) {
return globalSettings.get(globalBlacksmithSetting);
}
/**
* Gets the current raw value of the given default NPC setting
*
* @param scrapperNPCSetting <p>The setting to get</p>
* @return <p>The current raw setting value</p>
*/
public Object getRawValue(ScrapperNPCSetting scrapperNPCSetting) {
return defaultNPCSettings.get(scrapperNPCSetting);
}
/**
* Gets the current value of the default NPC settings
*
* @return <p>The current value of the default NPC settings</p>
*/
public Map<ScrapperNPCSetting, Object> getDefaultNPCSettings() {
return new HashMap<>(this.defaultNPCSettings);
}
/**
* Gets whether to show exact time for reforging wait-time, and for wait-time between sessions
*
* @return <p>Whether to show exact time</p>
*/
public boolean getShowExactTime() {
return asBoolean(GlobalScrapperSetting.SHOW_EXACT_TIME);
}
/**
* Gets the given value as a boolean
*
* <p>This will throw an exception if used for a non-boolean value</p>
*
* @param setting <p>The setting to get the value of</p>
* @return <p>The value of the given setting as a boolean</p>
*/
public boolean asBoolean(GlobalScrapperSetting setting) {
return ConfigHelper.asBoolean(getValue(setting));
}
/**
* Gets the given value as a double
*
* <p>This will throw an exception if used for a non-double setting</p>
*
* @param setting <p>The setting to get the value of</p>
* @return <p>The value of the given setting as a double</p>
*/
public double asDouble(GlobalScrapperSetting setting) {
return ConfigHelper.asDouble(getValue(setting));
}
/**
* Gets the value of a setting, using the default if not set
*
* @param setting <p>The setting to get the value of</p>
* @return <p>The current value</p>
*/
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 <p>The root node of all global settings</p>
*/
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 <p>The root node of all default NPC settings</p>
*/
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();
}
}