Custom YML File

Discussion in 'Plugin Development' started by thefiscster510, May 1, 2012.

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

    thefiscster510

    Hey, I was wondering how you would make a custom YML file. Like besides config.yml, For example players.yml. I still want to be able to do things like getConfig().getString("variable") but i need it to be a separate file. Also, on a side note, How do i Write information to a config? All I know how to do is read from it. One last thing, If you could put as much code in your responses as possible it would be awesome. I learn much better from code snippets then i do from words. Thanks in advance!
     
  2. Offline

    CorrieKay

    this is actually fairly simple, if you are comfortable at all with java IO.

    there are two parts to custom config files, getting, and setting.

    Getting:

    First off, you want to load the config into a File object
    File file = new File(plugin.getDataFolder()+File.separator+"filename.yml");

    Once youve done this, a common practice is to check if it exists, there is a method in a file object for this.

    After youve gotten your config in the file, you load it:
    FileConfiguration config = YamlConfiguration.loadConfiguration(file);


    and thats pretty much all there is to it. Note that "loadConfiguration" and "separator" are both static methods/variables respectively.

    Setting:

    This ones a hair trickier, since you have to get the right file, but basically, the same thing.
    File file = new File(fileLocation);

    then you save it, using the method in FileConfiguration objects:
    config.save(file);

    And there you have it!
     
    thefiscster510 likes this.
  3. Offline

    thefiscster510

    I now understand how to create custom configs, But i don't really get the editing of a config file...

    For example, if i have a config file with just

    Name: bob

    how would i change the name to "bill" in code?
     
  4. Offline

    CorrieKay

    well, you would use "config" like a config object.

    use it just like you use getConfig() in your main plugin, since thats a method that returns a FileConfiguration :3
     
  5. Offline

    thefiscster510

    I will try that now :) Get back to you with what i get =3

    CorrieKay, Whenever I run this code
    Code:
    plugin.getDataFolder()+File.separator+"config.yml"
    i get a JavaLangNullPointerException..

    However, If i use
    Code:
    "plugins"+File.separator+"RegionBuy"+File.separator+"config.yml"
    it works.. Is that ok? Or should i not use that.. Because it's unsafe code..

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  6. Offline

    Sagacious_Zed Bukkit Docs

  7. Offline

    thefiscster510

  8. Offline

    Sagacious_Zed Bukkit Docs

    Any place that causes that piece of code to be executed before the plugin is initialized.
     
  9. Offline

    thefiscster510

    Alright, Then I should be good. Thanks! And thanks again CorrieKay You're the best!
     
  10. Offline

    Sagacious_Zed Bukkit Docs

    thefiscster510
    also hardcoding the string "plugin" is bad because plugins need not reside there.
     
  11. Offline

    CorrieKay

    "plugin" needs to be the instance of your plugin object.

    To make it easier to access your plugin instance, onEnable, set a static variable of itsself as the instance of your plugin, then you can just access it via the public static variable, or a getter method.
     
  12. Offline

    thefiscster510

    That i already knew. :) Do it with all my plugins. Just makes stuff easier.
     
  13. Offline

    daniel.marbles

    How do you create an instance of the plugin object?
     
  14. Offline

    CorrieKay

    You dont. Your main class file that extends JavaPlugin, bukkit creates an instance of it. you can pass in the plugin in a parameter with the "this" keyword.
     
  15. Offline

    MDCreator

    Very close, my dear sir.
    File actually provides a constructor where you can do this:
    Code:
    File filenameFile = new File(getDataFolder(), "filename.yml");
    
     
  16. Offline

    daniel.marbles

    Ah, thank you :)
     
  17. Offline

    CorrieKay

    ftfy :D

    Also, its not required to do that, mostly preference, as i dont think theres any difference. Could be wrong though, but also, i wrote that 3 1/2 months back :p
     
Thread Status:
Not open for further replies.

Share This Page