Configuration Issue

Discussion in 'BungeeCord Plugin Development' started by Christophe6, Jun 22, 2016.

  1. Hello,
    In spigot I should use the addDefault() method... But how should I do it in BungeeCord? I tried to create this:
    Code (Java):
        public void addDefault(String string, int value) {
            if(config.getString(string) == null) {
                getConfig().set(string, value);
            }
        }
    It causes a NullPointerException, what's very normal... But I don't know what to use instead of this.
    I will include my whole SettingsManager:
    Code (Java):
    package com.christophe6.vipjoin;

    import java.io.File;
    import java.io.IOException;

    import net.md_5.bungee.api.ProxyServer;
    import net.md_5.bungee.config.Configuration;
    import net.md_5.bungee.config.ConfigurationProvider;
    import net.md_5.bungee.config.YamlConfiguration;

    public class SettingsManager {
        private SettingsManager() { }
           
        static SettingsManager instance = new SettingsManager();
           
        public static SettingsManager getInstance() {
            return instance;
        }
       
        public  Configuration config;
        public ConfigurationProvider configp = ConfigurationProvider.getProvider(YamlConfiguration.class);
        public File configfile = new File(ProxyServer.getInstance().getPluginsFolder()+ "/config.yml");
       
        public void setup() {
            if(!configfile.exists()) {
                try {
                    configfile.createNewFile();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
       
        public Configuration getConfig() {
            try {
                config = configp.load(configfile);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           
            return config;
        }
       
        public void saveConfig() {
            try {
                configp.save(config, configfile);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
       
        public void addDefault(String string, int value) {
            if(config.getString(string) == null) {
                getConfig().set(string, value);
            }
        }
       
        public void addDefault(String string, String value) {
            if(config.getString(string) == null) {
                getConfig().set(string, value);
            }
        }
    }
    Thank you!
     
  2. Where do you run the setup method?
     
  3. @White_Knight28
    I added it to my onEnable() but it keeps giving this error:
    Code (Java):
    11:10:23 [WARNING] Exception encountered when loading plugin: VipJoin
    java.lang.NullPointerException
        at com.christophe6.vipjoin.SettingsManager.addDefault(SettingsManager.java:57)
        at com.christophe6.vipjoin.MainClass.onEnable(MainClass.java:12)
        at net.md_5.bungee.api.plugin.PluginManager.enablePlugins(PluginManager.java:227)
        at net.md_5.bungee.BungeeCord.start(BungeeCord.java:267)
        at net.md_5.bungee.BungeeCordLauncher.main(BungeeCordLauncher.java:55)
        at net.md_5.bungee.Bootstrap.main(Bootstrap.java:15)
     
     
    #4 Christophe6, Jun 23, 2016
    Last edited: Jun 23, 2016
  4. I think my problem is saving the plugin! The config.yml is saved in the /plugins folder.
    How to fix that?
     
  5. Make one file for the data folder of the plugin and use that in getting the config file
     
  6. Use getConfig on line 57 in place of config