Solved List in the config is getting deleted after reload

Discussion in 'Spigot Plugin Development' started by astaspasta, May 22, 2017.

  1. Well.... like the title says.. the list in the config its getting deleted every time the server restarts or the plugin reloads... and i want to save the players that are in a specific arraylist join the list again after the plugin reloads..
    soo yeah...

    the code to register the player in the config is
    Code (Text):
              for(String stone : Pets.stone) {
                  String stonelist = "Players.Stone";
                  if (players.contains(Bukkit.getPlayer(stone).getUniqueId())) {
               
                  } else {
                      players.add(Bukkit.getPlayer(stone).getUniqueId());
                      getConfig().set(stonelist, players);
                      saveConfig();
                  }
         }

    and it adds the players that are in the ArrayList in the config.yml like this...

    Code (Text):
    Players:
      Stone:
      - !!java.util.UUID '5509d005-a41e-35dc-bc08-cf4ace8a3527'
     
    and that is what i want... but when the plugin is reloaded it just makes everything in the config like the default....

    Code (Text):
    Players:
      Stone: []
     
    any help?

    Thank you.
     
  2. Lets see the code in onEnable, or where ever you are setting the defaults.
     
  3. Are you creating an instance of the FileConfiguration object inside of your ConfigurationManager or whatever your config class is? If so you might be saving the wrong instance of the configuration!

    My recommendation would be to do a System.out.println() on JavaPlugin.getConfig() (your main class) after you do config.set.

    I'm thinking you have the default config the plugin creates, and your own instance of the config, and that's the problem.

    If you have a FileConfiguration field anywhere this is likely the problem.
    Either that or you're calling super.onDisable() before your save function!
     
  4. @BillyGalbreath and @Redmancometh ...
    here... in onEnable i set players that are in an ArrayList at the config...

    Code (Text):
        @SuppressWarnings("deprecation")
        public void onEnable() {
           
            this.getConfig();
            registerConfig();
           
              String stonelist = "Players.Stone";
              getConfig().addDefault(stonelist, players);
              getConfig().options().copyDefaults(true);
              saveConfig();
             
             

             


             

            getCommand("pp").setExecutor(new PPMain());
           
     

        pets = new Pets(this);
        race = new Race(this);
       
       
        Bukkit.getServer().getPluginManager().registerEvents(this, this);
       
       
        Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
           
        public void run()
          {
       

              for(String stone : Pets.stone) {
                  if (players.contains(Bukkit.getPlayer(stone).getUniqueId())) {
             
                        String allinthestonelist = getConfig().getString("Players.Stone");
                        Pets.stone.add(allinthestonelist);
                     
                  } else {

                     
                      players.add(Bukkit.getPlayer(stone).getUniqueId());
                      getConfig().set(stonelist, players);
                      saveConfig();
                  }
                 

         }

             

           
          }
         
       
       
        }, 0, 0);
       
       
           
       
      }
       
        public void onDisable() {

              saveConfig();
           
        }
        private void registerConfig() {
            getConfig().options().copyDefaults(true);
            saveConfig();
           
        }

    the whole onEnable() and onDisable()
    the config is being saved... until plugin's reload
     
    #4 astaspasta, May 22, 2017
    Last edited: May 22, 2017
  5. dont saveConfig() after copyDefaults()
     
  6. I'm pretty sure it's because you're copying the defaults on initialize without a check. I usually use my own ConfigManager, and avoid the bukkit methods, but I'm pretty sure you're supposed to check if the config exists first.
    Try temporarily removing the calls to copyDefaults and save onEnable, and if it works you know you just need a conditional.
     
  7. @Redmancometh
    You mean like this?
    Code (Text):
            File configFile = new File(this.getDataFolder(), "config.yml");
           
            if (configFile.exists()) {
               
            } else {
                this.getConfig();
                registerConfig();
            }
    still deletes the config...
     
  8. You could save this headache by simply not using copyDefaults at all. I actually hate that method.

    Instead, create a default config.yml file and put it where you have your plugin.yml and include it in the jar when you compile the plugin. This default config.yml file will contain your default configuration options and values.

    Then, the first line in your onEnable method should be
    Code (Text):
    saveDefaultConfig();
    and forget about all that copyDefault stuff. Remove all the copyDefault stuff.

    The saveDefaultConfig() does its own checks to make sure it doesnt overwrite any existing changes. It will only copy the config.yml from your jar to the disk if it doesnt already exist on the disk.
     
    • Like Like x 1
    • Agree Agree x 1
  9. Do you populate or initialize the list after you load in the plugin from the config? If not, then the list would be a empty list that your setting and saving.
     
  10. @SystemUpdate_ @BillyGalbreath @Redmancometh


    .... idk how but i managed to do it... (i mean add them in the config and keep them after reload without losing it) don't ask me how...now the problem is... how to add all of them in an ArrayList.. if could any of you help me...
     
  11. Show us your code?
     
  12. here @SystemUpdate_

    Code (Text):
        @SuppressWarnings("deprecation")
        public void onEnable() {

              String stonelist = "Players.Stone";

            getCommand("pp").setExecutor(new PPMain());
       
        pets = new Pets(this);
        race = new Race(this);
       
       
        Bukkit.getServer().getPluginManager().registerEvents(this, this);
       
       
        Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
           
        public void run()
          {
       

              for(String stone : Pets.stone) {
                  if (players.contains(Bukkit.getPlayer(stone).getUniqueId())) {
             
                        String allinthestonelist = getConfig().getString("Players.Stone");
                        Pets.stone.add(allinthestonelist);
                     
                  } else {

                     
                      players.add(Bukkit.getPlayer(stone).getUniqueId());
                      getConfig().set(stonelist, players);
                      saveConfig();
                  }
                 

         }

             

           
          }
         
       
       
        }, 0, 0);
     
      }
       
        public void onDisable() {

              saveConfig();
           
        }