Initial file creation overwriting defaults

Discussion in 'Plugin Development' started by JellyRekt, May 15, 2019.

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


    I poked around in Bukkit docs until I fixed the issue by including plugin.saveResource(String resourcepath, boolean replace) in my onEnable to copy defaults from the resource file.
        private void loadFiles() throws IOException {
            saveResource("resource-with-defaults.yml", false);  // This line here fixed the issue
            resource = new Resource(this, "resource-with-defaults");
            resource2 = new Resource2(this, "resource2");
            resource3 = new Resource3(this, "resource3");
    The string argument tells you which file to copy defaults, and the boolean tells you whether you should override an existing file.

    Original question
    I am attempting to write a YamlConfiguration wrapper for my plugin. The subclasses each use a constructor that matches their superconstructor exactly. When the plugin enables, it loads the three necessary yml files in the expected directory (PluginName/data).

    All three files are loading completely blank. However, one of those files should be loading defaults from a file (src/main/resources/data/filename.yml).

    Here is the constructor for the abstract superclass:
    public abstract class CustomConfig {
        private String path:
        private File file;
        private YamlConfiguration config;
        private JavaPlugin plugin;
        protected CustomConfig(JavaPlugin p, String filename) throws IOException {
            path = plugin.getDataFolder() + File.separator + "data";
            File dir = new File(path);
            if (!dir.exists())
            path += File.separator + filename + ".yml";
            plugin = p;
            file = new File(path);
            config = YamlConfiguration.loadConfiguration(file);
        // Some protected accessor methods...
    It may be a dumb question, or dumb code. Files are definitely not my strong suit.
    Last edited: May 16, 2019
  2. Offline


    @JellyRekt I don't see you write to the file anywhere, you only create it.
  3. Offline


    Shouldn't it load the existing file stored in the resources folder? IE, from src/main/resources/data/File.yml in my project to server/plugins/Plugin/data/File.yml in my server files? File#createNewFile() shouldn't do anything if the file already exists.
  4. Offline

    timtower Moderator Moderator

    Not if you do it like that. There you just load a file.
    That might be useful.
    And the loading from the resource folder is just for the config.yml I believe. Not for other files.
  5. Offline


    @timtower thanks but I think I actually solved it with plugin.saveResource()
Thread Status:
Not open for further replies.

Share This Page