Solved Custom config files always returns null when created with a method

Discussion in 'Spigot Plugin Development' started by Stellrow, Feb 27, 2020.

  1. So i wanted to shorten my code of creating configs with a method
    Code (Text):
    public void createIConfig(File file,FileConfiguration filecfg,String path) {
            file = new File(getDataFolder(),path);
            if(!file.exists()) {
                getServer().getConsoleSender().sendMessage(path + " not found,creating");
                file.getParentFile().mkdirs();
                saveResource(path,false);
            }
            filecfg = new YamlConfiguration();
            try{
                filecfg.load(file);
            }catch(IOException | InvalidConfigurationException e) {
                e.printStackTrace();
            }
        }
    Using it
    Code (Text):
    createIConfig(cgen,cgencfg,"cgen.yml");
    But whenever i try to acces it using the methods
    Code (Text):
    public void saveCgen() throws IOException {
            cgencfg.save(cgen);
        }
    public FileConfiguration getCgen() {
            return this.cgencfg;
        }
    Will always return a null file

    Everything is in proper order(i create the configs first before anything at onEnable

    The version im using that works is this
    Code (Text):
    crucible = new File(getDataFolder(),"crucible.yml");
            if(!crucible.exists()) {
                getServer().getConsoleSender().sendMessage("crucible.yml is being created!");
                crucible.getParentFile().mkdirs();
                saveResource("crucible.yml",false);
            }
            cruciblecfg = new YamlConfiguration();
            try {
                cruciblecfg.load(crucible);
            }catch(IOException | InvalidConfigurationException e) {
                e.printStackTrace();
            }
    Wich allows me to properly use the methods above,even though the method does basically the same i always get a null

    Why is that happening?
     
  2. SteelPhoenix

    Moderator

    You never overwrite the fields outside of the method scope.
     
  3. How so? Im passing defined variables in
    Code (Text):
    private File crucible,cgen,butoiLemn,siever,players,shopItems;
        private FileConfiguration cruciblecfg,cgencfg,butoiLemncfg,sievercfg,playerscfg,shopItemscfg;
    If that's the case how would i solve the problem?

    Im basically doing what the method does(1 by 1) (exactly the same as the method) and it works
     
  4. SteelPhoenix

    Moderator

    Java is not pass-by-reference.
     
    • Useful Useful x 1
  5. Haven't had enuff caffeine this morning but I believe you should be returning the configuration you loaded and assigning it to a variable.
    *wanders off to find something with caffeine in it*
     
    • Useful Useful x 1
  6. when you do 'new YamlConfiguration()' you are basically destroying any reference and making a new object that you load with data and then do nothing with.
     
  7. Not as good looking as i wanted but will suffice
    Code (Text):
    public FileConfiguration retCfg(FileConfiguration cfg,File filetoLoad) {
            cfg = new YamlConfiguration();
            try {
                cfg.load(filetoLoad);
            }catch(IOException | InvalidConfigurationException e) {
                e.printStackTrace();
            }
            return cfg;
        }
        public File retFile(File file,String name) {
            file = new File(getDataFolder(),name);
            if(!file.exists()) {
                file.getParentFile().mkdirs();
                saveResource(name,false);
            }
            return file;
        }
    Code (Text):
    butoiLemn = retFile(butoiLemn,"butoiLemn.yml");
            butoiLemncfg = retCfg(butoiLemncfg,butoiLemn);
     
  8. You dont need to pass in the file or the cfg as an argument if you are returning them and assigning them. It's just redundant...
     
  9. Yeah noticed that when i was working with them
    Code (Text):
    crucible = retFile("crucible.yml");
            cruciblecfg = retCfg(crucible);
    Code (Text):
    public FileConfiguration retCfg(File filetoLoad) {
            FileConfiguration cfg = new YamlConfiguration();
            try {
                cfg.load(filetoLoad);
            }catch(IOException | InvalidConfigurationException e) {
                e.printStackTrace();
            }
            return cfg;
        }
        public File retFile(String name) {
            File file = new File(getDataFolder(),name);
            if(!file.exists()) {
                file.getParentFile().mkdirs();
                saveResource(name,false);
            }
            return file;
        }
     
  10. If you look at the source for saveResource in JavaPlugin it creates the plugin folder for you if it doesn't exist. So thats one more thing you don't have to check for and create. In fact your whole retFile method seems to just be checks what saveResource already does. The only thing this method actually accomplishes is creating a new File object. I do this kind of stuff in my first drafts all the time and then berate myself when I'm cleaning it up. So don't feel bad, you made it work now you are trying to making it right. Which is exactly what you should do. :) Cause in Java just because it works doesn't mean its right :p That should be on a t-shirt or something lol
     
    • Useful Useful x 1