Creating, reading, and writing to files

Discussion in 'Plugin Development' started by bigbeno37, Jul 2, 2012.

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

    bigbeno37

    Hey there!

    I am wondering as to how I would go about creating a config.yml file AND a storage.yml file (To store information) and also how to write to them. Also, I need some clarification as to how to use the 'getters' as so put on the Plugin tutorial wiki: http://wiki.bukkit.org/Introduction_to_the_New_Configuration

    Can someone please help me as the wiki doesn't really make much sense to me and doesn't answer some questions like 'Where do I put the external .yml files?'.
     
  2. You would have to create a default template just like the config.yml and put it in your project, just like the config.yml.
    After that you could create a new class, that's how i did it, and let that class load the other config file from your jar.
    If you need some examples just say, than i will post something for you to have a look at:)

    greetz blackwolf12333
     
  3. Offline

    bigbeno37

    Thanks for that reply. Yes, please put some examples in so that I can understand to a better degree.
     
  4. It is pretty simple, just have a look at this code:
    Code:
    static YourPluginsMainClass plugin;
        public static File configFile;
        public static File customFile;
        public static FileConfiguration config;
        public static FileConfiguration customConfig;
       
        public static ConfigValues values = new ConfigValues();
       
        public GLConfigHandler(YourPluginsMainClass plugin) {
            GLConfigHandler.plugin = plugin;
        }
       
        public static void setupGriefLogConfig() {
            configFile = new File(plugin.getDataFolder(), "config.yml");
            config = new YamlConfiguration();
     
            if(!configFile.exists()) {
                configFile.getParentFile().mkdirs();
                try {
                    configFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                copy(plugin.getResource("config.yml"), configFile);
            }
        }
       
            // this is the function you are looking for
        public static void setupCustomConfig() {
            customFile = new File(plugin.getDataFolder(), "custom.yml");
            customConfig = new YamlConfiguration();
     
            if(!friendsFile.exists()) {
                friendsFile.getParentFile().mkdirs();
                copy(plugin.getResource("custom.yml"), customFile);
            }
        }
    and to save it:
    Code:
    public static void saveFriendsConfig() { //Save configuration file
            try {
                customConfig.save(customFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    That's it:)

    greetz blackwolf12333
     
  5. Offline

    bigbeno37

    Okay, I need you to clarify some things for me:

    1) The above code, should it be this:

    Code:
    static YourPluginsMainClass plugin{
        public static File configFile;
        public static File customFile;
        public static FileConfiguration config;
        public static FileConfiguration customConfig;
     
        public static ConfigValues values = new ConfigValues();
     
        public GLConfigHandler(YourPluginsMainClass plugin) {
            GLConfigHandler.plugin = plugin;
        }
     
        public static void setupGriefLogConfig() {
            configFile = new File(plugin.getDataFolder(), "config.yml");
            config = new YamlConfiguration();
     
            if(!configFile.exists()) {
                configFile.getParentFile().mkdirs();
                try {
                    configFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                copy(plugin.getResource("config.yml"), configFile);
            }
        }
     
            // this is the function you are looking for
        public static void setupCustomConfig() {
            customFile = new File(plugin.getDataFolder(), "custom.yml");
            customConfig = new YamlConfiguration();
     
            if(!friendsFile.exists()) {
                friendsFile.getParentFile().mkdirs();
                copy(plugin.getResource("custom.yml"), customFile);
            }
        }
    }
    (Notice how I added in the brackets)

    Or should it be like this:

    Code:
    static YourPluginsMainClass plugin;
    public static File configFile;
    public static File customFile;
    public static FileConfiguration config;
    public static FileConfiguration customConfig;
     
    public static ConfigValues values = new ConfigValues();
     
    public GLConfigHandler(YourPluginsMainClass plugin) {
        GLConfigHandler.plugin = plugin;
    }
     
    public static void setupGriefLogConfig() {
        configFile = new File(plugin.getDataFolder(), "config.yml");
        config = new YamlConfiguration();
     
        if(!configFile.exists()) {
            configFile.getParentFile().mkdirs();
            try {
                configFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            copy(plugin.getResource("config.yml"), configFile);
        }
    }
     
        // this is the function you are looking for
    public static void setupCustomConfig() {
        customFile = new File(plugin.getDataFolder(), "custom.yml");
        customConfig = new YamlConfiguration();
     
        if(!friendsFile.exists()) {
            friendsFile.getParentFile().mkdirs();
            copy(plugin.getResource("custom.yml"), customFile);
        }
    }
    2) What is GLConfigHandler (LN 9)?

    3) How would I go about writing an ArrayList to my custom .yml file and also reading / loading values from it?

    4) In my config file, I have a section called 'enabled'. If it is set to true, then the plugin should enable. However, if it is set to false or a non-boolean, it disables the plugin. How would I get the boolean after the 'enabled:' part?

    Found out that in order to get that Boolean, I need to find the path (which would be "enabled") via getConfig(). I found this out here.

    Sorry for all these questions, but they need to be answered.

    Thanks,
    bigbeno37
     
  6. Offline

    KeybordPiano459

    Bump - I need to know this too :p
     
  7. Offline

    calebbfmv

    I say it is safe to bump!
     
  8. Offline

    skore87

    2) He copied code from an existing file and didn't change everything. That was intended to be the constructor for that class file.

    3) It would depend what the contents are of that ArrayList.
     
Thread Status:
Not open for further replies.

Share This Page