Solved Using getConfig() multiple times vs creating config variable?

Discussion in 'Spigot Plugin Development' started by Diamond_King099, May 25, 2015.

  1. I'm just getting started with configs, and I'd like to know if it's better to use getConfig().getWhatever() each time I need to get a config value, or should I create a FileConfiguration variable?

    It would seem better to create a variable, but I looked at the source code of several plugins and they all use getConfig().getWhatever(). Also, if I was to use a config variable, I would have to reset it every time I ran the plugin's reload command.
     
  2. Both options are the same. getConfig() points directly to the object. I would prefer using getConfig() because you got one variable less in your plugin then. For Spigot/Bukkit it doesn't matter which way you use.
     
  3. Correct me if I'm wrong, but I don't think this is true. A variable is far more efficient because it reduces the number of instances of the config to just one - running getConfig() every time creates a new copy of the config each time, and can therefore reduce performance.
     
  4. From the Bukkit source:

    public FileConfiguration getConfig()
    {
    if (this.newConfig == null) {
    reloadConfig();
    }
    return this.newConfig;
    }

    Believe me, it doesnt create a new instance every time ;)
     
  5. Thanks :p
     
  6. Thank you, @Friwi! You have answered my question.
     
  7. The most simple method for him would actually be:

    Enable:
    (never overwrites itself once copied)
    Code (Text):
    public void onEnable()
    {
         saveDefaultConfig();
    }
    and every time he/she needs to call upon it they can just use:

    Code (Text):
    this.getConfig().get<type>()
     
  8. I use
    Code (Text):
    If (new File.getString().exists()) {

    reloadConfig();
    } else {
    // Set config stuff here...
    }