Add New ConfigMigrator Style

This commit is contained in:
Eric Stokes 2011-07-29 16:35:27 -06:00
parent dca9a01c25
commit cd7ad37887
5 changed files with 40 additions and 30 deletions

View File

@ -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);
}
}

View File

@ -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<String, MVPlayerSession> 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"));

View File

@ -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) {

View File

@ -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<String> list = Arrays.asList(oldConfig.getString("worlds." + key + oldProperty).split(","));
if (list.size() > 0) {
if (list.get(0).length() == 0) {
list = new ArrayList<String>();
}
}
newConfig.setProperty("worlds." + key + newProperty, list);
}
}

View File

@ -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<String> list = Arrays.asList(oldConfig.getString("worlds." + key + oldProperty).split(","));
if (list.size() > 0) {
if (list.get(0).length() == 0) {
list = new ArrayList<String>();
}
}
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;
}