Question Creating a second configuration file

Discussion in 'Plugin Help/Development/Requests' started by NewDevOnTheBlock, Apr 22, 2015.

Thread Status:
Not open for further replies.
  1. Hello,

    Does anyone have a good video or written tutorial on making a second configuration file for your plugins? I <mostly> understand what to do with the normal config.yml, but now I'm trying to store all of my player data in one file called 'playerData.yml'.

    I read through the Bukkit Configuration API twice, but for some reason I'm just not catching onto it too easily, and what is written there just was not working that well.

    So, does anyone have a good video/written tutorial on how to create a second configuration file?

    Thank you!
     
  2. Offline

    BestMcPlayers

    I'm not the one you want help from but I'll try my best.

    We'll need FileConfiguration and a file.
    Code:
        public FileConfiguration mainConfig;
        public File mainConfigFile;
    Then you want to create the file.
    Code:
            mainConfigFile = new File(getDataFolder(), "config.yml");
    What getDataFolder() does is get your plugin folder. Its name is determined by the name in plugin.yml.

    Once you've create the file, the next step is to make the config a YMAL configuration file.
    Code:
            mainConfig = new YamlConfiguration();
    Now we can load the file.

    Code:
    mainConfig.load(mainConfigFile);
    Note: using this code will overwrite the config.yml, making the one in your .jar file useless. What you need to do is check if it's already made, and if it's made it'll copy the defaults. I like to use this:
    Code:
        public void copy(InputStream in, File file)
        {
            try
            {
                OutputStream out = new FileOutputStream(file);
                byte[] buf = new byte[1024];
                int len;
       
                while((len = in.read(buf)) > 0)
                {
                    out.write(buf, 0 ,len);
                }
       
                out.close();
                in.close();
            }
            catch (Exception e)
            {
            }
        }
    Then this:

    Code:
        private void firstrun() throws Exception
        {
            if(!mainConfigFile.exists())
            {
                mainConfigFile.getParentFile().mkdirs();
                copy(getResource("config.yml"), mainConfigFile);
            }
    }
    What this will do is copy the config.yml in your .jar file if mainConfigFile doesn't exisit.

    That's how you set up your config file and to use it just use your basic java skills.

    Code:
            if (mainConfig.getString("swag") = null)
            {
                log("Please insert a string under swag in the config.yml file.");
            }
    Good luck. This is the best method I know, feel free to correct me people. Also, all of the creating the files should be done or called inside your onEnable.
     
    Last edited: Apr 23, 2015
    NewDevOnTheBlock likes this.
Thread Status:
Not open for further replies.

Share This Page