Config not saving

Discussion in 'Spigot Plugin Development' started by TheGamerPlayz, May 19, 2016.

  1. I made a custom config, but when i try saving to it using the method in it it does not. Anyone know why?

    Manager
    Code (Java):

    public class EcoManager{
        private Main pl;
        public EcoManager(Main pl){
            this.pl = pl;
        }
        File file;
        FileConfiguration fc;
        String FileName = "Economy.yml";
       
        public File getFile(){
            if(file == null)
                file = new File(pl.getDataFolder(), FileName);
            return file;
        }
       
        public FileConfiguration getEco(){
            fc = YamlConfiguration.loadConfiguration(getFile());
            return fc;
        }
       
        public void saveEco(){
            try{
                getEco().save(getFile());
            } catch(IOException e){
                e.printStackTrace();
                System.out.println("[Economy] Attempting to fix rrror...");
                createEco();
                saveEco();
            }
        }
        public void createEco(){
            if(!getFile().exists()){
                if(!pl.getDataFolder().exists()){
                    pl.getDataFolder().mkdirs();
                }
                pl.saveResource(FileName, false);
            }
        }
    }
     
     
  2. Every time that you call the getEco() method, you reassign fc to what the file reads. Don't reassign the variable, you're blocking the changes before you can save them.
     
  3. But all that is doing is getting the same file everytime
     
  4. Configs are loaded into memory as FileConfiguration objects. Once it's loaded you can manipulate this data held in the object. Its just a fancy Map<?, ?> object in memory. When you want to save it, then save it. Don't reload the contents from disk again before you save... This just wipes out all the changes you made in memory and restores the original values...
     
    • Like Like x 1
    • Agree Agree x 1
  5. Oh I get it now thanks.