From 48f9226d2779a06a887714be074143e9925860b4 Mon Sep 17 00:00:00 2001 From: Eric Stokes Date: Fri, 29 Jul 2011 16:57:38 -0600 Subject: [PATCH] Refactoring on the ConfigMigrator --- .../MultiverseCore/MultiverseCore.java | 5 ++- .../configuration/DefaultConfiguration.java | 2 +- .../configuration/MVConfigMigrator.java | 36 +++++++++++++++++++ .../configuration/MVCoreConfigMigrator.java | 30 +--------------- 4 files changed, 40 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java index af8e6ce7..c9d44961 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -98,7 +98,6 @@ public class MultiverseCore extends JavaPlugin { private PurgeWorlds worldPurger; private GenericBank bank = null; private AllPay banker = new AllPay(this, tag + " "); - public static boolean defaultConfigsCreated = false; protected MVConfigMigrator migrator = new MVCoreConfigMigrator(this); protected int pluginCount; private DestinationFactory destFactory; @@ -190,8 +189,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, MultiverseCore.defaultConfigsCreated); - new DefaultConfiguration(getDataFolder(), "worlds.yml", this.migrator, MultiverseCore.defaultConfigsCreated); + new DefaultConfiguration(getDataFolder(), "config.yml", this.migrator); + new DefaultConfiguration(getDataFolder(), "worlds.yml", this.migrator); // 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 d7c4d4be..1de51faa 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultConfiguration.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/DefaultConfiguration.java @@ -11,7 +11,7 @@ import java.io.InputStream; */ public class DefaultConfiguration { - public DefaultConfiguration(File folder, String name, MVConfigMigrator migrator, boolean defaultsCreated) { + public DefaultConfiguration(File folder, String name, MVConfigMigrator migrator) { File actual = new File(folder, name); // If defaults have been created, and we're being called again, we should try to migrate if (!actual.exists() && !migrator.migrate(name, folder)) { diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigMigrator.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigMigrator.java index 10212bcd..29329b99 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigMigrator.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVConfigMigrator.java @@ -4,9 +4,13 @@ import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.logging.Level; +import org.bukkit.plugin.Plugin; import org.bukkit.util.config.Configuration; +import com.onarandombox.MultiverseCore.MultiverseCore; + public abstract class MVConfigMigrator { public abstract boolean migrate(String name, File folder); @@ -20,4 +24,36 @@ public abstract class MVConfigMigrator { } newConfig.setProperty("worlds." + key + newProperty, list); } + + protected final File detectMultiverseFolders(File folder, MultiverseCore core) { + File oldFolder = null; + + // They still have MV 1 installed! Good! + if (core.getServer().getPluginManager().getPlugin("MultiVerse") != null) { + core.log(Level.INFO, "Found MultiVerse 1. Starting Config Migration..."); + Plugin plugin = core.getServer().getPluginManager().getPlugin("MultiVerse"); + // We found MV1, but it's enabled, get the data folder, then disable it! + if (!core.getServer().getPluginManager().isPluginEnabled("MultiVerse")) { + core.getServer().getPluginManager().disablePlugin(plugin); + } + oldFolder = plugin.getDataFolder(); + } else { + // They didn't have MV 1 enabled... let's try and find the folder... + File[] folders = folder.getParentFile().listFiles(); + List folderList = Arrays.asList(folders); + for (File f : folderList) { + if (f.getName().equalsIgnoreCase("MultiVerse")) { + core.log(Level.INFO, "Found the MultiVerse 1 config folder. Starting Config Migration..."); + oldFolder = f; + } + + } + if (oldFolder == null) { + core.log(Level.INFO, "Did not find the MV1 Folder. If you did not have MultiVerse 1 installed and this is the FIRST time you're running MV2, this message is GOOD. "); + core.log(Level.INFO, "If you did, your configs were **NOT** migrated! Go Here: INSERTURLFORHELP"); + return null; + } + } + return oldFolder; + } } diff --git a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVCoreConfigMigrator.java b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVCoreConfigMigrator.java index 1ecfc4ab..ba6828fa 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/configuration/MVCoreConfigMigrator.java +++ b/src/main/java/com/onarandombox/MultiverseCore/configuration/MVCoreConfigMigrator.java @@ -1,11 +1,9 @@ package com.onarandombox.MultiverseCore.configuration; import java.io.File; -import java.util.Arrays; import java.util.List; import java.util.logging.Level; -import org.bukkit.plugin.Plugin; import org.bukkit.util.config.Configuration; import com.onarandombox.MultiverseCore.MultiverseCore; @@ -18,33 +16,7 @@ public class MVCoreConfigMigrator extends MVConfigMigrator { } public boolean migrate(String name, File folder) { - File oldFolder = null; - - // They still have MV 1 installed! Good! - if (this.core.getServer().getPluginManager().getPlugin("MultiVerse") != null) { - this.core.log(Level.INFO, "Found MultiVerse 1. Starting Config Migration..."); - if (!this.core.getServer().getPluginManager().isPluginEnabled("MultiVerse")) { - Plugin plugin = this.core.getServer().getPluginManager().getPlugin("MultiVerse"); - oldFolder = plugin.getDataFolder(); - this.core.getServer().getPluginManager().disablePlugin(plugin); - } - } else { - // They didn't have MV 1 enabled... let's try and find the folder... - File[] folders = folder.getParentFile().listFiles(); - List folderList = Arrays.asList(folders); - for (File f : folderList) { - if (f.getName().equalsIgnoreCase("MultiVerse")) { - this.core.log(Level.INFO, "Found the MultiVerse 1 config folder. Starting Config Migration..."); - oldFolder = f; - } - - } - if (oldFolder == null) { - this.core.log(Level.INFO, "Did not find the MV1 Folder. If you did not have MultiVerse 1 installed and this is the FIRST time you're running MV2, this message is GOOD. "); - this.core.log(Level.INFO, "If you did, your configs were **NOT** migrated! Go Here: INSERTURLFORHELP"); - return false; - } - } + File oldFolder = this.detectMultiverseFolders(folder, core); if (name.equalsIgnoreCase("worlds.yml")) { return this.migrateWorlds(name, oldFolder, folder);