From cd7ad37887b65cb24f4647bbb9237d76ac6aeb1e Mon Sep 17 00:00:00 2001 From: Eric Stokes Date: Fri, 29 Jul 2011 16:35:27 -0600 Subject: [PATCH] Add New ConfigMigrator Style --- .../MultiverseCore/MVPluginListener.java | 2 +- .../MultiverseCore/MultiverseCore.java | 10 ++++--- .../configuration/DefaultConfiguration.java | 9 ++----- .../configuration/MVConfigMigrator.java | 23 ++++++++++++++++ .../MVCoreConfigMigrator.java} | 26 ++++++------------- 5 files changed, 40 insertions(+), 30 deletions(-) create mode 100644 src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigMigrator.java rename src/main/java/com/onarandombox/MultiverseCore/{MVConfigMigrator.java => configuration/MVCoreConfigMigrator.java} (82%) diff --git a/src/main/java/com/onarandombox/MultiverseCore/MVPluginListener.java b/src/main/java/com/onarandombox/MultiverseCore/MVPluginListener.java index c45911b4..2b2a4c5a 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MVPluginListener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MVPluginListener.java @@ -38,7 +38,7 @@ public class MVPluginListener extends ServerListener { if (event.getPlugin().getDescription().getName().equals("MultiVerse")) { this.plugin.getServer().getPluginManager().disablePlugin(event.getPlugin()); this.plugin.log(Level.WARNING, "I just disabled the old version of Multiverse for you. You should remove the JAR now, your configs have been migrated."); - new DefaultConfiguration(this.plugin.getDataFolder(), "config.yml", this.plugin.migrator); + new DefaultConfiguration(this.plugin.getDataFolder(), "config.yml", this.plugin.migrator, MultiverseCore.defaultConfigsCreated); } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java index f09d3c2b..af8e6ce7 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -51,6 +51,8 @@ import com.onarandombox.MultiverseCore.commands.UnloadCommand; import com.onarandombox.MultiverseCore.commands.VersionCommand; import com.onarandombox.MultiverseCore.commands.WhoCommand; import com.onarandombox.MultiverseCore.configuration.DefaultConfiguration; +import com.onarandombox.MultiverseCore.configuration.MVConfigMigrator; +import com.onarandombox.MultiverseCore.configuration.MVCoreConfigMigrator; import com.onarandombox.utils.DebugLog; import com.onarandombox.utils.DestinationFactory; import com.onarandombox.utils.ExactDestination; @@ -95,9 +97,9 @@ public class MultiverseCore extends JavaPlugin { private HashMap playerSessions; private PurgeWorlds worldPurger; private GenericBank bank = null; - private AllPay banker = new AllPay(this, "[Multiverse-Core] "); + private AllPay banker = new AllPay(this, tag + " "); public static boolean defaultConfigsCreated = false; - protected MVConfigMigrator migrator = new MVConfigMigrator(this); + protected MVConfigMigrator migrator = new MVCoreConfigMigrator(this); protected int pluginCount; private DestinationFactory destFactory; @@ -188,8 +190,8 @@ public class MultiverseCore extends JavaPlugin { public void loadConfigs() { // Call the defaultConfiguration class to create the config files if they don't already exist. - new DefaultConfiguration(getDataFolder(), "config.yml", this.migrator); - new DefaultConfiguration(getDataFolder(), "worlds.yml", this.migrator); + new DefaultConfiguration(getDataFolder(), "config.yml", this.migrator, MultiverseCore.defaultConfigsCreated); + new DefaultConfiguration(getDataFolder(), "worlds.yml", this.migrator, MultiverseCore.defaultConfigsCreated); // Now grab the Configuration Files. this.configMV = new Configuration(new File(getDataFolder(), "config.yml")); diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultConfiguration.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultConfiguration.java index 872cc4fa..d7c4d4be 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultConfiguration.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultConfiguration.java @@ -4,9 +4,6 @@ import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; -import com.onarandombox.MultiverseCore.MVConfigMigrator; -import com.onarandombox.MultiverseCore.MultiverseCore; - /** * https://github.com/Nijikokun/iConomy3/blob/master/com/nijiko/coelho/iConomy/iConomy.java * @@ -14,12 +11,10 @@ import com.onarandombox.MultiverseCore.MultiverseCore; */ public class DefaultConfiguration { - public DefaultConfiguration(File folder, String name, MVConfigMigrator migrator) { + public DefaultConfiguration(File folder, String name, MVConfigMigrator migrator, boolean defaultsCreated) { File actual = new File(folder, name); // If defaults have been created, and we're being called again, we should try to migrate - if (MultiverseCore.defaultConfigsCreated) { - migrator.migrate(name, folder); - } else if (!actual.exists() && !migrator.migrate(name, folder)) { + if (!actual.exists() && !migrator.migrate(name, folder)) { InputStream input = this.getClass().getResourceAsStream("/defaults/" + name); if (input != null) { diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigMigrator.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigMigrator.java new file mode 100644 index 00000000..10212bcd --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigMigrator.java @@ -0,0 +1,23 @@ +package com.onarandombox.MultiverseCore.configuration; + +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.bukkit.util.config.Configuration; + +public abstract class MVConfigMigrator { + + public abstract boolean migrate(String name, File folder); + + protected final void migrateListItem(Configuration newConfig, Configuration oldConfig, String key, String oldProperty, String newProperty) { + List list = Arrays.asList(oldConfig.getString("worlds." + key + oldProperty).split(",")); + if (list.size() > 0) { + if (list.get(0).length() == 0) { + list = new ArrayList(); + } + } + newConfig.setProperty("worlds." + key + newProperty, list); + } +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/MVConfigMigrator.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVCoreConfigMigrator.java similarity index 82% rename from src/main/java/com/onarandombox/MultiverseCore/MVConfigMigrator.java rename to src/main/java/com/onarandombox/MultiverseCore/configuration/MVCoreConfigMigrator.java index f9bfa4ab..1ecfc4ab 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MVConfigMigrator.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVCoreConfigMigrator.java @@ -1,7 +1,6 @@ -package com.onarandombox.MultiverseCore; +package com.onarandombox.MultiverseCore.configuration; import java.io.File; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.logging.Level; @@ -9,10 +8,12 @@ import java.util.logging.Level; import org.bukkit.plugin.Plugin; import org.bukkit.util.config.Configuration; -public class MVConfigMigrator { +import com.onarandombox.MultiverseCore.MultiverseCore; + +public class MVCoreConfigMigrator extends MVConfigMigrator { private MultiverseCore core; - public MVConfigMigrator(MultiverseCore core) { + public MVCoreConfigMigrator(MultiverseCore core) { this.core = core; } @@ -72,29 +73,18 @@ public class MVConfigMigrator { newConfig.setProperty("worlds." + key + ".pvp", oldConfig.getProperty("worlds." + key + ".pvp")); newConfig.setProperty("worlds." + key + ".alias.name", oldConfig.getProperty("worlds." + key + ".alias")); newConfig.setProperty("worlds." + key + ".tempspawn", oldConfig.getProperty("worlds." + key + ".spawn")); - newConfig.setProperty("worlds." + key + ".price", oldConfig.getProperty("worlds." + key + ".price")); + newConfig.setProperty("worlds." + key + ".entryfee.amount", oldConfig.getProperty("worlds." + key + ".price")); + newConfig.setProperty("worlds." + key + ".entryfee.currency", -1); newConfig.setProperty("worlds." + key + ".environment", oldConfig.getProperty("worlds." + key + ".environment")); // Have to convert CSLs to arrays migrateListItem(newConfig, oldConfig, key, ".blockBlacklist", ".blockblacklist"); migrateListItem(newConfig, oldConfig, key, ".worldBlacklist", ".worldblacklist"); - migrateListItem(newConfig, oldConfig, key, ".playerBlacklist", ".playerblacklist"); - migrateListItem(newConfig, oldConfig, key, ".playerWhitelist", ".playerwhitelist"); } newConfig.save(); this.core.log(Level.INFO, "Migration SUCCESS!"); return true; } - private void migrateListItem(Configuration newConfig, Configuration oldConfig, String key, String oldProperty, String newProperty) { - List list = Arrays.asList(oldConfig.getString("worlds." + key + oldProperty).split(",")); - if (list.size() > 0) { - if (list.get(0).length() == 0) { - list = new ArrayList(); - } - } - newConfig.setProperty("worlds." + key + newProperty, list); - } - private boolean migrateMainConfig(String name, File oldFolder, File newFolder) { Configuration newConfig = new Configuration(new File(newFolder, "config.yml")); this.core.log(Level.INFO, "Migrating config.yml..."); @@ -107,7 +97,7 @@ public class MVConfigMigrator { newConfig.setProperty("disableautoheal", false); newConfig.setProperty("fakepvp", false); newConfig.setProperty("bedrespawn", true); - newConfig.setProperty("version", 2.0); + newConfig.setProperty("version", 2.2); newConfig.save(); return true; }