Struggling to overwrite old config when versions don't match.

Discussion in 'Plugin Development' started by Benyon, Sep 7, 2019.

  1. Offline

    Benyon

    What I'm attempting to do is when onEnable() is executed, it will load the config, find verison, which is currently 1.0 in the deployed folder, compare it to the version number of the plugin which is 1.1, if this doesn't match, delete the old config file, and reload new from the config file in the plugin, which is 1.1.

    The actual outcome is that when attempting to save default config again, it says one already exists in the console, I have attached the code, and I will attach the FileManager class too.

    Code:
            fileManager = new FileManager(this);
    
            if (fileManager.getConfig("Config.yml").get().get("version") != getDescription().getVersion()) {
                fileManager.getConfig("Config.yml").saveDefaultConfig();
                getLogger().info("Config version does not match plugin version, overwriting config, please ensure you adjust appropriately...");
            }
    I have verified the if statement is passing 'true'

    Code:
    public class FileManager {
    
        private final JavaPlugin plugin;
        private HashMap<String, Config> configs = new HashMap<String, Config>();
    
        public FileManager(JavaPlugin plugin) {
            this.plugin = plugin;
        }
    
        public Config getConfig(String name) {
            if (!configs.containsKey(name))
                configs.put(name, new Config(name));
    
            return configs.get(name);
        }
    
        public Config saveConfig(String name) {
            return getConfig(name).save();
        }
    
        public Config reloadConfig(String name) {
            return getConfig(name).reload();
        }
    
        public class Config {
    
            private String name;
            private File file;
            private YamlConfiguration config;
    
            public Config(String name) {
                this.name = name;
            }
    
            public Config save() {
                if ((this.config == null) || (this.file == null))
                    return this;
                try {
                    if (config.getConfigurationSection("").getKeys(true).size() != 0)
                        config.save(this.file);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                return this;
            }
    
            public YamlConfiguration get() {
                if (this.config == null)
                    reload();
    
                return this.config;
            }
    
            public Config saveDefaultConfig() {
                file = new File(plugin.getDataFolder(), this.name);
    
                plugin.saveResource(this.name, false);
    
                return this;
            }
    
            public Config reload() {
                if (file == null)
                    this.file = new File(plugin.getDataFolder(), this.name);
    
                this.config = YamlConfiguration.loadConfiguration(file);
    
                Reader defConfigStream;
                try {
                    defConfigStream = new InputStreamReader(plugin.getResource(this.name), "UTF8");
    
                    if (defConfigStream != null) {
                        YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                        this.config.setDefaults(defConfig);
                    }
                } catch (UnsupportedEncodingException | NullPointerException e) {
    
                }
                return this;
            }
    
            public Config copyDefaults(boolean force) {
                get().options().copyDefaults(force);
                return this;
            }
    
            public Config set(String key, Object value) {
                get().set(key, value);
                return this;
            }
    
            public Object get(String key) {
                return get().get(key);
            }
        }
    
    }
    
    Is there a way to delete old config and re-init the new one?
     
  2. Offline

    KarimAKL

    @Benyon The method saveDefaultConfig() doesn't overwrite, you need to delete the old config first. Delete it with this line "new File(getDataFolder(), "config.yml").delete()"
    EDIT: Nvm, i just saw that the saveDefaultConfig() method is your own, just change the "false" in saveResource to "true".
     
    Benyon likes this.
  3. Offline

    Benyon

    I'm an idiot, thank you so much.
     
    KarimAKL likes this.

Share This Page