Add ConfigMigrator

This commit is contained in:
Eric Stokes 2011-07-13 18:13:12 -06:00
parent fa07867f64
commit c6a16d00ce
6 changed files with 142 additions and 9 deletions

View File

@ -1,9 +1,105 @@
package com.onarandombox.MultiverseCore;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import org.bukkit.ChatColor;
import org.bukkit.plugin.Plugin;
import org.bukkit.util.config.Configuration;
public class MVConfigMigrator {
private MultiverseCore core;
public MVConfigMigrator(MultiverseCore core) {
this.core = core;
}
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<File> folderList = Arrays.asList(folders);
File MV1Folder = null;
for (File f : folderList) {
if (f.getName().equalsIgnoreCase("MultiVerse")) {
this.core.log(Level.INFO, "Found the MultiVerse 1 config folder. Starting Config Migration...");
MV1Folder = f;
}
}
if (MV1Folder == 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;
}
}
if (name.equalsIgnoreCase("worlds.yml")) {
return this.migrateWorlds(name, oldFolder, folder);
}
if (name.equalsIgnoreCase("config.yml")) {
return this.migrateMainConfig(name, oldFolder, folder);
}
return true;
}
private boolean migrateWorlds(String name, File oldFolder, File newFolder) {
Configuration newConfig = new Configuration(new File(newFolder, "worlds.yml"));
this.core.log(Level.INFO, "Migrating worlds.yml...");
Configuration oldConfig = new Configuration(new File(oldFolder, "worlds.yml"));
oldConfig.load();
List<String> keys = oldConfig.getKeys("worlds");
for (String key : keys) {
newConfig.setProperty("worlds." + key + ".animals.spawn", oldConfig.getProperty("worlds." + key + ".animals"));
newConfig.setProperty("worlds." + key + ".monsters.spawn", oldConfig.getProperty("worlds." + key + ".mobs"));
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 + ".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");
migrateListItem(newConfig, oldConfig, key, ".editBlacklist", ".editblacklist");
migrateListItem(newConfig, oldConfig, key, ".editWhitelist", ".editwhitelist");
}
newConfig.save();
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...");
Configuration oldConfig = new Configuration(oldFolder);
oldConfig.load();
return true;
}
}

View File

@ -70,6 +70,7 @@ public class MVPermissions implements PermissionsInterface {
// If there's anyone in the whitelist, then the whitelist is ACTIVE, anyone not in it is blacklisted.
if (whiteList.size() > 0) {
p.sendMessage("Whitelist Active!");
returnValue = false;
}
for (String bls : blackList) {

View File

@ -9,6 +9,7 @@ import org.bukkit.event.server.ServerListener;
import com.fernferret.allpay.AllPay;
import com.nijikokun.bukkit.Permissions.Permissions;
import com.onarandombox.MultiverseCore.configuration.DefaultConfiguration;
public class MVPluginListener extends ServerListener {
@ -34,6 +35,11 @@ public class MVPluginListener extends ServerListener {
if (Arrays.asList(AllPay.validEconPlugins).contains(event.getPlugin().getDescription().getName())) {
this.plugin.bank = this.plugin.banker.loadEconPlugin();
}
if(event.getPlugin().getDescription().getName().equalsIgnoreCase("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);
}
}
/**

View File

@ -3,6 +3,7 @@ package com.onarandombox.MultiverseCore;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import org.bukkit.ChatColor;
import org.bukkit.World;
@ -128,6 +129,26 @@ public class MVWorld {
this.getBlockBlacklist().addAll(config.getIntList("worlds." + this.name + ".blockblacklist", new ArrayList<Integer>()));
this.getEditWhitelist().addAll(config.getStringList("worlds." + this.name + ".editwhitelist", new ArrayList<String>()));
this.getEditBlacklist().addAll(config.getStringList("worlds." + this.name + ".editblacklist", new ArrayList<String>()));
String tempspawn = config.getString("worlds." + this.name + ".tempspawn", "");
if (tempspawn.length() > 0) {
String[] coordsString = tempspawn.split(":");
if (coordsString.length >= 3) {
int[] coords = new int[3];
try {
for (int i = 0; i < 3; i++) {
coords[i] = Integer.parseInt(coordsString[i]);
}
this.world.setSpawnLocation(coords[0], coords[1], coords[2]);
} catch (NumberFormatException e) {
this.plugin.log(Level.WARNING, "A MV1 spawn value was found, but it could not be migrated. Format Error. Sorry.");
}
} else {
this.plugin.log(Level.WARNING, "A MV1 spawn value was found, but it could not be migrated. Format Error. Sorry.");
}
this.config.removeProperty("worlds." + this.name + ".tempspawn");
}
config.save();
// The following 3 lines will add some sample data to new worlds created.

View File

@ -22,6 +22,8 @@ import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.config.Configuration;
import sun.tools.attach.MacosxVirtualMachine;
import com.fernferret.allpay.AllPay;
import com.fernferret.allpay.GenericBank;
import com.onarandombox.MultiverseCore.command.commands.*;
@ -70,7 +72,8 @@ public class MultiverseCore extends JavaPlugin {
private PurgeWorlds worldPurger;
public GenericBank bank = null;
public AllPay banker = new AllPay(this, "[Multiverse-Core] ");
;
public static boolean defaultConfigsCreated = false;
protected MVConfigMigrator migrator = new MVConfigMigrator(this);
@Override
public void onLoad() {
@ -142,9 +145,10 @@ public class MultiverseCore extends JavaPlugin {
* Load the Configuration files OR create the default config files.
*/
public void loadConfigs() {
// Call the defaultConfiguration class to create the config files if they don't already exist.
new DefaultConfiguration(getDataFolder(), "config.yml");
new DefaultConfiguration(getDataFolder(), "worlds.yml");
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"));

View File

@ -4,6 +4,9 @@ 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
*
@ -11,10 +14,13 @@ import java.io.InputStream;
*/
public class DefaultConfiguration {
public DefaultConfiguration(File folder, String name) {
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 (MultiverseCore.defaultConfigsCreated) {
migrator.migrate(name, folder);
} else if (!actual.exists() && !migrator.migrate(name, folder)) {
if (!actual.exists()) {
InputStream input = this.getClass().getResourceAsStream("/defaults/" + name);
if (input != null) {
FileOutputStream output = null;
@ -48,5 +54,4 @@ public class DefaultConfiguration {
}
}
}
}