From 7f17d6cb29448358e2f612ff6999612a50be9404 Mon Sep 17 00:00:00 2001 From: Connor M Date: Sat, 31 Dec 2011 11:43:42 -0600 Subject: [PATCH] Added a configuration converter --- war/.classpath | 3 + .../war/command/RenameZoneCommand.java | 2 +- .../war/mappers/PropertiesConverter.java | 112 ++++++++++++++++++ .../tommytony/war/mappers/PropertiesFile.java | 2 +- .../com/tommytony/war/mappers/WarMapper.java | 26 +++- .../tommytony/war/mappers/WarzoneMapper.java | 25 +++- 6 files changed, 162 insertions(+), 8 deletions(-) create mode 100644 war/src/main/java/com/tommytony/war/mappers/PropertiesConverter.java diff --git a/war/.classpath b/war/.classpath index 21e4d33..9edbf49 100644 --- a/war/.classpath +++ b/war/.classpath @@ -8,6 +8,9 @@ + + + diff --git a/war/src/main/java/bukkit/tommytony/war/command/RenameZoneCommand.java b/war/src/main/java/bukkit/tommytony/war/command/RenameZoneCommand.java index 8fc2bf7..49f5d9b 100644 --- a/war/src/main/java/bukkit/tommytony/war/command/RenameZoneCommand.java +++ b/war/src/main/java/bukkit/tommytony/war/command/RenameZoneCommand.java @@ -55,7 +55,7 @@ public class RenameZoneCommand extends AbstractZoneMakerCommand { War.war.getWarzones().remove(zone); // rename zone file - (new File(War.war.getDataFolder().getPath() + "/warzone-" + zone.getName() + ".txt")).renameTo(new File(War.war.getDataFolder().getPath() + "/warzone-" + this.args[0] + ".txt")); + (new File(War.war.getDataFolder().getPath() + "/warzone-" + zone.getName() + ".yml")).renameTo(new File(War.war.getDataFolder().getPath() + "/warzone-" + this.args[0] + ".yml")); // rename zone folder (new File(War.war.getDataFolder().getPath() + "/dat/warzone-" + zone.getName())).renameTo(new File(War.war.getDataFolder().getPath() + "/dat/warzone-" + this.args[0])); diff --git a/war/src/main/java/com/tommytony/war/mappers/PropertiesConverter.java b/war/src/main/java/com/tommytony/war/mappers/PropertiesConverter.java new file mode 100644 index 0000000..8bcda0c --- /dev/null +++ b/war/src/main/java/com/tommytony/war/mappers/PropertiesConverter.java @@ -0,0 +1,112 @@ +package com.tommytony.war.mappers; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; + +import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.configuration.file.YamlConfiguration; + +public class PropertiesConverter { + File properties; + File yaml; + public PropertiesConverter (File properties, File yaml) { + this.properties = properties; + this.yaml = yaml; + } + public void ConvertWarCfg() throws FileNotFoundException, IOException, InvalidConfigurationException { + PropertiesFile propertiesUtil = new PropertiesFile(properties.getAbsolutePath()); + YamlConfiguration yamlUtil = new YamlConfiguration(); + if (!yaml.exists()) { + yaml.createNewFile(); + } + propertiesUtil.load(); + yamlUtil.load(yaml); + yamlUtil.set("warzones", propertiesUtil.getString("warzones")); + yamlUtil.set("zoneMakers", propertiesUtil.getString("zoneMakers")); + yamlUtil.set("commandWhitelist", propertiesUtil.getString("commandWhitelist")); + yamlUtil.set("defaultLoadout", propertiesUtil.getString("defaultLoadout")); + yamlUtil.set("defaultExtraLoadouts", propertiesUtil.getString("defaultExtraLoadouts")); + yamlUtil.set("maxZones", propertiesUtil.getInt("maxZones")); + yamlUtil.set("defaultLifePool", propertiesUtil.getInt("defaultLifePool")); + yamlUtil.set("defaultMonumentHeal", propertiesUtil.getInt("defaultMonumentHeal")); + yamlUtil.set("defaultFriendlyFire", propertiesUtil.getBoolean("defaultFriendlyFire")); + yamlUtil.set("defaultAutoAssignOnly", propertiesUtil.getBoolean("defaultAutoAssignOnly")); + yamlUtil.set("defaultFlagPointsOnly", propertiesUtil.getBoolean("defaultFlagPointsOnly")); + yamlUtil.set("defaultTeamCap", propertiesUtil.getInt("defaultTeamCap")); + yamlUtil.set("defaultScoreCap", propertiesUtil.getInt("defaultScoreCap")); + yamlUtil.set("pvpInZonesOnly", propertiesUtil.getBoolean("pvpInZonesOnly")); + yamlUtil.set("defaultBlockHeads", propertiesUtil.getBoolean("defaultBlockHeads")); + yamlUtil.set("buildInZonesOnly", propertiesUtil.getBoolean("buildInZonesOnly")); + yamlUtil.set("disablePvpMessage", propertiesUtil.getBoolean("disablePvpMessage")); + yamlUtil.set("tntInZonesOnly", propertiesUtil.getBoolean("tntInZonesOnly")); + yamlUtil.set("spawnStyle", propertiesUtil.getString("spawnStyle")); + yamlUtil.set("flagReturn", propertiesUtil.getString("flagReturn")); + yamlUtil.set("defaultReward", propertiesUtil.getString("defaultReward")); + yamlUtil.set("defaultUnbreakableZoneBlocks", propertiesUtil.getBoolean("defaultUnbreakableZoneBlocks")); + yamlUtil.set("defaultNoCreatures", propertiesUtil.getBoolean("defaultNoCreatures")); + yamlUtil.set("defaultGlassWalls", propertiesUtil.getBoolean("defaultGlassWalls")); + yamlUtil.set("defaultPvpInZone", propertiesUtil.getBoolean("defaultPvpInZone")); + yamlUtil.set("defaultInstaBreak", propertiesUtil.getBoolean("defaultInstaBreak")); + yamlUtil.set("defaultNoDrops", propertiesUtil.getBoolean("defaultNoDrops")); + yamlUtil.set("defaultNoHunger", propertiesUtil.getBoolean("defaultNoHunger")); + yamlUtil.set("defaultSaturation", propertiesUtil.getInt("defaultSaturation")); + yamlUtil.set("defaultMinPlayers", propertiesUtil.getInt("defaultMinPlayers")); + yamlUtil.set("defaultMinTeams", propertiesUtil.getInt("defaultMinTeams")); + yamlUtil.set("defaultResetOnEmpty", propertiesUtil.getBoolean("defaultResetOnEmpty")); + yamlUtil.set("defaultResetOnLoad", propertiesUtil.getBoolean("defaultResetOnLoad")); + yamlUtil.set("defaultResetOnUnload", propertiesUtil.getBoolean("defaultResetOnUnload")); + yamlUtil.set("warhub", propertiesUtil.getString("warhub")); + yamlUtil.save(yaml); + propertiesUtil.close(); + + } + public void ConvertZoneCfg() throws FileNotFoundException, IOException, InvalidConfigurationException { + PropertiesFile propertiesUtil = new PropertiesFile(properties.getAbsolutePath()); + YamlConfiguration yamlUtil = new YamlConfiguration(); + if (!yaml.exists()) { + yaml.createNewFile(); + } + propertiesUtil.load(); + yamlUtil.load(yaml); + yamlUtil.set("name", propertiesUtil.getString("name")); + yamlUtil.set("world", propertiesUtil.getString("world")); + yamlUtil.set("teleport", propertiesUtil.getString("teleport")); + yamlUtil.set("teams", propertiesUtil.getString("teams")); + yamlUtil.set("teamFlags", propertiesUtil.getString("teamFlags")); + yamlUtil.set("friendlyFire", propertiesUtil.getString("friendlyFire")); + yamlUtil.set("loadout", propertiesUtil.getString("loadout")); + yamlUtil.set("extraLoadouts", propertiesUtil.getString("extraLoadouts")); + yamlUtil.set("author", propertiesUtil.getString("author")); + yamlUtil.set("lifePool", propertiesUtil.getString("lifePool")); + yamlUtil.set("monumentHeal", propertiesUtil.getString("monumentHeal")); + yamlUtil.set("autoAssignOnly", propertiesUtil.getString("autoAssignOnly")); + yamlUtil.set("flagPointsOnly", propertiesUtil.getString("flagPointsOnly")); + yamlUtil.set("teamCap", propertiesUtil.getString("teamCap")); + yamlUtil.set("scoreCap", propertiesUtil.getString("scoreCap")); + yamlUtil.set("blockHeads", propertiesUtil.getString("blockHeads")); + yamlUtil.set("spawnStyle", propertiesUtil.getString("spawnStyle")); + yamlUtil.set("flagReturn", propertiesUtil.getString("flagReturn")); + yamlUtil.set("reward", propertiesUtil.getString("reward")); + yamlUtil.set("unbreakableZoneBlocks", propertiesUtil.getString("unbreakableZoneBlocks")); + yamlUtil.set("disabled", propertiesUtil.getString("disabled")); + yamlUtil.set("noCreatures", propertiesUtil.getString("noCreatures")); + yamlUtil.set("glassWalls", propertiesUtil.getString("glassWalls")); + yamlUtil.set("pvpInZone", propertiesUtil.getString("pvpInZone")); + yamlUtil.set("instaBreak", propertiesUtil.getString("instaBreak")); + yamlUtil.set("noDrops", propertiesUtil.getString("noDrops")); + yamlUtil.set("noHunger", propertiesUtil.getString("noHunger")); + yamlUtil.set("saturation", propertiesUtil.getString("saturation")); + yamlUtil.set("minPlayers", propertiesUtil.getString("minPlayers")); + yamlUtil.set("minTeams", propertiesUtil.getString("minTeams")); + yamlUtil.set("resetOnEmpty", propertiesUtil.getString("resetOnEmpty")); + yamlUtil.set("resetOnLoad", propertiesUtil.getString("resetOnLoad")); + yamlUtil.set("resetOnUnload", propertiesUtil.getString("resetOnUnload")); + yamlUtil.set("rallyPoint", propertiesUtil.getString("rallyPoint")); + yamlUtil.set("monuments", propertiesUtil.getString("monuments")); + yamlUtil.set("lobby", propertiesUtil.getString("lobby")); + yamlUtil.save(yaml); + propertiesUtil.close(); + + } +} diff --git a/war/src/main/java/com/tommytony/war/mappers/PropertiesFile.java b/war/src/main/java/com/tommytony/war/mappers/PropertiesFile.java index 254c313..d084a87 100644 --- a/war/src/main/java/com/tommytony/war/mappers/PropertiesFile.java +++ b/war/src/main/java/com/tommytony/war/mappers/PropertiesFile.java @@ -404,4 +404,4 @@ public final class PropertiesFile { this.save(); } -} +} \ No newline at end of file diff --git a/war/src/main/java/com/tommytony/war/mappers/WarMapper.java b/war/src/main/java/com/tommytony/war/mappers/WarMapper.java index 9a778ce..a4b44a0 100644 --- a/war/src/main/java/com/tommytony/war/mappers/WarMapper.java +++ b/war/src/main/java/com/tommytony/war/mappers/WarMapper.java @@ -29,11 +29,25 @@ public class WarMapper { (new File(War.war.getDataFolder().getPath() + "/dat")).mkdir(); YamlConfiguration warConfig = new YamlConfiguration(); File config = new File(War.war.getDataFolder().getPath() + "/war.yml"); + File oldconfig = new File(War.war.getDataFolder().getPath() + "/war.txt"); + if (oldconfig.exists()) { + War.war.log("Old config file found. Converting to YAML...", Level.INFO); + try { + new PropertiesConverter(oldconfig, config).ConvertWarCfg(); + oldconfig.delete(); + War.war.log("Converted successfully!", Level.INFO); + } catch (Exception e) { + e.printStackTrace(); + oldconfig.renameTo(new File(War.war.getDataFolder().getPath() + "/war.txt.bad")); + War.war.log("Error in conversion, old war.txt has been renamed to war.txt.bad. Default config will be generated.", Level.INFO); + } + } + oldconfig = null; try { warConfig.load(config); } catch (Exception e) { - War.war.log("Failed to load war.txt file.", Level.WARNING); - e.printStackTrace(); + War.war.log("Failed to load war.yml file.", Level.WARNING); + e.getMessage(); } // Create file if need be @@ -58,7 +72,13 @@ public class WarMapper { } // zone makers - String[] makers = warConfig.getString("zoneMakers").split(","); + String[] makers = null; + try { + makers = warConfig.getString("zoneMakers").split(","); + } catch (NullPointerException e) { + warConfig.set("zoneMakers", ""); + makers = warConfig.getString("zoneMakers").split(","); + } War.war.getZoneMakerNames().clear(); for (String makerName : makers) { if (makerName != null && !makerName.equals("")) { diff --git a/war/src/main/java/com/tommytony/war/mappers/WarzoneMapper.java b/war/src/main/java/com/tommytony/war/mappers/WarzoneMapper.java index f40b293..a1b2da3 100644 --- a/war/src/main/java/com/tommytony/war/mappers/WarzoneMapper.java +++ b/war/src/main/java/com/tommytony/war/mappers/WarzoneMapper.java @@ -34,10 +34,29 @@ public class WarzoneMapper { public static Warzone load(String name, boolean createNewVolume) { // war.getLogger().info("Loading warzone " + name + " config and blocks..."); YamlConfiguration warzoneConfig = new YamlConfiguration(); - File config = new File(War.war.getDataFolder().getPath() + "/warzone-" + name + ".yml");try { + File config = new File(War.war.getDataFolder().getPath() + "/warzone-" + name + ".yml"); + File oldconfig = new File(War.war.getDataFolder().getPath() + "/warzone-" + name + ".txt"); + File dataFolder = new File(War.war.getDataFolder().getPath() + "/dat/warzone-" + name); + if (oldconfig.exists()) { + War.war.log("Old zone file found. Converting to YAML...", Level.INFO); + try { + new PropertiesConverter(oldconfig, config).ConvertZoneCfg(); + oldconfig.delete(); + War.war.log("Converted successfully!", Level.INFO); + } catch (Exception e) { + e.printStackTrace(); + oldconfig.renameTo(new File(War.war.getDataFolder().getPath() + "/warzone-" + name + ".txt.bad")); + dataFolder.renameTo(new File(War.war.getDataFolder().getPath() + "/dat/warzone-" + name + "-bad")); + War.war.getWarzones().remove(Warzone.getZoneByName(name)); + WarMapper.save(); + War.war.log("Error in conversion, old warzone-" + name + ".txt has been renamed to warzone-" + name + ".txt.bad and old data folder /dat/warzone-" + name + " has been renamed to /dat/warzone-" + name + "-bad. Zone has been removed.", Level.INFO); + } + } + oldconfig = null; + try { warzoneConfig.load(config); } catch (Exception e) { - War.war.getLogger().info("Failed to load warzone-" + name + ".txt file."); + War.war.getLogger().info("Failed to load warzone-" + name + ".yml file."); e.printStackTrace(); } @@ -591,7 +610,7 @@ public class WarzoneMapper { if (!deletedData) { War.war.log("Failed to delete folder " + zoneFolder.getName(), Level.WARNING); } - File zoneFile = new File(War.war.getDataFolder().getPath() + "/warzone-" + name + ".txt"); + File zoneFile = new File(War.war.getDataFolder().getPath() + "/warzone-" + name + ".yml"); deletedData = zoneFile.delete(); if (!deletedData) { War.war.log("Failed to delete file " + zoneFile.getName(), Level.WARNING);