Solved Remove from config

Discussion in 'Spigot Plugin Development' started by Goofan, Jun 17, 2016.

  1. Alright so I know the basics of how to remove from a config I just can't seem to get it to work.

    Data.yml
    Code (Text):
    world:
      Capitals:
        '0':
          Name: Erion
          Location:
            X: 394.6741030606424
            Y: 97.44195321783684
            Z: -501.7518882013182
            World: world
          Spawn:
            X: 394.6741030606424
            Y: 97.44195321783684
            Z: -501.7518882013182
            World: world
        '1':
          Name: Orion
          Location:
            X: 394.6741030606424
            Y: 97.44195321783684
            Z: -501.7518882013182
            World: world
          Spawn:
            X: 394.6741030606424
            Y: 97.44195321783684
            Z: -501.7518882013182
            World: world
     

    ConfigManager.java
    Code (Text):
    //AutoReload taskID
        private File file;
        private String defaults;
        private static Integer aLoadID = null;
     
        /**
         * Creates new Config File, without defaults
         * @param path - String
         * @param fileName - String
         */
        public ConfigManager(String path, String fileName) {
            this(path, fileName, null);
        }
     
        /**
         * Creates new Config File, with defaults
         * @param path - String
         * @param fileName - String
         * @param defaultsName - String
         */
        public ConfigManager(String path, String fileName, String defaultsName) {
            defaults = defaultsName;
         
            String pathway = (path == null) ? Main.getInstance().getDataFolder() + File.separator + fileName : Main.getInstance().getDataFolder() + File.separator + path + File.separator + fileName;
            file = new File(pathway);
            reload();
        }
     
        /**
         * Reload configuration
         * return void
         */
        public void reload() {
            if (!file.exists()) {
                try {
                    file.getParentFile().mkdirs();
                    file.createNewFile();
                }catch (IOException exception){
                    exception.printStackTrace();
                    Main.getInstance().getLogger().severe("Error while creating file " + file.getName());
                }
            }
         
            try {
                load(file);
                if (defaults != null){
                    InputStreamReader reader = new InputStreamReader(Main.getInstance().getResource(defaults));
                    FileConfiguration defaultsConfig = YamlConfiguration.loadConfiguration(reader);    
                 
                    setDefaults(defaultsConfig);
                    options().copyDefaults(true);
                 
                    reader.close();
                    save();
                }
            }catch (IOException exception){
                exception.printStackTrace();
                Main.getInstance().getLogger().severe("Error while loading file " + file.getName());
             
            }catch (InvalidConfigurationException exception){
                exception.printStackTrace();
                Main.getInstance().getLogger().severe("Error while loading file " + file.getName());
            }
        }
     
        /**
         * Save configuration
         * @return void
         */
        public void save() {
            try {
                options().indent(2);
                save(file);
            }catch (IOException exception){
                exception.printStackTrace();
                Main.getInstance().getLogger().severe("Error while saving file " + file.getName());
            }
        }
    }

    Remove.java
    Code (Text):
    data = new ConfigManager("Data", "Data.yml", null);

    data.set(world.getName() + ".Capitals." + cr.getID().toString(), null);
    data.reload();
    System.out.println(world.getName() + ".Capitals." + cr.getID().toString());
    puts out this:
    world.Capitals.0

    I run the code nothing :/

    Been at it for a few days (managed to work around it whiel working on other features) but now I need it to work

    Thanks in Advance,
    Thomas
     
  2. Of course it prints that out, you're not telling it to print out the what the config contains under that key, you're just printing the key...

    Also... I don't see any setters or getters for config values in your ConfigManager, care to share what it's extending / implementing?
     
  3. public class ConfigManager extends YamlConfiguration {

    That bit of code was just to say that I know the path of the key is correct (show the path to me) towards the config and the key in there.
    And that is correct so I know it should work (normally) but can't for the love of me figure out why it's not.

    Regards,
    Thomas
     
  4. Code (Text):

    data.set(world.getName() + ".Capitals." + cr.getID().toString(), null);
    data.reload();
     
    Here's that that code is doing:

    1) Change config value in memory by setting to null.

    2) Reload the config from disk.

    When you reload you are overwriting the changes you made in memory to the disk.

    The config.yml file is loaded into memory when the plugin loads. When you make edits to it you are editing whats in the memory, not on disk. If you want to save your changes to disk you must save the config file. You do not need to reload after this step because the memory will match what is on the disk since you just saved it..

    ;)
     
    • Useful Useful x 1
  5. Haha thanks I just figured this one out on my own aswell :D

    Solution: Use data.save(); instead of data.reload();

    Just in time for me to put this as solved thanks tho Billly!