Adding Missing Lines to Custom Config File

Discussion in 'Spigot Plugin Development' started by MrAxeTv, Apr 29, 2019.

  1. Hello there I have problem with adding missing lines to custom config file
    this is my code which work with no problem
    Code (Text):
    package me.MrAxe.BeastTokens;

    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.configuration.InvalidConfigurationException;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;



    public class YmlCreator{

        private BeastTokens pl;
        List<String> results;
        public YmlCreator(BeastTokens plugin){
            pl = plugin;
            createFiles();
            results = new ArrayList<String>();
        }



        public File configf, dataf, messagesf, mobf, shopf, aliasf,blockf;
        public FileConfiguration config, data, messages, mob, shop,alias,block;



        public FileConfiguration getData() {
            return data;
        }
        public FileConfiguration getAlias() {
            return alias;
        }
        public FileConfiguration getMessages() {
            return messages;

        }
        public FileConfiguration getMob() {
            return mob;
        }
        public FileConfiguration getBlock() {
            return block;
        }
        public void reloadConfig(){
            createFiles();
            results = new ArrayList<String>();
        }





        public void saveConfig(){

        }

        public void createFiles() {
            File dirf = new File(pl.getDataFolder()+"/Shops");
            if (!dirf.exists()){
                dirf.mkdir();
                pl.saveResource("Shops\\Banana.yml", false);
                pl.saveResource("Shops\\Menu.yml", false);    
            }
            if (pl.getDrive().equalsIgnoreCase("File")) {
               
                dataf = new File(pl.getDataFolder(), "data.yml");
                if (!dataf.exists()) {
                    dataf.getParentFile().mkdirs();
                    pl.saveResource("data.yml", false);      
                }
                data = new YamlConfiguration();  
                try {
                    data.load(dataf);
                } catch (IOException | InvalidConfigurationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }



            configf = new File(pl.getDataFolder(), "config.yml");
            aliasf = new File(pl.getDataFolder(), "alias.yml");  
            messagesf = new File(pl.getDataFolder(), "messages.yml");
            mobf = new File(pl.getDataFolder(), "mob.yml");
            blockf = new File(pl.getDataFolder(), "block.yml");

            if (!configf.exists()) {
                configf.getParentFile().mkdirs();
                pl.saveResource("config.yml", false);
            }
            if (!aliasf.exists()) {
                aliasf.getParentFile().mkdirs();
                pl.saveResource("alias.yml", false);
            }
            if (!messagesf.exists()) {
                messagesf.getParentFile().mkdirs();
                pl.saveResource("messages.yml", false);
            }
            if (!mobf.exists()) {
                mobf.getParentFile().mkdirs();
                pl.saveResource("mob.yml", false);
            }
            if (!blockf.exists()) {
                blockf.getParentFile().mkdirs();
                pl.saveResource("block.yml", false);
            }


            config = new YamlConfiguration();
            alias = new YamlConfiguration();
            messages = new YamlConfiguration();
            mob = new YamlConfiguration();
            block = new YamlConfiguration();
            try {
                config.load(configf);
                alias.load(aliasf);
                messages.load(messagesf);
                mob.load(mobf);
                block.load(blockf);
            } catch (Exception e) {
                e.printStackTrace();
            }

       
           
        }
    }
    Problem is when I release new version of plugin and add some new lines to custom config
    If they miss from old version I want to add them from new config version from jar file
    could some one explain me how to do it or send some simple example
     
  2. I actually had this issue and have some code on github that I created for this issue, you may have to modify parts of it if you need to ignore a config section or something. https://github.com/tchristofferson/Config-Updater

    Edit: What it does is it caches the current config and then it checks whatever file you are updating from, usually default config, and it will insert the new lines into your config while maintaining the currently set values.
     
    #2 tchristofferson, Apr 29, 2019
    Last edited: Apr 29, 2019
    • Like Like x 1