Initial file creation overwriting defaults

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

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

    JellyRekt

    Solution
    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.
    Code:
        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:
    Code:
    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())
                dir.mkdirs();
            path += File.separator + filename + ".yml";
            plugin = p;
            file = new File(path);
            file.createNewFile();
            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

    KarimAKL

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

    JellyRekt

    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.
    https://hub.spigotmc.org/javadocs/b...n.html#saveResource-java.lang.String-boolean-
    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

    JellyRekt

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

Share This Page