Solved Help with loading a custom configuration from a resource file

Discussion in 'Spigot Plugin Development' started by thehydrogen, Mar 7, 2020.

  1. Hey, I'm trying to get my custom configuration files being loaded from a resource file.

    Basically, I'm creating a file called "settings.yml" and I want it to be loaded from a file I have in my resources directory called the same thing. What happens is when I load up the plugin, everything works and the content of the resource appears. However, to the code, the file is empty. I've tried a lot and I can't seem to figure it out.

    Custom configuration maker:
    Code (Java):
    package me.thehydrogen.hydrolib.api.config;

    import me.thehydrogen.hydrolib.api.HydroInstance;
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.Plugin;

    import java.io.File;

    public class HydroFile {

        private static File file;
        private static FileConfiguration config;

        public HydroFile(String fileName) {

            final Plugin plugin = HydroInstance.getPlugin();

            file = new File(plugin.getDataFolder(), fileName + ".yml");

            if (!file.exists()) {
                file.getParentFile().mkdirs();
                plugin.saveResource(fileName + ".yml", false);
            }

            try {
                config = new YamlConfiguration();
                config.load(file);
            } catch (Exception e) {
                Bukkit.getLogger().warning("[!] COULD NOT CREATE A HYDROFILE.");
            }
        }

        public void save() {
            try {
                config.save(file);
                reload();
            } catch(Exception e) {
                Bukkit.getLogger().info("[!] FAILED TO SAVE A HYDROFILE");
            }
        }

        public void reload() {
            try {
                config = YamlConfiguration.loadConfiguration(file);
            } catch (Exception e) {
                Bukkit.getLogger().info("[!] FAILED TO RELOAD A HYDROFILE");
            }
        }

        public FileConfiguration get() { return config; }
        public FileConfiguration getConfig() { return config; }
    }
     
    Here is my main plugin file where the config file is created (removed rest of the code, only relevant parts):
    Code (Text):
    public final class PLUGIN extends JavaPlugin {
        public static HydroFile settings;
        public static HydroFile data;

        @Override
        public void onEnable() {
            getConfig().options().copyDefaults(true);
            saveConfig();

            try {
                settings = new HydroFile("settings");
                data = new HydroFile("data");
            } catch (Exception e) {
                Bukkit.getLogger().warning("couldn't load data and/or settings file(s)");
            }
        }
    }
    Any possible help?
     
  2. What exactly do you mean by that? Is any yml-file created or not?
     
  3. Yep, it's created, with the contents of the resource. But when trying to access values, they are all empty and don't exist.
     
  4. SteelPhoenix

    Moderator

    Yea because you're abusing static. you try to store different configurations in the same field.
     
  5. What's a way I can fix my code?
     
  6. What do you think? maybe start removing the statics...
     
  7. Never mind, I just realized the mistake. Sorry for the bother!
     
  8. Just saw it, never noticed that it was static.