YML file creation help?

Discussion in 'Plugin Development' started by Lemoncakecake, Jan 9, 2012.

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

    Lemoncakecake

    Okay, so I feel like a complete idiot right now, but I have been trying for hours, and have not been able to create different files such as config.yml in a /plugins/pluginname folder (I can't make the directory either)

    I have tried all the new tutorials on YAMLS and looking at other plugin sources, but I just can't get it to work!

    If you could post code of a simple config class, or anything like that would be super helpful! Thanks
     
  2. Offline

    DirtyStarfish

    Theres a few different methods that people use. This is how I do it. There are other ways, for example, you can copy the config across from your plugin jar file to the directory.

    You could just copy the code here, and add to the set and get parts to create your config.

    I create a new class called CreateFiles and add this code:
    Code:
    public class CreateFiles {
        ConfigTest plugin;
    
        private static File pluginFolder = new File("plugins", "ConfigTest");
        private static File configFile = new File(pluginFolder, "config.yml");
        public static FileConfiguration config;
    
        public static void firstRun() {
            config = new YamlConfiguration();
    
            if (!pluginFolder.exists()) {
                try {
                    pluginFolder.mkdir();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    
            if (!configFile.exists()) {
                try {
                    configFile.createNewFile();
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
                config.set("Path.to.message", "Config message");
                try {
                    config.save(configFile);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            loadConfig();
        }
    I create 2 Files, one for the folder, and one for the config file. I set the folder to a folder called ConfigTest, as that is the name of this plugin, the ConfigTest folder is inside the plugins folder.
    Then, the File variable configFile is set to the location of the folder, and the file config.yml.

    There is also a FileConfiguration variable called config that is used to save, load, set and get information.
    I set config to a new YamlConfiguration, and then check if the folder exists or not. If it doesn't, I try to create it. I do the same for the configFile, if it doesn't exist, try to create a new file.

    After that, I have to set the different information that will be contained in the config file. In this case, I set a string "Config message" to the path of "Path.to.message".
    Then, I try to save the file to the configFile that was defined before, at the top of the class.

    Next I use another method loadConfig(); This is outside of the if statements.

    Here is the loadConfig method (which is in the same class):
    Code:
        private static void loadConfig() {
            config = new YamlConfiguration();
            try {
                config.load(configFile);
            } catch (Exception e) {
                e.printStackTrace();
            }
            String msg = config.getString("Path.to.message");
            System.out.println(msg);
        }
    Here I set config to a new YamlConfiguration (not sure if I needed to do that), and try to load the configFile, which is still set to the folder and file from the top of the class.
    A String called msg is set to the string that is defined in the configFile at the path "Path.to.message", which was set in the previous method.
    To check it worked, I printed the message to the console.

    The config file looked like this:
    Code:
    Path:
      to:
        message: Config message
    In the onEnable() method in the main class of the plugin, I would write:
    Code:
    CreateFiles.firstRun();
    The downside to this method is that it has a lot of code, when you can use much less by copying the file from the jar. However, I prefer this way, as it also allows me to create files with different names using the same method, such as, creating a file for each player that joins the server.


    Instead of this:
    Code:
    private static File configFile = new File(pluginFolder, "config.yml");
    You could use this:
    Code:
    String playername;
    private static File configFile = new File(pluginFolder, playername + ".yml");
    So a file for each playername will be made. You would put the File variables into the method, and pass the String playername to it.

    Reading over this, it looks a bit complicated, but hopefully it helps...
     
    Lemoncakecake likes this.
  3. Offline

    Lemoncakecake

    Wow! Thank you so much! I'll be sure to try this tomorrow (and in case you are wondering, I am new to Java (as of last week) but I've been doing web design programming for about 3 years now, so I'm somewhat experienced)
     
  4. Offline

    hatstand

    You're making an assumption about where the data folder is located, you should be using the getDataFolder() method.
     
Thread Status:
Not open for further replies.

Share This Page