Need help with custom config files.

Discussion in 'Plugin Development' started by V1R4L_CoOKie, Aug 10, 2017.

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

    V1R4L_CoOKie

    I'm somewhat a beginner to bukkit plugins, and i'm having a very hard time understanding how to setup config files. Here is my code so far:

    Code:
    package me.V1R4L_CoOKie;
    
    import java.io.File;
    import java.io.IOException;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import net.minecraft.server.v1_11_R1.PlayerConnection;
    
    public class Bank extends JavaPlugin implements Listener {
       
        private File file = null;
        private FileConfiguration config;
        
        @Override
        public void onEnable()
        {
            if (!getDataFolder().exists())
            {
                getDataFolder().mkdirs();
            }
           
            file = new File(getDataFolder(), "PlayerEquity.yml");
           
            if (!file.exists())
            {
                try {file.createNewFile();} catch (IOException e) {e.printStackTrace();}
            }
           
            config = YamlConfiguration.loadConfiguration(file);
        }
       
        public void save()
        {
            try {config.save(file);} catch (IOException e) {e.printStackTrace();}
        }
    }
    I don't know why it's not working, and I don't even understand what most of my variables and stuff are. I read/watched several tutorials, and this is what i created. If anyone knows of a tutorial that's actually resonably understandable, or thinks they can explain this to me, thank you.
     
  2. Offline

    Zombie_Striker

    @V1R4L_CoOKie
    First, one of the reasons why it is hard to understand is because of the format you used. Brackets should be on the same line as their statements and methods, and each line should be on its own line.

    Lets break down what each step does:
    First, these lines are called. All these do is check if the plugin folder for your plugin has been created. If not, this will make that directory.

    Next, it tries to get the "PlayerEquity.yml" file in that folder.

    After that it checks if that file exists. If it does not exist, it will try to create the config file. If it can't, it will print out the error.

    Finally, this will attempt to load the file into bukkit. config will now be equal to that config instance. At this point, you can treat this "config" variable as though it is "getConfig()", so if you wanted to get a String from the config with path "path", you would use
    Code:
    code.getString("path");
    Now, you will most likely also want to save variables to the config. What this method does is try to save the config to the file. This works the same as saveConfig(), but this is saving your custom file. After you made your changes to the config, you should call this method.
     
  3. Offline

    V1R4L_CoOKie

    @Zombie_Striker

    Alright thank you that does help. I've found that creating the file with all this wasn't the problem with what i was doing, but the error i found is googlable, and im on the right track. Thank you soo much i was so lost with this part of this plugin, it just wasn't coming to me. <3

    also i'll try and put the braces as you said when im posting code, but just my own preference i like it how i do it. Also the try/catch's, i put it like that so it didn't take up much space when all it's doing is creating/saving.
     
  4. Offline

    RaxiCax

    @V1R4L_CoOKie

    What I usually do when I need a custom config file is create a class fx. LangConfig.java. Within the class, a static variable called INSTANCE of YamlConfiguration and four static functions will be added. The names of the functions: setup, build, load and save. Save is sometimes not required and occasionally reload function is added aswell. You will then call setup() inside onEnabled(). the rest can be used whenever needed.


    The content:
    Code:
    public class LangConfig {
        public static YamlConfiguration INSTANCE; // or CONF - it happens...
    
        // File file = new File(getDataFolder(), "language.yml");
        // I place file instances in the JavaPlugin class as static.
    
        static {
            INSTANCE = new YamlConfiguration();
        }
    
        public static void setup() {
            if(!file.exists()) {
                build();
            }
    
            load();
        }
    
        private static void build() {
            // default values goes here
            INSTANCE.options().copyHeader(true);
            INSTANCE.options().copyDefaults(true);
    
            INSTANCE.options().header("");
    
            // When creating paths, I use a enum to maintain it better.
            INSTANCE.addDefault(path, value);
    
            save();
        }
    
        public static void load() {
            INSTANCE = YamlConfiguration.loadConfiguration(file);
        }
    
        public static void save() {
            try {
                INSTANCE.save(file);
            } catch(IOException ex) {
                ex.printStackTrace();
            }
        }
    }
     
  5. Offline

    A5H73Y

    None of this needs to be static, if anything you're abusing static. What you could do is make this class reusable for multiple different types of config, rather than a hardcoded "language.yml".
     
  6. Offline

    RaxiCax

    Yet, configuration differs from each other. I will still need to override the build function, since it will be different from another configuration file. I'm just using this method, because it gives a better overview over the config files. When I know there will only be one of them, why make it an object/instance. I know singletons exist, but this more manageable.
     
Thread Status:
Not open for further replies.

Share This Page