How to edit config file in not main class?

Discussion in 'Plugin Development' started by Chlorek, Oct 23, 2011.

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

    Chlorek

    Same as topic. I am trying to access config file from another class (not main) and then I am getting errrors. So how to?
     
  2. Offline

    thehutch

    Post your code so we can see what your doing wrong
     
  3. Offline

    HexPang

    i used this in my main class.
    Code:
        public static Configuration getConfig(){
            return CONFIG;
        }
    in other class just
     
  4. Offline

    Windwaker

    I actually am having trouble with this as well. It seems like you can't make a static reference to FileConfiguration :/
     
    pyraetos likes this.
  5. Offline

    Chlorek

    Code:
    Code:
    public void onBlockBreak(BlockBreakEvent event)
        {
            String nickname = event.getPlayer().getName();
            final FileConfiguration config = this.getConfig();
    
            int blockc = config.getInt("players."+nickname+".blocks");
            blockc++;
            config.set("players."+nickname+".blocks", blockc);
            this.saveConfig();
        }
     
  6. Offline

    Sagacious_Zed Bukkit Docs

    Don't do that, JavaPlugin already has a getConfig

    And since the original config variable in JavaPlugin is not static, DO NOT make it static.
    If you don't know what static means, don't use it. You are more likely to run into random problems.
     
  7. Offline

    HexPang

    Code:
    protected static Configuration CONFIG;
    this code under clas statement.

    thanks for remind!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  8. Offline

    Chlorek

    So it's nearly to work but there is error:
    "The method getConfig() is undefined for the type XGTrapBlockListener XGTrapBlockListener.java /XGTrap/src/me/chlorek/xgtrap line 22 Java Problem"
     
  9. Offline

    thehutch

    post your code for us
     
  10. Offline

    Chlorek

    Hmmm I edited my code so much, I am going to check is it working then I'll tell results.
     
  11. Offline

    coldandtired

    Make the config a public variable of your main class. When you create your Listener class, change the constructor so it gets passed your main class.

    In the Listener get the main class from the constructor and access the config that way.
     
  12. Offline

    Sagacious_Zed Bukkit Docs

    Or instead of creating a variable, just call getConfig() on your main plugin.
     
  13. Offline

    thehutch

    Simplest thing to do would just create the config in the mainclass simply follow this guide here:
    Code:
        // decleare this in your mainclass at the top
        FileConfiguration config;
    
        // call this method in your onEnable()
        public void setupConfig() {
    
            config = this.getConfig();
    
            // fill in these addDefaults with your config values
            config.addDefault("String value here" , "Default value");
            config.addDefault();
            config.addDefault();
            config.addDefault();
            config.addDefault();
    
            config.options().copyDefaults(true);
            saveConfig();
        }
    Made it myself :)
     
  14. Offline

    coldandtired

    That's actually identical to how I did it, except I didn't bother copying the defaults.
     
    thehutch likes this.
  15. Update your API and CB if you still have problems
     
Thread Status:
Not open for further replies.

Share This Page