external .yml

Discussion in 'Spigot Plugin Development' started by james.boy123, Feb 5, 2015.

  1. I know how to create and use a config.yml in my Project, but can someone explain me how to make & use an external .yml, for example i want to create a kit.yml where I can change the tools or what else.
  2. To make external YAML files, you can use the built-in FileConfiguration class in Bukkit. Just instantiate the object with the YamlConfiguration#loadConfiguration method. From there, you can access all of your stuff through the get and set methods.

    [Edit]: An example would something like this:
    Code (Java):
    File file = new File(getDataFolder(), "kits.yml");
    FileConfiguration fileConfig = YamlConfiguration.loadConfiguration(file);
    // Get some information...
    boolean someBoolean = fileConfig.getBoolean("some.boolean.path");
    // Set some information...
    fileConfig.set("some.set.path", "some.value");
    • Agree Agree x 2
    • Like Like x 1
  3. And how can I make it that i have a File in my plugins folder? So that if I sell the Plugin to someone that he can change the entrys in my .yml file.
  4. This is how you create the file in your directory if it does not exist:

    Code (Text):

            File dir = this.getDataFolder(); //Your plugin folder
            dir.mkdirs(); //Make sure your plugin folder exists

            File example = new File(this.getDataFolder() + "/example.yml"); //This is your external file
            YamlConfiguration config1 = YamlConfiguration.loadConfiguration(example); //Get the configuration of your external File
            YamlConfiguration config2 = YamlConfiguration.loadConfiguration(this.getResource("example.yml")); //This is optional, so you can predefine your config. You simply add it to your source as same as your plugin.yml
            if(!example.exists()) { //Check if your external file exists
                try {
                    example.createNewFile(); //if not so, create a new one
                    config1.save(example); //save the configuration of config1 or config2 to your new file
                } catch (IOException e) {
                    System.out.println("[PluginName] couldn't create some files!"); //if something goes wrong this is what will be done then
    Additional Stuff:

    This is an example for adding files to your source. You can simply get their configuration by doing it as same as i did it with the "config2".

    Step 1: Right-Click "src"
    Step 2: Hit "new"
    Step 3: Click on "File"
    Step 4: Create your external File :)
    • Like Like x 1
  5. And how can I read out the data of the file?
  6. Code (Text):

            File external = new File(Main.plugin.getDataFolder() + "/external.yml");
            YamlConfiguration cfg= YamlConfiguration.loadConfiguration(external);
            cfg.get("path.to.object"))); //This is how to read
  7. And how do i set data?
    If i write
    config.set("friend", "database");
    Nothing happens, no error but no creating...
  8. D
    Did you create the file first?
  9. Nvm, i forgot to save ;)
    • Funny Funny x 1
    • Agree Agree x 1
  10. It is...It showed him how to make the config and if he pleases, call the method to reload it ;P