From 30252ba51b28e3f78c200e8fa090fbea0dedba52 Mon Sep 17 00:00:00 2001 From: Telesphoreo Date: Wed, 23 Feb 2022 22:55:04 -0600 Subject: [PATCH] Auto updating files --- src/main/java/dev/plex/config/Config.java | 30 +++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/main/java/dev/plex/config/Config.java b/src/main/java/dev/plex/config/Config.java index bd4d185..4b0a39e 100644 --- a/src/main/java/dev/plex/config/Config.java +++ b/src/main/java/dev/plex/config/Config.java @@ -1,7 +1,10 @@ package dev.plex.config; import dev.plex.Plex; +import dev.plex.util.PlexLog; import java.io.File; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; import org.bukkit.configuration.file.YamlConfiguration; /** @@ -13,6 +16,7 @@ public class Config extends YamlConfiguration * The plugin instance */ private Plex plugin; + /** * The File instance */ @@ -23,6 +27,11 @@ public class Config extends YamlConfiguration */ private String name; + /** + * Whether new entries were added to the file automatically + */ + private boolean added = false; + /** * Creates a config object * @@ -48,6 +57,27 @@ public class Config extends YamlConfiguration { try { + YamlConfiguration externalYamlConfig = YamlConfiguration.loadConfiguration(file); + InputStreamReader internalConfigFileStream = new InputStreamReader(Plex.get().getResource(name), StandardCharsets.UTF_8); + YamlConfiguration internalYamlConfig = YamlConfiguration.loadConfiguration(internalConfigFileStream); + + // Gets all the keys inside the internal file and iterates through all of it's key pairs + for (String string : internalYamlConfig.getKeys(true)) + { + // Checks if the external file contains the key already. + if (!externalYamlConfig.contains(string)) + { + // If it doesn't contain the key, we set the key based off what was found inside the plugin jar + externalYamlConfig.set(string, internalYamlConfig.get(string)); + PlexLog.log("Setting key: " + string + " to the default value(s) since it does not exist!"); + added = true; + } + } + if (added) + { + externalYamlConfig.save(file); + PlexLog.log("Saving new configuration file..."); + } super.load(file); } catch (Exception ex)