1.12.2 Error getting yaml files list name

Discussion in 'Spigot Plugin Development' started by Nico12, Feb 11, 2020.

  1. I have this code
    Code (Text):

    public class ClasePrincipal extends JavaPlugin implements Listener{
       
        private File file = null;
        private FileConfiguration fc = null;
       
        SetupPlugin sp = new SetupPlugin();
        @Override
        public void onEnable() {
            Bukkit.getConsoleSender().sendMessage("skere");
            registerConfig();
            Bukkit.getPluginCommand("menu").setExecutor(new Command(this));
            sp.plugin(this);
            registerConfig();
            registerCustomConfig();
        }
        public void registerConfig() {
            File config = new File(this.getDataFolder(),"config.yml");
            if(!config.exists()){
                this.getConfig().options().copyDefaults(true);
                saveConfig();
            }
        }
        public void saveYML() {
            try {
                fc.save(file);
                }catch(IOException e) {
                e.printStackTrace();
            }
        }
        public String registerNewConfig() {
            File[] files = folder().listFiles();
            for(int i = 0; i < files.length; i++) {
                if(files[i].isFile()) {
                    return files[i].getName();      
                }
            }
            return registerNewConfig();
        }
        public File folder() {
            File folder = new File(this.getDataFolder() + File.separator + "Inventory");
            if(!folder.exists()) {
                folder.mkdir();
            }
            return folder;
        }
        public FileConfiguration getFiles() {
            if(fc == null) {
                reloadCustomConfig();
            }
            return fc;
        }
        public void reloadCustomConfig() {
            if(file != null)  {
                file = new File(folder(), registerNewConfig());
            }
           
            Reader defConfigStream;  
           
            fc = YamlConfiguration.loadConfiguration(file);
                try {
                    defConfigStream = new InputStreamReader(this.getResource(registerNewConfig()), "UTF8");

                   
                    if (defConfigStream != null) {
                        YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);

                        fc.setDefaults(defConfig);
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
        }
        public void registerCustomConfig() {
            File file2 = new File(this.getDataFolder() + File.separator + "Inventory", "ejemplo.yml");
            FileConfiguration fc2 = YamlConfiguration.loadConfiguration(file2);
            file = new File(folder(), registerNewConfig());
            if(!file.exists() ) {
                this.getFiles().options().copyDefaults(true);
                saveYML();
            }
            if(!file2.exists()) {
                fc2.options().copyDefaults(true);
                try {
                    fc2.save(file2);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
       
    }
     
    What it does is get the names of all the files in a directory (this.getDataFolder + File.Separator + "Inventory") turns out to work to a point, when there are no files inside the Inventory folder it throws this error:
    Code (Text):

    java.lang.StackOverflowError: null
            at com.theunclebarto.mein.ClasePrincipal.folder(ClasePrincipal.java:54) ~[?:?]
            at com.theunclebarto.mein.ClasePrincipal.registerNewConfig(ClasePrincipal.java:45) ~[?:?]
            at com.theunclebarto.mein.ClasePrincipal.registerNewConfig(ClasePrincipal.java:51) ~[?:?]
            at com.theunclebarto.mein.ClasePrincipal.registerNewConfig(ClasePrincipal.java:51) ~[?:?]
            at com.theunclebarto.mein.ClasePrincipal.registerNewConfig(ClasePrincipal.java:51) ~[?:?]
            at com.theunclebarto.mein.ClasePrincipal.registerNewConfig(ClasePrincipal.java:51) ~[?:?]
    and repeat too many times "at com.theunclebarto.mein.ClasePrincipal.registerNewConfig (Main Class.java:51) ~ [?:?]"
     
    Can you help me?

    ~ Thanks in the advance
     
  2. The method #registerNewConfig returns the method #registerNewConfig so it is called infinitely and is overflowing the stack, hence a StackOverflowError.
     
  3. drives_a_ford

    Moderator

    • Agree Agree x 1