1.16.5 Load Default Config?

Discussion in 'Spigot Plugin Development' started by TaskID, Nov 11, 2020.

  1. Hello,

    I'm currently working on a plugin where I have a config.yml and a messages.yml. My problem is that I want to create my files directly in the plugin src folder (as "default" files), and load and create them if they were not created (to add custom comments, line spacing etc.) in the plugins folder. How would I do that using YamlConfiguration?

    Edit:
    I guess I got it!
    Code (Java):
    plugin.saveResource(String resourcePath, boolean replace);
     
  2. I have answered a similar question yesterday. Click here to get to the post.
     
  3. You wouldn't, Bukkit's YAML handler doesn't support comments that aren't in the header, or custom spacing.
    You'll need to use a third party YAML handler such as snakeyml or create your own file parsing system similar to the method used in a popular block logging plugin which i shall not name.

    A simple way to bypass this would be to writye your data as bytes into a plain text file, then rename the file to .yml format at the end of the save & modify method(s).
     
  4. You can use this

    Code (Java):
        @Override
        public void onEnable(){
            createCustomConfig();
        }

        private void createCustomConfig() {
            customConfigFile = new [URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+file']File[/URL](getDataFolder(), "custom.yml");
            if (!customConfigFile.exists()) {
                customConfigFile.getParentFile().mkdirs();
                saveResource("custom.yml", false);
             }

            customConfig= new YamlConfiguration();
            try {
                customConfig.load(customConfigFile);
            } catch ([URL='http://www.google.com/search?hl=en&q=allinurl%3Adocs.oracle.com+javase+docs+api+ioexception']IOException[/URL] | InvalidConfigurationException e) {
                e.printStackTrace();
            }
        }
     
    So it will load your config if it doesn't exists

    And there's a good wiki page on spigot about configs -> here
     
  5. That's not what the question was about, read the OP.
     
    • Agree Agree x 1