Solved Reading from default config file

Discussion in 'Spigot Plugin Development' started by vallas, Feb 13, 2019.

  1. When I request a value from a config (lang.yml in this case) and it is not present, it should look in the default YAML file. This should happen without replacing the current configuration file but that's giving me some issues.
    I'm using IntelliJ and (the relevant part of) my project structure is as follows:
    • src
      • main
        • java
          • Project source files (in a package)
        • resources
          • plugin.yml
          • config.yml
          • lang.yml
      • test
    I don't have any problems reading from a saved file but getting lang.yml from the resources folder has proven to be a pain (to me at least). This is how I'm currently attempting to get the FileConfiguration:
    Code (Java):
    YamlConfiguration.loadConfiguration(new File (ConfigAPI.class.getResource("lang.yml").getFile()));
    It does not throw a NullPointerException but when I use FileConfiguration#getKeys(true).size(), it returns 0 while lang.yml is definitely not empty (since it can copy its contents to a test server and reading from those files works fine, the size is not 0 when reading from there).
    How would one go about doing this properly?

    Thanks in advance!

    The code below will load a file from the default resources folder in the jar file.
    Code (Java):
    Reader defaultStream = null;
    try {
        defaultStream = new InputStreamReader(plugin.getResource(fileName), "UTF8");
    } catch (UnsupportedEncodingException ex) {}

    if (defaultStream != null) {
        this.defaultConfig = YamlConfiguration.loadConfiguration(defaultStream);
    #1 vallas, Feb 13, 2019
    Last edited: Feb 17, 2019
    • Friendly Friendly x 1
  2. Alright, thank you @DiamondDagger590. I couldn't get it to work though, but I probably just need some good night's sleep and I'll take another (fresh) look tomorrow.
  3. No problem, if you need any help further just tag me again or dm me here or discord and I'll see what I can do
  4. Thank you!!

Share This Page