[Tutorial]How to create multiple yml files with another class

Discussion in 'Resources' started by Hero9909, May 13, 2014.

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

    Hero9909

    Hi there ive searched so long for somethig like this i hope i can help you with this tutorial.

    In the following ill explain how to create yml files with two classes and save them in the plugins folder

    first we need our main class

    Code:java
    1. package de.Hero9909;
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4. public class MainClass extends JavaPlugin {
    5.  
    6. @Override
    7. public void onEnable(){
    8. getLogger().info("Loading configs");
    9. }
    10.  
    11. }


    What we´ve got here is our main class "MainClass"
    which extends JavaPlugin.
    The Plugin will say in the console "Loading configs"
    so nothing special.

    next we need the class which creates our yml files

    Code:java
    1. package de.Hero9909;
    2.  
    3.  
    4. public class Configuration{
    5.  
    6.  
    7. }

    but ist empty

    first we need the constructor

    Code:java
    1. public [Classname](){
    2.  
    3. }


    replace [classname] with the name of yor class

    so in the class Configuration it would be:

    Code:java
    1. public Configuration(){
    2.  
    3. }


    so next we want so create an language.yml file
    to do this we need the following code from
    wiki.bukkit.org/Configuration_API_Reference#Mirroring the JavaPlugin implementation

    Code:java
    1. File Lanuage =new File("plugins/yourplugin/","Language.yml");
    2. FileConfiguration Lang = YamlConfiguration.loadConfiguration(Language);


    after this you can add the defaults for the yml files
    do not add the defaults beneath the save(file) method the defaults wont get saved.


    but the file won´t get created
    unless we tell the plugin to create them.
    the plugin will create them if we say:

    Code:java
    1. Lang.save(Language);


    WARNING surround this with try and catch cause your plugins will crash if the code can´t be run. (see summary)

    but the plugin didn´t know that there is a class that creates the configs so we need to "link" them
    so we head over to our MainClass and create a variable that acts as the Configuration class
    to construct the variable, because it can´t be anything like int or String, we got our constructor "Configuration()"

    so lets create a new variable:

    Code:java
    1. private Configuration cfg;


    why did we use private because this shouldn´t show up in othe class which uses the MainClass.

    so next we need is to import the Configuation class in our MainClass and say that the variable cfg should run the code from the constructor Configuration()

    Code:java
    1. this.cfg = new Configuration();


    so now the plugin would create an Language.yml in the folder plugins
    if you whant more than one file just copy

    Code:java
    1. File fileLang =new File("plugins/your plugin/","Language.yml");
    2. FileConfiguration Lang = YamlConfiguration.loadConfiguration(fileLang);

    and

    Code:java
    1. Lang.save(Language);


    and place it in the constructor

    PS: keep in mind to import the methods we use

    Summary

    The whole code:

    MainClass:

    Code:java
    1. package de.Hero9909;
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4.  
    5. import de.Hero9909.Configuration;
    6.  
    7. public class MainClass extends JavaPlugin {
    8.  
    9. private Configuration cfg;
    10.  
    11.  
    12.  
    13. @Override
    14. public void onEnable(){
    15. getLogger().info("Loading configs");
    16. this.cfg = new Configuration();
    17.  
    18. }
    19.  
    20.  
    21. }
    22.  


    Configuration class:

    Code:java
    1. package de.Hero9909;
    2.  
    3. import java.io.File;
    4. import java.io.IOException;
    5.  
    6. import org.bukkit.configuration.file.FileConfiguration;
    7. import org.bukkit.configuration.file.YamlConfiguration;
    8.  
    9.  
    10. public class Configuration{
    11.  
    12.  
    13.  
    14.  
    15. public Configuration(){
    16.  
    17. File fileLang =new File("plugins/yourplugin/","Language.yml");
    18. FileConfiguration Lang = YamlConfiguration.loadConfiguration(fileLang);
    19.  
    20. try {
    21. Lang.save(fileLang);
    22. } catch(IOException e) {
    23. // Handle any IO exception here
    24. e.printStackTrace();
    25. }
    26. }
    27.  
    28. }


    Some Questions and the Answers:

    Q: Why did you post this you can althougth create configs with the main class ?!
    A: Shure but to keep it nice and friendly to change something an organisation is nice.

    If you ask me more ill maby place it here...


    Thanks for reading and have fun with this.
    Hero9909
     
  2. Hero9909
    I have a question: Why should I use this, a class that creates a file and nothing else, over the ones that allow me to create multiple files and edit them?

    And in my opinion, 7 lines of code does not make my class look messy or unorganized.
     
  3. Offline

    JoeyDevs

    Lol stop hate my plugin for my server requires + 8 custom config file + for every player a file. I use a method like this and it works fine. And you can also use this in 1 Class ( Main as example) so why you crying here? 'Good Developer' that is hating on lines.. I don't know but most of the time you will a organized,compact code or you like to have a code of 200 line if it can in 75?


    But back to topic:
    Hero9909 Nice tutorial.
     
  4. JoeyDevs
    At what point was I hating? I was simply asking a question and sharing my opinion.
    And what makes you think I'm crying? You're actually the one hating and crying here over someone's opinion.
    Sorry, but I don't understand what you're trying to say here. If you're going to argue with someone, please try to at least use proper grammar and spelling.
     
  5. Offline

    JoeyDevs

    Hero9909 Thanks i have BTW learned from this to do something diffrent the next time! Thanks!
     
  6. Offline

    Iroh

    Removed pointless flame.
     
    Garris0n likes this.
  7. Offline

    Hero9909

    yeah for shure if you use 1 yml file if wont look messy but if you use more than this like mythic drops or so on its more confortable to organize this a litte.
    &&
    JoeyDevs
    thank you for reding this iths my first tutoril like this im happy to help somewon with this
     
Thread Status:
Not open for further replies.

Share This Page