Reloading a Configuration File successfully?

Discussion in 'Spigot Plugin Development' started by BetaNyan, Feb 6, 2015.

  1. Hello,

    I am having trouble reloading my configuration file. I am using using this to easily change configurable messages and other configuration settings through a command. I am also using a custom configuration API I made, not the Bukkit API.

    The current code is this:

    Code (Text):

    public void save() {
            try {
                config.save(file);
            } catch (IOException e)    {
                e.printStackTrace();
            }
        }
       
        public void reload() {
            YamlConfiguration.loadConfiguration(file);
            save();
        }
     
    I use the save() method when setting paths and creating lists, and it seems to work, but when just calling it alone, save or reload, it actually changes my values back to what I had before.

    Can anyone help?
     
  2. Tux

    Tux

    ...you're using the Bukkit configuration API. Just saying.

    The return value of YamlConfiguration.loadConfiguration() is being ignored.
     
  3. Code (Text):
        public void reload() {
            reloadConfig();
        }
     
    @BetaNyan
     
  4. I meant I'm not using the .getConfig() in JavaPlugin
     
  5. eh.
     
  6. Alright I have this method what I use. It looks complicated but it's fairly easy to understand once you know it.

    I have an enumerator class with all the config objects in it:

    PHP:
    public enum Config {

        PLUGIN_ENABLE(true);

        public Object value;

        Config (Object value) {
            this.value = value;
        }

    }
    Then, I have another class with the static variables that will allow me to get data from the files:

    PHP:
        public static File config;
        public static FileConfiguration configC;
        public static ConfigurationSection configCS;
    And then in the "onEnable" method, I just simply do this to load the config:

    Code (Text):
        public static void initConfig() {
            try {
                B.config = new File("plugins/" + B.getDataFile().getName(),
                        "config.yml");
                if (!B.config.exists()) {
                    B.config.getParentFile().mkdirs();
                    B.config.createNewFile();
                }

                B.configC = new YamlConfiguration();
                B.configCS = B.configC.getConfigurationSection("");
                B.configC.load(B.config);

                for (Config value : Config.values()) {
                    if (B.configCS.get(value.name().replaceAll("_", ".")) == null) {
                        B.configCS.set(value.name().replaceAll("_", "."),
                                value.value);
                        B.configC.save(B.config);
                    }

                    value.value = B.configCS.get(value.name().replaceAll("_", "."));
                }

                B.configC.load(B.config);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    And and and then when I want to say get data from the config, I'll just do this:

    PHP:
            B.configC.get(...)
    Always get the variable with the single C. It sounds complicated buuuuuuut it works and that's all I care about. I shouldn't spoon feed code but I know how hard configs can be...I've always hated them! xD
     
    • Informative Informative x 1
    • Useful Useful x 1