[Tutorial] Static access config files (in detail)

Discussion in 'Resources' started by tylersyme, Jan 25, 2014.

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

    tylersyme

    <-- Introduction -->
    I've made many PvP plugins, and the GREATEST organizational hazard of the entire plugin has always been the config file. The problem was that there was no way to access config files statically, or at least that's what I thought (and what I was told). Well it's actually pretty funny how easy it is to create a static access config file, and that's what this quick tutorial will show you.

    <-- The Setup -->

    You should have two classes:
    - Main Class
    - FileManager Class

    Of course you can name these whatever you choose, but this is my preferred method.
    The Main class is whatever class that contains the "onEnable()" method

    <-- The FileManager Class -->

    Now in the FileManager class, you first need to create your config. We will not be using the default bukkit config methods. Instead we'll be creating our config manually.

    To begin, create these two fields in your FileManager class
    Code:java
    1. public static YamlConfiguration config;
    2. private static File configFile;


    So what we've done is create our two config fields. You will need both of these in order to have a working config.

    *Note: For those who don't know, "configFile" is private, meaning you won't be able to access it from other classes, if you want to be able to access it from other classes, simply change "private" to "public"*

    Ok, so we have our two fields, but they aren't initialized yet, well in order to initialize them (give them a value), we will do this with a constructor. There may be other (possibly better ways) of doing this, but I know this works and it's very simple.

    Code:java
    1. static MainClass plugin;
    2. public FileManager(MainClass pluginX) {
    3. plugin = pluginX;
    4.  
    5. configFile = new File(plugin.getDataFolder(), "config.yml"); //The file (you can name it what you want)
    6. config = YamlConfiguration.loadConfiguration(configFile); //Take the file and basically turning it into a .yml file
    7. }


    *Note: Make sure that the "MainClass" is renamed to the class that your "onEnable()" method is located in.*

    So what we're doing is getting an instance of our main class, now, our main class will be extending "JavaPlugin", which is a very important piece. Notice when we are giving our "configFile" field a value, it uses "plugin.getDataFolder()". The data folder represents your plugin's folder, which contains your config files. We need this so that our plugin knows where to put the config file.

    The last thing that we need to do in the "FileManager" class is save our config file. If we don't save it, it won't show up in our plugin's folder. Thankfully, saving is very easy.

    Code:java
    1. try {
    2. config.save(configFile);
    3. } catch (IOException e) {
    4. e.printStackTrace();
    5. }


    Put the code above, inside our constructor BELOW the two config fields.
    Now our FileManager class is complete!

    So is that all? Not quite, we have one thing left to do!

    <-- The Main Class -->

    Go into your MainClass, and put this inside your "onEnable()" method

    Code:java
    1. new FileManager(this);


    And there you go, you are finished. The best part is, that the config file is static, meaning not only can you access it from any class, but there only has to be a single instance of it.

    In order to access the config file, all you have to do is "FileManager.config.DO_STUFF"

    <----------------------- Conclusion ----------------------->

    I hope this tutorial has helped you deal with annoying config files, and if you have any questions, please ask them and I'll get back to you asap. Thanks for reading!
     
    ProMCKingz and Wizehh like this.
  2. Not another one... seems like the new trend on the streets it to create a tutorial about custom yml files.
     
  3. Offline

    tylersyme

    lol, yep
     
  4. Offline

    ProMCKingz

  5. Offline

    xTrollxDudex

    omigod please, you are KILLING me...

    Code:java
    1. public static YamlConfiguration config;
    2. private static File configFile;

    Code:java
    1. static
     
    Monkey_Swag and AdamQpzm like this.
  6. Offline

    Quantum64

    Why would you WANT to access your configuration files statically is my question.
     
    AdamQpzm likes this.
  7. Edit: Just noticed this thread was from January. Shame on you, ProMCKingz! :p

    Trust me, this wasn't a problem.

    Yeah, for good reason.

    Abuse of static is never funny!

    Okay, so net result, here's the biggest tip for all of you people who want to misuse static for easy access:

    Just don't.

    It can cause a whole bunch of issues (issues that this tutorial has not mentioned, let alone provided a solution for), is not very object oriented, and is not what the static keyword is meant to be used for. If you're not sure whether you should use static for something, you probably shouldn't be using it. But here's a little tip which everyone should know:

    Is this thing I want to make static completely irrelevant to an instance?

    If it is irrelevant to an instance, that is exactly what static is used for! This is generally used for utility classes. For example, the Math class in Java is full of a bunch of static methods to do math related stuff (random numbers, min and max methods, sin cos and tan, etc.) - having an instance of Math class wouldn't really make much sense. There's only one Math, and the rules of it aren't going to change, so why would you have a bunch of independent Math objects? You wouldn't.

    This is not the case with configs. Configs are tied to a specific instances of JavaPlugin. A config isn't a global thing that you wouldn't have multiple copies of, it's something you would have a copy of for each plugin. It's tied specifically to an instance, so saying "this is irrelevant to all instances" (e.g. by using static) makes no sense at all.

    Please don't be lazy. Pass an instance of your main class when you want to use methods from it. Don't make them static, or have a static instance of your main class. This is a common mistake for beginners to make, I have done it myself, but believe me when I say it's not the magic option that you think it is.
     
    Europia79, Monkey_Swag and Quantum64 like this.
  8. Offline

    Garris0n

     
  9. Offline

    ProMCKingz

    Garris0n AdamQpzm Quantum64
    The reason you would want this, is because if you are making a plugin for example a GUI, you would need static in order to make configuration sections in the GUI. Hence you cannot use it in other clases, thats why I was here. [fire]
     
  10. ProMCKingz No, that's not right. You can use an instance. I've yet to encounter a situation like this in which static is necessary. Such beliefs are due to a lack of understanding of when and how to use the static keyword.
     
  11. Offline

    Quantum64

    You are incorrect.

    Theoretically you could do anything you currently can in Java without the static keyword (that I can think of off the top of my head), it is there to make the lives of developers easier when used in moderation.

    Instead of making a static variable for configuration, the correct way to access it would be JavaPlugin.getPlugin(YourPlugin.class).getConfig();
     
  12. Offline

    teej107

    We need another Garris0n correction with this one.
     
    ProMCKingz likes this.
  13. Offline

    ProMCKingz

    Quantum64
    Oh, thanks, I am a new developer, thanks for the help. You actually helped, unlike the other comments
     
  14. Offline

    xTrollxDudex

    The point was to help you actually do some research instead of hailing the absurd misuse of the Java language to create despicable code.
     
  15. Offline

    Dragonphase


    What was going through my head before I saw your post:

     
Thread Status:
Not open for further replies.

Share This Page