Przeglądaj źródła

refactoring and comments

RedstoneFuture 3 lat temu
rodzic
commit
b08d74a9ae

+ 38 - 31
missilewars-plugin/src/main/java/de/butzlabben/missilewars/Config.java

@@ -45,25 +45,28 @@ public class Config {
     private static final File dir = MissileWars.getInstance().getDataFolder();
     private static final File file = new File(MissileWars.getInstance().getDataFolder(), "config.yml");
     private static YamlConfiguration cfg;
+    private static boolean configNew = false;
 
     public static void load() {
-        boolean configNew = false;
 
+        // check if the directory "/MissileWars" is exist
         if (!dir.exists())
             dir.mkdirs();
 
         SetupUtil.checkMissiles();
+
+        // check if the config file is exist
         if (!file.exists()) {
-            configNew = true;
-            dir.mkdirs();
             try {
                 file.createNewFile();
             } catch (IOException e) {
                 Logger.ERROR.log("Could not create config!");
                 e.printStackTrace();
             }
+            configNew = true;
         }
 
+        // try to load the config
         try {
             cfg = YamlConfiguration
                     .loadConfiguration(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
@@ -73,11 +76,40 @@ public class Config {
             return;
         }
 
+        // copy the config input
         cfg.options().copyDefaults(true);
 
+        // validate the config options
+        addDefaults();
+
+        // re-save the config with only validated options
+        saveConfig();
+    }
+
+    public static HashMap<String, Integer> getScoreboardEntries() {
+        HashMap<String, Integer> ret = new HashMap<>();
+        ConfigurationSection section = cfg.getConfigurationSection("sidebar.entries");
+        for (String s : section.getKeys(false)) {
+            ret.put(section.getString(s), Integer.valueOf(s));
+        }
+        return ret;
+    }
+
+    public static Material getStartReplace() {
+        String name = cfg.getString("replace.material", "JUKEBOX").toUpperCase();
+        try {
+            return valueOf(name);
+        } catch (Exception e) {
+            Logger.WARN.log("Unknown material " + name + " in start_replace");
+        }
+        return null;
+    }
+
+    private static void addDefaults() {
         cfg.addDefault("debug", false);
-        if (debug())
+        if (debug()) {
             Logger.DEBUG.log("Debug enabled");
+        }
 
         cfg.addDefault("setup_mode", false);
 
@@ -131,38 +163,13 @@ public class Config {
             cfg.addDefault("sidebar.entries.2", "   ");
             cfg.addDefault("sidebar.entries.1", "%team2% §7» %team2_color%%team2_amount%");
         }
-        try {
-            cfg.save(file);
-        } catch (IOException e) {
-            Logger.ERROR.log("Could not save config!");
-            e.printStackTrace();
-        }
-    }
-
-    public static HashMap<String, Integer> getScoreboardEntries() {
-        HashMap<String, Integer> ret = new HashMap<>();
-        ConfigurationSection section = cfg.getConfigurationSection("sidebar.entries");
-        for (String s : section.getKeys(false)) {
-            ret.put(section.getString(s), Integer.valueOf(s));
-        }
-        return ret;
     }
 
-    public static Material getStartReplace() {
-        String name = cfg.getString("replace.material", "JUKEBOX").toUpperCase();
-        try {
-            return valueOf(name);
-        } catch (Exception e) {
-            Logger.WARN.log("Unknown material " + name + " in start_replace");
-        }
-        return null;
-    }
-
-    public static void save(YamlConfiguration cfg) {
+    private static void saveConfig() {
         try {
             cfg.save(file);
         } catch (IOException e) {
-            Logger.ERROR.log("Couldn't save config");
+            Logger.ERROR.log("Could not save config!");
             e.printStackTrace();
         }
     }

+ 5 - 4
missilewars-plugin/src/main/java/de/butzlabben/missilewars/MissileWars.java

@@ -80,6 +80,7 @@ public class MissileWars extends JavaPlugin {
 
         Logger.BOOT.log("Loading properties...");
 
+        // delete old missile wars temp-worlds from the last server session
         deleteTempWorlds();
 
         Config.load();
@@ -99,9 +100,6 @@ public class MissileWars extends JavaPlugin {
         }
         this.signRepository = repository;
 
-        // TODO (Why twice again?)
-        deleteTempWorlds();
-
         Bukkit.getPluginManager().registerEvents(new PlayerListener(), this);
         Bukkit.getPluginManager().registerEvents(new ClickListener(), this);
         Bukkit.getPluginManager().registerEvents(new ManageListener(), this);
@@ -111,7 +109,6 @@ public class MissileWars extends JavaPlugin {
         framework.registerCommands(new MWCommands());
         framework.registerCommands(new StatsCommands());
         framework.registerCommands(new UserCommands());
-        Logger.BOOTDONE.log("Registering commands");
 
         Arenas.load();
         SetupUtil.checkShields();
@@ -120,6 +117,7 @@ public class MissileWars extends JavaPlugin {
 
         new Metrics(this, 3749);
 
+        // Check if FAWE is installed
         foundFAWE = Bukkit.getPluginManager().getPlugin("FastAsyncWorldEdit") != null;
 
         GameManager.getInstance().getGames().values().forEach(game -> {
@@ -155,6 +153,9 @@ public class MissileWars extends JavaPlugin {
         ConnectionHolder.close();
     }
 
+    /**
+     * @return true, of FAWE is installed
+     */
     public boolean foundFAWE() {
         return foundFAWE;
     }