Custom Config Bug

Discussion in 'Spigot Plugin Development' started by DenOwq, May 24, 2015.

  1. Hi! I have little problem with my Custom Config file, if i set some path with object, plugin register that and NOT write intro to file.
    My code:
    Code (Text):
    public class Config {

        private File file;
        private File configFile;
        private YamlConfiguration config;
        private boolean loaded;

        public Config(String name) {
        file = new File(Core.getPlugin().getDataFolder()
            .getAbsolutePath());
        configFile = new File(Core.getPlugin().getDataFolder()
            .getAbsolutePath()
            + "/" + name + ".yml");

        if (!file.exists()) {
            file.mkdir();
        }

        if (!configFile.exists()) {
            try {
            configFile.createNewFile();
            } catch (IOException e) {
            e.printStackTrace();
            }
        }

        config = YamlConfiguration.loadConfiguration(configFile);
        loaded = true;
        }

        public boolean isLoaded() {
        return loaded;
        }

        public void save() {
        try {
            config.save(configFile);
        } catch (IOException e) {
            Core.print("error on file Config.java, Line 48");
        }
        }

        public Config add(String path, Object value) {
        try {
            config.set(path, value);
            save();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return this;
        }

        public YamlConfiguration getConfig() {
        return config;
        }

        public Object get(String path) {
        return config.get(path);
        }
    }
    Thanks, DenOwq.
     
    #1 DenOwq, May 24, 2015
    Last edited: May 24, 2015
  2. Hi DenOwq!

    You've forgotten the difference between an object stored in memory at runtime and its actual presence read and written by IO streams. You'll need to call the save() method you've made, as while you seem to have created it it doesn't look like you ever actually run it.
     
  3. Thanks for you fast answer.
    I run my own method save(), in this method is already config method to save.

    Code (Text):
     save();


    public void save() {
        try {
            config.save(configFile);
        } catch (IOException e) {
            Core.print("error on file Config.java, Line 48");
        }
        }
     
  4. I dont know how i repair that! It's working :)