2023-11-14 16:04:48 +01:00
|
|
|
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;
|
2023-11-16 01:17:27 +01:00
|
|
|
import net.knarcraft.blacksmith.config.Settings;
|
2023-11-14 16:04:48 +01:00
|
|
|
import net.knarcraft.blacksmith.util.ConfigHelper;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2023-11-16 01:17:27 +01:00
|
|
|
public class GlobalScrapperSettings implements Settings<ScrapperSetting> {
|
2023-11-14 16:04:48 +01:00
|
|
|
|
2023-11-16 01:17:27 +01:00
|
|
|
private final Map<ScrapperSetting, Object> settings = new HashMap<>();
|
2023-11-14 16:04:48 +01:00
|
|
|
|
|
|
|
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
|
2023-11-16 01:17:27 +01:00
|
|
|
settings.clear();
|
2023-11-14 16:04:48 +01:00
|
|
|
|
|
|
|
//Load/Save global settings
|
2023-11-16 01:17:27 +01:00
|
|
|
loadSettings(root);
|
2023-11-14 16:04:48 +01:00
|
|
|
|
|
|
|
//Save any modified values to disk
|
|
|
|
defaultConfig.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Changes the value of the given setting
|
|
|
|
*
|
2023-11-16 01:17:27 +01:00
|
|
|
* @param scrapperSetting <p>The default NPC setting to change</p>
|
|
|
|
* @param newValue <p>The new value for the setting</p>
|
2023-11-14 16:04:48 +01:00
|
|
|
*/
|
2023-11-16 01:17:27 +01:00
|
|
|
public void changeValue(ScrapperSetting scrapperSetting, Object newValue) {
|
|
|
|
if (scrapperSetting.getValueType() == SettingValueType.STRING_LIST ||
|
|
|
|
scrapperSetting.getValueType() == SettingValueType.REFORGE_ABLE_ITEMS) {
|
2023-11-14 16:04:48 +01:00
|
|
|
//Workaround to make sure it's treated as the correct type
|
2023-11-16 01:17:27 +01:00
|
|
|
settings.put(scrapperSetting, newValue == null ? null : ConfigHelper.asStringList(newValue));
|
2023-11-14 16:04:48 +01:00
|
|
|
} else {
|
2023-11-16 01:17:27 +01:00
|
|
|
settings.put(scrapperSetting, newValue);
|
2023-11-14 16:04:48 +01:00
|
|
|
}
|
|
|
|
save();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the current raw value of the given global setting
|
|
|
|
*
|
2023-11-16 01:17:27 +01:00
|
|
|
* @param scrapperSetting <p>The setting to get</p>
|
2023-11-14 16:04:48 +01:00
|
|
|
* @return <p>The current raw setting value</p>
|
|
|
|
*/
|
2023-11-16 01:17:27 +01:00
|
|
|
public Object getRawValue(ScrapperSetting scrapperSetting) {
|
|
|
|
return settings.get(scrapperSetting);
|
2023-11-14 16:04:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the current value of the default NPC settings
|
|
|
|
*
|
|
|
|
* @return <p>The current value of the default NPC settings</p>
|
|
|
|
*/
|
2023-11-16 01:17:27 +01:00
|
|
|
public Map<ScrapperSetting, Object> getDefaultNPCSettings() {
|
|
|
|
return new HashMap<>(this.settings);
|
2023-11-14 16:04:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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() {
|
2023-11-16 01:17:27 +01:00
|
|
|
return asBoolean(ScrapperSetting.SHOW_EXACT_TIME);
|
2023-11-14 16:04:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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>
|
|
|
|
*/
|
2023-11-16 01:17:27 +01:00
|
|
|
public boolean asBoolean(ScrapperSetting setting) {
|
2023-11-14 16:04:48 +01:00
|
|
|
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>
|
|
|
|
*/
|
2023-11-16 01:17:27 +01:00
|
|
|
public double asDouble(ScrapperSetting setting) {
|
2023-11-14 16:04:48 +01:00
|
|
|
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>
|
|
|
|
*/
|
2023-11-16 01:17:27 +01:00
|
|
|
private Object getValue(ScrapperSetting setting) {
|
|
|
|
Object value = settings.get(setting);
|
2023-11-14 16:04:48 +01:00
|
|
|
//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>
|
|
|
|
*/
|
2023-11-16 01:17:27 +01:00
|
|
|
private void loadSettings(DataKey root) {
|
|
|
|
for (ScrapperSetting setting : ScrapperSetting.values()) {
|
|
|
|
if (!root.keyExists(setting.getChildPath())) {
|
2023-11-14 16:04:48 +01:00
|
|
|
//If the setting does not exist in the config file, add it
|
2023-11-16 01:17:27 +01:00
|
|
|
root.setRaw(setting.getChildPath(), setting.getDefaultValue());
|
2023-11-14 16:04:48 +01:00
|
|
|
} else {
|
|
|
|
//Set the setting to the value found in the path
|
2023-11-16 01:17:27 +01:00
|
|
|
settings.put(setting, root.getRaw(setting.getChildPath()));
|
2023-11-14 16:04:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Saves all current settings to the config file
|
|
|
|
*/
|
|
|
|
private void save() {
|
|
|
|
DataKey root = defaultConfig.getKey("");
|
2023-11-16 01:17:27 +01:00
|
|
|
//Save all default settings
|
|
|
|
for (ScrapperSetting setting : ScrapperSetting.values()) {
|
|
|
|
root.setRaw(setting.getPath(), settings.get(setting));
|
2023-11-14 16:04:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Perform the actual save to disk
|
|
|
|
defaultConfig.save();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|