Reload Implementation for Custom Files

Discussion in 'Plugin Development' started by x_Jake_s, Nov 23, 2016.

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

    x_Jake_s

    I have recently taking the leap into configuration files after working with my plugin which works beautifully however i dont have any files to write information to and the basic config wont do. so i decided to venture into my old friend java docs and i have created some code for reloading my files that will reload all of them at once even the default one however im not sure if it's done efficiently. All im asking is for to have this looked over and told if there is an easier and more sensible way to do this:

    Code:
    public void reloadDMConfig() {
            // PLAYER FILE RELOAD
            if (playerFile == null) {
                playerFile = new File(getDataFolder(), "players.yml");
            }
            players = YamlConfiguration.loadConfiguration(playerFile);
    
            // Look for defaults in the jar
            try {
                Reader defConfigStream = new InputStreamReader(this.getResource("players.yml"), "UTF8");
                if (defConfigStream != null) {
                    YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                    players.setDefaults(defConfig);
                }
            } catch (Exception e) {
            }
    
            // ARENA FILE RELOAD
            if (arenaFile == null) {
                arenaFile = new File(getDataFolder(), "arenas.yml");
            }
            arenas = YamlConfiguration.loadConfiguration(arenaFile);
    
            // Look for defaults in the jar
            try {
                Reader defConfigStream = new InputStreamReader(this.getResource("arenas.yml"), "UTF8");
                if (defConfigStream != null) {
                    YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
                    arenas.setDefaults(defConfig);
                }
            } catch (Exception e) {
            }
          
            // DEFAULT CONFIG RELOAD
            reloadConfig();
        }
     
  2. Offline

    mythbusterma

    @x_Jake_s

    It looks fine as long as it works for the most part. Other than you are swallowing errors in places, and you really shouldn't be doing that report or properly handle errors. Also, instead of copy-pasting code, refactor it into a method and reuse common parts of the code.
     
    x_Jake_s likes this.
Thread Status:
Not open for further replies.

Share This Page