Improves configuration migration, and fixes some issues
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good

Fixes incorrect dropItems instead of dropItem key in config.yml
Adds some missing dropper messages to config.yml
Adds proper migration keys for all known configuration options
Adds a modified version of Stargate's StargateYamlConfiguration to allow retaining comments during configuration migration.
Fixes the annoying "[]" is not a valid repairable item
Removes the use of data keys for the global configuration
This commit is contained in:
2024-05-04 17:40:21 +02:00
parent 8b8890c408
commit 757fcdf139
9 changed files with 587 additions and 152 deletions

View File

@ -1,17 +1,15 @@
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 net.knarcraft.blacksmith.util.ItemHelper;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
@ -29,35 +27,29 @@ public class GlobalScrapperSettings implements Settings<ScrapperSetting> {
private final List<Material> defaultSalvageableMaterials = new ArrayList<>();
private final Map<Material, Set<Material>> ignoredSalvage = new HashMap<>();
private final YamlStorage defaultConfig;
private final BlacksmithPlugin instance;
/**
* Instantiates a new "Settings"
*
* @param plugin <p>A reference to the blacksmith plugin</p>
* @param instance <p>A reference to the blacksmith plugin</p>
*/
public GlobalScrapperSettings(BlacksmithPlugin plugin) {
this.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.");
public GlobalScrapperSettings(BlacksmithPlugin instance) {
this.instance = instance;
}
/**
* Loads all configuration values from the config file
*/
public void load() {
//Load the config from disk
this.defaultConfig.load();
DataKey root = this.defaultConfig.getKey("");
//Just in case, clear existing values
this.settings.clear();
//Load/Save global settings
loadSettings(root);
loadSettings();
//Save any modified values to disk
this.defaultConfig.save();
instance.saveConfig();
}
/**
@ -137,17 +129,18 @@ public class GlobalScrapperSettings implements Settings<ScrapperSetting> {
/**
* Loads all global settings
*
* @param root <p>The root node of all global settings</p>
*/
private void loadSettings(DataKey root) {
private void loadSettings() {
instance.reloadConfig();
FileConfiguration configuration = instance.getConfiguration();
for (ScrapperSetting setting : ScrapperSetting.values()) {
if (!root.keyExists(setting.getPath())) {
if (!configuration.contains(setting.getPath())) {
//If the setting does not exist in the config file, add it
root.setRaw(setting.getPath(), setting.getDefaultValue());
configuration.set(setting.getPath(), setting.getDefaultValue());
} else {
//Set the setting to the value found in the path
this.settings.put(setting, root.getRaw(setting.getPath()));
this.settings.put(setting, configuration.get(setting.getPath()));
}
}
loadSalvageAbleItems();
@ -158,14 +151,15 @@ public class GlobalScrapperSettings implements Settings<ScrapperSetting> {
* Saves all current settings to the config file
*/
private void save() {
DataKey root = this.defaultConfig.getKey("");
FileConfiguration fileConfiguration = instance.getConfiguration();
//Save all default settings
for (ScrapperSetting setting : ScrapperSetting.values()) {
root.setRaw(setting.getPath(), this.settings.get(setting));
fileConfiguration.set(setting.getPath(), this.settings.get(setting));
}
//Perform the actual save to disk
this.defaultConfig.save();
instance.saveConfig();
}
/**

View File

@ -131,7 +131,7 @@ public enum ScrapperSetting implements Setting {
* The message displayed if a player presents a different item after seeing the price to reforge an item
*/
ITEM_UNEXPECTEDLY_CHANGED_MESSAGE("itemChangedMessage", SettingValueType.STRING, "&cThat's not the item" +
" you wanted to reforge before!", "The message to display when presenting a different item than" +
" you wanted to salvage before!", "The message to display when presenting a different item than" +
" the one just evaluated", true, true),
/**