Solved Using multiple .yml files in a plugin

Discussion in 'Plugin Development' started by BlueNova, Mar 29, 2020.

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

    BlueNova

    Hello! I am relatively new to bukkit and java (1 week in) and the learning process has been going kinda smooth until i wanted to save data into yml files

    I tried looking for videos or articles that tutor this kind of thing but i never found anything helpful..

    what i mean is that i want to save tables containing an amount of money a player has or what pets he has. (config files are ok i found those on YT)

    i am not looking for direct explanation from anyone, instead , i was hoping if someone knows any link that might help me understand how saving plugin and/or player data , i would really appreciate any help i can get :) (although some explanation will never hurt and i would greatly appreciate the help!)

    thanks alot!
     
  2. Offline

    KarimAKL

    @BlueNova I'll try to explain what you need to do.

    Creating a new file:
    Code:Java
    1. // This gets the folder named after your plugin, inside the server's "plugins" folder
    2. File dir = getDataFolder(); // This method is inherited from the JavaPlugin class
    3.  
    4. // This creates a new File object with the path "server/plugins/{YourPlugin}/fileName.yml"
    5. File file = new File(dir, "fileName.yml");
    6.  
    7. // This checks if the plugin's folder exists, otherwise, uses the "make directory" method
    8. if (!dir.exists()) dir.mkdir();
    9.  
    10. // This checks if our new file exists (we don't want to overwrite it), otherwise, creates it
    11. if (!file.exists()) file.createNewFile();


    We now use Bukkit's API for adding YAML inside our new file:
    Code:Java
    1. // We pass our new file into Bukkit's YamlConfiguration, that's what we'll be using to save and load values into the file
    2. FileConfiguration config = YamlConfiguration.loadConfiguration(file);


    From there, you can see view all the methods available from the FileConfiguration class here:
    https://hub.spigotmc.org/javadocs/spigot/org/bukkit/configuration/file/FileConfiguration.html

    Note: In paths, you'll be using the '.' character to access a new section, for example, this:
    Code:Java
    1. config.getString("your.path")

    would return:
    Code:YAML
    1. your:
    2. path: "this value"


    I hope this helped you.
     
  3. Offline

    BlueNova

    @KarimAKL so FileConfiguration is not just for making a config file but also store data? im not even sure if that was what you mentioned, but ill give it a look thanks alot for the help :D
     
Thread Status:
Not open for further replies.

Share This Page