Best way to deal with config files

Discussion in 'Plugin Development' started by unrealdesign, Jun 13, 2014.

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

    unrealdesign

    I've been looking at source codes and reading around a bit and I still can't figure out the best way to deal with file configs. I don't use any special wrappers or something for FileConfiguration, but I'm just looking for the best way to deal with each file.

    Problem?
    One way or another, it seems like I have to pass an instance of the main class to any class that wants to have access to the files. Is there a way around this? I have a class designated to deal with all the config files, but to load all the config files I need an instance of the main class in order to get the files.
    EX:
    Code:java
    1.  
    2. private File settingsFile = new File(plugin.getDataFolder(), "Settings.yml");
    3. private FileConfiguration settings = new YamlConfiguration();
    4.  


    So basically I just want to know the best way to deal with this so I don't have to pass an instance of my mainclass to every freaking class that needs access to a config file.

    Thanks!
     
  2. Offline

    jthort

    Why do you need to pass the instance of the main class into every other class when you have a separate class for the config? You should just create a method inside of the config class such as addValue() which will require a string or some type of data type that you want to add. Then just use the configs instance of the main class rather than having a instance of the main class for each individual class.

    To get the main class instance into the config class you can simply create a new instance of the config class, so for example it would look something like this in the main class to set up the config class
    Code:
    OnEnable(){
      ConfigHandler handler = new ConfigHandler(this);
    }
    Hopefully I answered your question correctly, best of luck
     
  3. Offline

    unrealdesign

    That's the exact same thing as passing the MainClass through to get the config. You would still need to give whatever class access to and instance of itself if you wanted to access ConfigHandler at all.
    I found a simple solution to my own problem:
    For the ConfigManager you would just do:
    Code:java
    1.  
    2. private ConfigManager() { }
    3. public ConfigManager instance = new ConfigManager();
    4. public ConfigManager getInstance()
    5. {
    6. return instance;
    7. }
    8. public void setup(MainClass plugin) { .... }
    9.  


    Then in your MainClass you would do
    Code:java
    1.  
    2. ConfigManager.getInstance().setup(this);
    3.  


    After this, you can use
    Code:java
    1.  
    2. ConfigManager.getInstance()
    3.  

    and you can have a single static instance for that class so all none static methods will return everything just fine as long as you did everything in the 'setup(MainClass plugin)' method.
     
  4. Offline

    jthort

    unrealdesign That's what I was trying to explain to you, but anyway

    Looks good, best of luck with your plugin development
     
  5. Offline

    unrealdesign

    Alright thanks, you too :)
     
  6. Offline

    CMG

    PogoStick29 likes this.
  7. Offline

    unrealdesign

    Yeah, that's pretty much exactly what I and jthort were talking about
     
    jthort likes this.
Thread Status:
Not open for further replies.

Share This Page