Custom File (yml) Help

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

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


    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:
    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:
    public class AbstractFile {
        protected DatabaseMain main;
        private File file;
        protected FileConfiguration config;
        public AbstractFile(DatabaseMain main, String filename){   
            this.main = main;
            this.file = new File(main.getDataFolder(), filename);
            //Create File
            if (!file.exists()){
                } catch (IOException e){
            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
            } //
        public void save(){
            }catch (IOException e){
        //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.
  2. Offline


    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