Custom Config saving help

Discussion in 'Spigot Plugin Development' started by Z3tr0nix, Jun 1, 2017.

  1. custom config not saving :\
    main class
    Code (Java):
        public YamlConfiguration getNameTagConfig(){
            nameConfig = new YamlConfiguration();
            File f = new File(getDataFolder(), "nametags.yml");
            if(!f.exists()){
                try {
                    f.createNewFile();
                    nameConfig.addDefault("default.prefix", "prefix ");
                    nameConfig.addDefault("default.suffix", " suffix");
                    nameConfig.addDefault("default-rank", "default");
                    nameConfig.load(f);
                    nameConfig.options().copyDefaults(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            try {
                nameConfig.save(f);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return nameConfig;
        }
        public void saveNameTagConfig(){
            try {
                nameConfig.save(new File(getDataFolder(), "nametags.yml"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    join class
    Code (Java):
                    if(Main.get().getNameTagConfig().getString(event.getPlayer().getUniqueId() +".Group") == null){
                        Main.get().getNameTagConfig().set(event.getPlayer().getUniqueId() +".Group",
                                Main.get().getNameTagConfig().get("default-rank"));
                        Main.get().saveNameTagConfig();
                    }
    it does not save in the main class or the join class.
     
  2. Load the config after you saved it
    Using " yamlconfig.load(file); "
     
    • Informative Informative x 1
  3. still doesnt work, and by the way then why isnt the save method working?
     
  4. Because You loaded the config before saving
    " nameConfig.load(f); " also put the copy Default to the top and not at the end.
     
  5. i did it. didnt work...
     
  6. You never set nameConfig to a file

    Instead at the top of your code you need to change the order
    Current:
    Code (Java):

    public YamlConfiguration getNameTagConfig(){
            nameConfig = new YamlConfiguration();
         File f = new File(getDataFolder(), "nametags.yml");
           if(!f.exists()){
     
    make it
    Code (Java):

    public YamlConfiguration getNameTagConfig(){
           File f = new File(getDataFolder(), "nametags.yml");
            nameConfig =  YamlConfiguration.loadConfiguration(f);
     
     
  7. ty
     
  8. it now saves defaults, but it does not save from external class
     
  9. I always had that issue too, no idea why, so I just wrote a method in the custom file class
    Code (Java):

    public void set(String path, Object obj){
      config.set(path, object);
      try{
          config.save(file);
      } catch (Exception e) {
          e.printStackTrace();
      }
    }
     
    On phone pls no hate for spacings :(
     
    • Like Like x 1
  10. thanks :D