[Tutorial] Custom config files! [EASY!]

Discussion in 'Resources' started by Assult, Sep 2, 2013.

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

    Assult

    This is probably the easiest way to create a custom .yml file

    First, Create 2 variables in the main class that extends JavaPlugin
    Code:java
    1. File newConfig;
    2. FileConfiguartion newConfigz;


    After you have done that, add this in onEnable

    Code:java
    1. newConfig = new File(getDataFolder(), "newconfig.yml"); // set the file location
    2. newConfigz = YamlConfiguration.loadConfiguration(newConfig); // this will give you all the functions such as .getInt, getString ect..
    3.  


    You're almost done, create a new void called saveNewConfig(), for example.
    Code:java
    1. public void saveNewConfig(){
    2.  
    3. }


    Now inside that void, you add
    Code:java
    1. newConfigz.save(newConfig);

    and surround it with try/catch.

    Now call the saveNewConfig() method in your onEnable and you're done!

    Example class:
    Code:java
    1. public class Main extends JavaPlugin {
    2.  
    3. File newConfig;
    4. FileConfiguration newConfigz;
    5.  
    6. public void onEnable(){
    7.  
    8. newConfig = new File(getDataFolder(), "newconfig.yml");
    9. newConfigz = YamlConfiguration.loadConfiguration(newConfig);
    10. saveNewConfig();
    11. }
    12.  
    13. public void saveNewConfig(){
    14. try{
    15. newConfigz.save(newConfig);
    16.  
    17. }catch(Exception e){
    18. e.printStackTrace();
    19. }
    20. }
    21.  
    22.  
    23. public void onDisable(){
    24.  
    25. }
    26.  
    27. }


    Imports used:
    Code:
    import java.io.File;
    import java.io.IOException;
    import java.util.List;
     
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    Also, to do stuff with the config, call newConfigz

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  2. Offline

    Gopaintman

    Which imports do we take?
    Also how do we make the stats.yml file generate from the file in the src?(Such as the config)
     
  3. Offline

    Assult

    I think its just newConfigz.options().copyDefaults(true);
     
  4. Offline

    Assult

    added imports used.
     
  5. Offline

    valon750

    Assult

    What would I do when trying to create a file, inside of a folder, inside of the DataFolder?

    Code:
    String playername = player.getName().toString();
    File name = new File(plugin.getDataFolder() + File.separator + "UserData", playername);
    This is what I'm using currently, however I get hit with a NullPointerException when casting the file. I have got the code set up for when it searches if it exists, if not it creates it, but it never reaches this part, just seems to assume there's nothing there?

    I do in fact have a UserData folder that's created in a separate class file, I assume that as I'm new to dealing with multiple folders/files, I'm simply missing something fairly obvious.
     
  6. Offline

    valon750

    Datdenkikniet

    Alright, so I tried this out in a separate class file, to keep things tidy, but I run in to some issues with "config", and "saveResource".

    It appears that config is never casted in your code, so what am I missing? as changing it to getConfig() doesn't work.

    No idea what to think of saveResource.
     
  7. valon750 I'm sorry, I will add the instructions for seperate classes tomorrow :) thank you for pointing It out
     
  8. Offline

    valon750

    Datdenkikniet

    Tomorrow?!

    But.. but I actually have the motivation to code noooow! Ah well, it'll let me focus on some other aspects of the plugins :) Keep me posted! Thank you!
     
  9. Offline

    TheUpdater

    this tutorial is really good i recoment it for more pp
     
  10. Offline

    Phasesaber

    Great tutorial!
    But....
    You can save the config to a custom folder inside the server folder by doing this:
    Code:java
    1. file= new File("TestFolder/newconfig.yml");
    2. config= YamlConfiguration.loadConfiguration(file);

    This will make a folder in the same directory that the plugins folder & bukkit.jar are in!
     
  11. Offline

    Etched

    Thanks for making such a great tut. I understand this now :)
     
Thread Status:
Not open for further replies.

Share This Page