From ac6fc430ff98a6f17b88fcf261e83c94da71d2d1 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Thu, 16 Nov 2023 17:34:37 +0100 Subject: [PATCH] Adds code for migrating old configuration values --- .../blacksmith/BlacksmithPlugin.java | 55 ++++++++++++++++++- .../scrapper/GlobalScrapperSettings.java | 18 +----- src/main/resources/config-migrations.txt | 2 + 3 files changed, 59 insertions(+), 16 deletions(-) create mode 100644 src/main/resources/config-migrations.txt diff --git a/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java b/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java index 48d6a87..a3414f5 100644 --- a/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java +++ b/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java @@ -22,6 +22,8 @@ import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.knarlib.formatting.TranslatableMessage; import net.knarcraft.knarlib.formatting.TranslatableTimeUnit; import net.knarcraft.knarlib.formatting.Translator; +import net.knarcraft.knarlib.property.ColorConversion; +import net.knarcraft.knarlib.util.FileHelper; import net.knarcraft.knarlib.util.UpdateChecker; import org.bukkit.command.CommandExecutor; import org.bukkit.command.PluginCommand; @@ -35,6 +37,9 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.Map; import java.util.logging.Level; /** @@ -142,8 +147,12 @@ public class BlacksmithPlugin extends JavaPlugin { //Copy default config to disk FileConfiguration fileConfiguration = this.getConfig(); - fileConfiguration.options().copyDefaults(true); this.saveDefaultConfig(); + if (fileConfiguration.getString("defaults.dropItem") != null) { + migrateConfig(fileConfiguration); + this.saveConfig(); + } + fileConfiguration.options().copyDefaults(true); this.reloadConfig(); this.saveConfig(); @@ -253,4 +262,48 @@ public class BlacksmithPlugin extends JavaPlugin { } } + /** + * Changes all configuration values from the old name to the new name + * + * @param fileConfiguration

The config to read from and write to

+ */ + private void migrateConfig(FileConfiguration fileConfiguration) { + //Save the old config just in case something goes wrong + try { + fileConfiguration.save(getDataFolder() + "/config.yml.old"); + } catch (IOException e) { + e.printStackTrace(); + return; + } + + //Read all available config migrations + Map migrationFields = null; + try { + InputStream inputStream = FileHelper.getInputStreamForInternalFile("/config-migrations.txt"); + if (inputStream != null) { + migrationFields = FileHelper.readKeyValuePairs(FileHelper.getBufferedReaderFromInputStream(inputStream), + "=", ColorConversion.NORMAL); + } + } catch (IOException e) { + e.printStackTrace(); + return; + } + + if (migrationFields == null) { + return; + } + + //Replace old config names with the new ones + for (String key : migrationFields.keySet()) { + if (fileConfiguration.contains(key)) { + String newPath = migrationFields.get(key); + Object oldValue = fileConfiguration.get(key); + if (!newPath.trim().isEmpty()) { + fileConfiguration.set(newPath, oldValue); + } + fileConfiguration.set(key, null); + } + } + } + } diff --git a/src/main/java/net/knarcraft/blacksmith/config/scrapper/GlobalScrapperSettings.java b/src/main/java/net/knarcraft/blacksmith/config/scrapper/GlobalScrapperSettings.java index 7586652..a5fe295 100644 --- a/src/main/java/net/knarcraft/blacksmith/config/scrapper/GlobalScrapperSettings.java +++ b/src/main/java/net/knarcraft/blacksmith/config/scrapper/GlobalScrapperSettings.java @@ -103,18 +103,6 @@ public class GlobalScrapperSettings implements Settings { 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 * @@ -137,12 +125,12 @@ public class GlobalScrapperSettings implements Settings { */ private void loadSettings(DataKey root) { for (ScrapperSetting setting : ScrapperSetting.values()) { - if (!root.keyExists(setting.getChildPath())) { + if (!root.keyExists(setting.getPath())) { //If the setting does not exist in the config file, add it - root.setRaw(setting.getChildPath(), setting.getDefaultValue()); + root.setRaw(setting.getPath(), setting.getDefaultValue()); } else { //Set the setting to the value found in the path - settings.put(setting, root.getRaw(setting.getChildPath())); + settings.put(setting, root.getRaw(setting.getPath())); } } } diff --git a/src/main/resources/config-migrations.txt b/src/main/resources/config-migrations.txt new file mode 100644 index 0000000..a3a0af6 --- /dev/null +++ b/src/main/resources/config-migrations.txt @@ -0,0 +1,2 @@ +defaults=blacksmith.defaults +global=blacksmith.global \ No newline at end of file