Solved saveResource() creating blank file

Discussion in 'Spigot Plugin Development' started by Nicbo, Dec 21, 2019.

  1. So I have a data.yml file in my plugin jar with some data in it but when I call this method:

    Code (Java):
     private void loadFile() {
            if(!dataFile.exists()){
                try{
                    plugin.saveResource("data.yml", false);
                    dataConfig = YamlConfiguration.loadConfiguration(dataFile);
                }
                catch(Exception ex){
                    ex.printStackTrace();
                }
            }
            else{
                //otherwise, set the config to the current file
                dataConfig = YamlConfiguration.loadConfiguration(dataFile);
            }
        }
    It creates a blank data.yml file. Any pointers would be really appreciated. Thanks
     
  2. If it already exists in your plugin folder you will need to remove it every time you wish to make edits to it in your plugin. You shouldn't use YAML files for storing object states.

    To make your life MUCH easier use a JSON file instead for data. JSON data should be loaded on start and saved on disable.
     
  3. I've never really worked with json so I think it would be easier for me to just stick to the yml. Why would I have to delete it everytime? Couldn't I just save the edits I make on it?
     
  4. How are you loading the YAML file? Depending the way, it will either load it from the resource or the plugin's data folder. If it is loading from the plugin data folder than it will never update from your changes.

    I suggest using JSON and save/load objects states. I can PM you two commonly used classes for this. Extremely simple to use.
     
  5. Ok thanks. I'll try it out. If you could pm me it that would be great
     
  6. I will PM you now