Custom File (yml) Help

Discussion in 'Plugin Development' started by xTay_, Sep 3, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    xTay_

    Hey,
    So I've made a class that allows me to easily create custom .yml files. It creates the file fine but I have a small yet annoying issue.

    This is the messages.yml file:
    Code:
    NoPermission: '&f(&cPermission&f) &7You do not have permission to do this.'
    ArgumentsError: '&f(&cArguments&f) &7Usage: '
    ConsoleError: '[ERROR] Only players may use this command.'
    As you can see I have put in some default values. These values are shown fine when the plugin is run, however, if you edit them and then reload/restart the server - they are reverted back to their default value. I'm aware this happens if you are overwriting the file every time you reload but I have tried many different things including checking to see if the file already exists, to checking if the file is empty before putting in the default values yet none of this has seemed to work. Even some other people I know could not solve the issue.

    Here is my method class to creating a file:
    Code:
    public class AbstractFile {
        protected DatabaseMain main;
        private File file;
        protected FileConfiguration config;
       
        @SuppressWarnings("deprecation")
        public AbstractFile(DatabaseMain main, String filename){   
            this.main = main;
            this.file = new File(main.getDataFolder(), filename);
           
           
            //Create File
            if (!file.exists()){
                try{
                    file.createNewFile();
                } catch (IOException e){
                    e.printStackTrace();
                }
            }
            //
           
            config = YamlConfiguration.loadConfiguration(file);
           
            if (config.getKeys(false).isEmpty())
            {
                InputStream readConfig = this.main.getResource(filename);
                config = YamlConfiguration.loadConfiguration(readConfig);
                //config.addDefaults(setDefaults); unused/deleted code
            } //
           
            save();
        }
        //Save
        public void save(){
            try{
                config.save(file);
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        //Get Config
        public FileConfiguration getConfig(){
            return config;
        }
    }
    I would really appreciate any help on this issue. If I have not included something that you need to see, just say so and I'll put it in the post.
    Thanks!
     
  2. Offline

    MasterDoctor

    I'd like to recommend that you add the default values when you create the file and then load the configuration afterwards. That'll save you loading it twice.
    For debugging, I recommend you print debug messages at certain points in your code to see if it's being executed.
     
Thread Status:
Not open for further replies.

Share This Page