Bladeren bron

refactoring and comments

RedstoneFuture 3 jaren geleden
bovenliggende
commit
91738c3dd0

+ 37 - 40
missilewars-plugin/src/main/java/de/butzlabben/missilewars/Config.java

@@ -53,14 +53,12 @@ public class Config {
         if (!dir.exists())
             dir.mkdirs();
 
-        SetupUtil.checkMissiles();
-
         // check if the config file is exist
         if (!file.exists()) {
             try {
                 file.createNewFile();
             } catch (IOException e) {
-                Logger.ERROR.log("Could not create config!");
+                Logger.ERROR.log("Could not create config.yml!");
                 e.printStackTrace();
             }
             configNew = true;
@@ -68,11 +66,10 @@ public class Config {
 
         // try to load the config
         try {
-            cfg = YamlConfiguration
-                    .loadConfiguration(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
-        } catch (FileNotFoundException e1) {
+            cfg = YamlConfiguration.loadConfiguration(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
+        } catch (FileNotFoundException e) {
             Logger.ERROR.log("Couldn't load config.yml");
-            e1.printStackTrace();
+            e.printStackTrace();
             return;
         }
 
@@ -86,25 +83,6 @@ public class Config {
         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()) {
@@ -169,11 +147,30 @@ public class Config {
         try {
             cfg.save(file);
         } catch (IOException e) {
-            Logger.ERROR.log("Could not save config!");
+            Logger.ERROR.log("Could not save config.yml!");
             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 Location getFallbackSpawn() {
         ConfigurationSection cfg = Config.cfg.getConfigurationSection("fallback_spawn");
         World world = Bukkit.getWorld(cfg.getString("world"));
@@ -195,27 +192,27 @@ public class Config {
     }
 
     public static String motdEnded() {
-        return cfg.getString("motd.ended", "&cError configuring motd in motd.ended");
+        return cfg.getString("motd.ended");
     }
 
     public static String motdGame() {
-        return cfg.getString("motd.ingame", "&cError configuring motd in motd.ingame");
+        return cfg.getString("motd.ingame");
     }
 
     public static String motdLobby() {
-        return cfg.getString("motd.lobby", "&cError configuring motd in motd.lobby");
+        return cfg.getString("motd.lobby");
     }
 
     public static boolean motdEnabled() {
-        return cfg.getBoolean("motd.enable", true);
+        return cfg.getBoolean("motd.enable");
     }
 
     public static int getReplaceTicks() {
-        return cfg.getInt("replace.after_ticks", 1);
+        return cfg.getInt("replace.after_ticks");
     }
 
     public static int getReplaceRadius() {
-        return cfg.getInt("replace.radius", 1);
+        return cfg.getInt("replace.radius");
     }
 
     static boolean debug() {
@@ -239,31 +236,31 @@ public class Config {
     }
 
     public static String getHost() {
-        return cfg.getString("mysql.host", "localhost");
+        return cfg.getString("mysql.host");
     }
 
     public static String getDatabase() {
-        return cfg.getString("mysql.database", "db");
+        return cfg.getString("mysql.database");
     }
 
     public static String getPort() {
-        return cfg.getString("mysql.port", "3306");
+        return cfg.getString("mysql.port");
     }
 
     public static String getUser() {
-        return cfg.getString("mysql.user", "root");
+        return cfg.getString("mysql.user");
     }
 
     public static String getPassword() {
-        return cfg.getString("mysql.password", "");
+        return cfg.getString("mysql.password");
     }
 
     public static String getFightsTable() {
-        return cfg.getString("mysql.fights_table", "mw_fights");
+        return cfg.getString("mysql.fights_table");
     }
 
     public static String getFightMembersTable() {
-        return cfg.getString("mysql.fightmember_table", "mw_fightmember");
+        return cfg.getString("mysql.fightmember_table");
     }
 
     public static String getArenaFolder() {

+ 27 - 7
missilewars-plugin/src/main/java/de/butzlabben/missilewars/MessageConfig.java

@@ -34,30 +34,47 @@ import org.bukkit.configuration.file.YamlConfiguration;
  */
 public class MessageConfig {
 
-    private static final File dir = new File(MissileWars.getInstance().getDataFolder(), "missiles/");
+    private static final File dir = MissileWars.getInstance().getDataFolder();
     private static final File file = new File(MissileWars.getInstance().getDataFolder(), "messages.yml");
     private static YamlConfiguration cfg;
 
     public static void load() {
-        if (!file.exists()) {
+
+        // check if the directory "/MissileWars" is exist
+        if (!dir.exists())
             dir.mkdirs();
+
+        // check if the config file is exist
+        if (!file.exists()) {
             try {
                 file.createNewFile();
             } catch (IOException e) {
-                Logger.ERROR.log("Could not create properties!");
+                Logger.ERROR.log("Could not create messages.yml!");
                 e.printStackTrace();
             }
         }
+
+        // try to load the config
         try {
             cfg = YamlConfiguration.loadConfiguration(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
-        } catch (FileNotFoundException e1) {
+        } catch (FileNotFoundException e) {
             Logger.ERROR.log("Couldn't load messages.yml");
-            e1.printStackTrace();
+            e.printStackTrace();
             return;
         }
 
+        // copy the config input
         cfg.options().copyDefaults(true);
 
+        // validate the config options
+        addDefaults();
+
+        // re-save the config with only validated options
+        saveConfig();
+    }
+
+    private static void addDefaults() {
+
         cfg.addDefault("prefix", "&6•&e● MissileWars &8▎  &7");
 
         cfg.addDefault("not_in_arena", "&cYou are not in an arena right now");
@@ -108,10 +125,13 @@ public class MessageConfig {
         cfg.addDefault("vote.finished", "The map %map% &7was elected");
         cfg.addDefault("vote.gui", "Vote for a map");
 
+    }
+
+    private static void saveConfig() {
         try {
             cfg.save(file);
         } catch (IOException e) {
-            Logger.ERROR.log("Could not save properties!");
+            Logger.ERROR.log("Could not save messages.yml!");
             e.printStackTrace();
         }
     }
@@ -129,6 +149,6 @@ public class MessageConfig {
     }
 
     public static String getPrefix() {
-        return ChatColor.translateAlternateColorCodes('&', cfg.getString("prefix", "&6•&e● MissileWars &8▎  &7"));
+        return ChatColor.translateAlternateColorCodes('&', cfg.getString("prefix"));
     }
 }

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

@@ -85,10 +85,7 @@ public class MissileWars extends JavaPlugin {
 
         Config.load();
         MessageConfig.load();
-
-        // TODO
-        // I don't know why, and I don't want to know why, but this is needed to ensure the the messages are properly loaded at the first time
-        MessageConfig.load();
+        SetupUtil.checkMissiles();
 
         new File(Config.getArenaFolder()).mkdirs();
         new File(Config.getLobbiesFolder()).mkdirs();

+ 3 - 1
missilewars-plugin/src/main/java/de/butzlabben/missilewars/util/SetupUtil.java

@@ -82,6 +82,8 @@ public class SetupUtil {
 
     public static void checkMissiles() {
         File file = new File(MissileWars.getInstance().getDataFolder(), "missiles");
+
+        // check if the directory "/missiles" is exist
         if (!file.isDirectory()) {
             Logger.BOOT.log("Copying default missiles folder");
 
@@ -90,7 +92,7 @@ public class SetupUtil {
             try {
                 copyZip(resource, file.getPath());
             } catch (IOException e) {
-                Logger.ERROR.log("Unable to copy missiles");
+                Logger.ERROR.log("Unable to copy missiles!");
                 e.printStackTrace();
             }
         }