Solved Update config.yml if it doesn't contain new variable

Discussion in 'Plugin Development' started by joelgodofwar, Nov 19, 2019.

Thread Status:
Not open for further replies.
  1. Offline

    joelgodofwar

    I've been looking for a few hours now, and have found no answers to this. I want to check the config.yml for my plugin, that is on the server's plugin/myplugin/config.yml, and if it doesn't contain a variable I've added to the new config.yml, I can copy the new config.yml from the jar to the directory.

    getConfig().getString("sleepmsg1") will return the results from the jar's config.yml, if it is not in the server's version. But i'd like to update the server's config.yml, and allow the server op to change the configs, with the new comments.

    I hope this makes sense, my mind is fried from all the reading i've done on this. I almost decided to switch to XML files.

    Well I found a method to search a file for a string, and then overwrite the old config.yml file with the jar's config.yml
    onEnable (open)

    Code:
            // Check config.yml if it is up to date.
            String oldConfig = getDataFolder() + File.separator + "config.yml";
            boolean needConfigUpdate = false;
            try {
                needConfigUpdate = fileContains(oldConfig, "randomsleepmsgs");
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            if(needConfigUpdate == false){
                saveResource("config.yml", true);
                log("" + Ansi.GREEN + "config.yml has been updated");
            }else{
                //log("" + Ansi.GREEN + "not found");
            }
            // End config.yml check.


    function (open)

    Code:
        @SuppressWarnings("resource")
        public boolean fileContains(String filePath, String searchQuery) throws IOException
        {
            searchQuery = searchQuery.trim();
            BufferedReader br = null;
    
            try
            {
                br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));
                String line;
                while ((line = br.readLine()) != null)
                {
                    if (line.contains(searchQuery))
                    {
                        return true;
                    }
                    else
                    {
                    }
                }
            }
            finally
            {
                try
                {
                    if (br != null)
                        br.close();
                }
                catch (Exception e)
                {
                    System.err.println("Exception while closing bufferedreader " + e.toString());
                }
            }
            return false;
        }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 19, 2019
Thread Status:
Not open for further replies.

Share This Page