/**
* Migrate the worlds.yml to SerializationConfig.
*/
private void migrateWorldConfig() { // SUPPRESS CHECKSTYLE: MethodLength
FileConfiguration wconf = new YamlConfiguration();
wconf.options().pathSeparator(PATH_SEPARATOR);
File worldsFile = new File(getDataFolder(), "worlds.yml");
try {
wconf.load(worldsFile);
} catch (IOException e) {
log(Level.WARNING, "Cannot load worlds.yml");
} catch (InvalidConfigurationException e) {
log(Level.WARNING, "Your worlds.yml is invalid!");
}
if (!wconf.isConfigurationSection("worlds")) { // empty config
this.log(Level.FINE, "No worlds to migrate!");
return;
}
Map<String, Object> values = wconf.getConfigurationSection("worlds").getValues(false);
boolean wasChanged = false;
Map<String, Object> newValues = new LinkedHashMap<String, Object>(values.size());
for (Map.Entry<String, Object> entry : values.entrySet()) {
if (entry.getValue() instanceof WorldProperties) {
// fine
newValues.put(entry.getKey(), entry.getValue());
} else if (entry.getValue() instanceof ConfigurationSection) {
this.log(Level.FINE, "Migrating: " + entry.getKey());
// we have to migrate this
WorldProperties world = new WorldProperties(Collections.EMPTY_MAP);
ConfigurationSection section = (ConfigurationSection) entry.getValue();
// migrate animals and monsters
if (section.isConfigurationSection("animals")) {
ConfigurationSection animalSection = section.getConfigurationSection("animals");
if (animalSection.contains("spawn")) {
if (animalSection.isBoolean("spawn"))
world.setAllowAnimalSpawn(animalSection.getBoolean("spawn"));
else
world.setAllowAnimalSpawn(Boolean.parseBoolean(animalSection.getString("spawn")));
}
if (animalSection.isList("exceptions")) {
world.getAnimalList().clear();
world.getAnimalList().addAll(animalSection.getStringList("exceptions"));
}
}
if (section.isConfigurationSection("monsters")) {
ConfigurationSection monsterSection = section.getConfigurationSection("monsters");
if (monsterSection.contains("spawn")) {
if (monsterSection.isBoolean("spawn"))
world.setAllowMonsterSpawn(monsterSection.getBoolean("spawn"));
else
world.setAllowMonsterSpawn(Boolean.parseBoolean(monsterSection.getString("spawn")));
}
if (monsterSection.isList("exceptions")) {
world.getMonsterList().clear();
world.getMonsterList().addAll(monsterSection.getStringList("exceptions"));
}
}
// migrate entryfee
if (section.isConfigurationSection("entryfee")) {
ConfigurationSection feeSection = section.getConfigurationSection("entryfee");
if (feeSection.isInt("currency"))
world.setCurrency(feeSection.getInt("currency"));
if (feeSection.isDouble("amount"))
world.setPrice(feeSection.getDouble("amount"));
else if (feeSection.isInt("amount"))
world.setPrice(feeSection.getInt("amount"));
}
// migrate pvp
if (section.isBoolean("pvp")) {
world.setPVPMode(section.getBoolean("pvp"));
}
// migrate alias
if (section.isConfigurationSection("alias")) {
ConfigurationSection aliasSection = section.getConfigurationSection("alias");
if (aliasSection.isString("color"))
world.setColor(aliasSection.getString("color"));
if (aliasSection.isString("name"))
world.setAlias(aliasSection.getString("name"));
}
// migrate worldblacklist
if (section.isList("worldblacklist")) {
world.getWorldBlacklist().clear();
world.getWorldBlacklist().addAll(section.getStringList("worldblacklist"));
}
// migrate scale
if (section.isDouble("scale")) {
world.setScaling(section.getDouble("scale"));
}
// migrate gamemode
if (section.isString("gamemode")) {
final GameMode gameMode = GameMode.valueOf(section.getString("gamemode").toUpperCase());
if (gameMode != null) {
world.setGameMode(gameMode);
}
}
// migrate hunger
if (section.isBoolean("hunger")) {
world.setHunger(section.getBoolean("hunger"));
}
// migrate hidden
if (section.isBoolean("hidden")) {
world.setHidden(section.getBoolean("hidden"));
}
// migrate autoheal
if (section.isBoolean("autoheal")) {
world.setAutoHeal(section.getBoolean("autoheal"));
}
// migrate portalform
if (section.isString("portalform")) {
try {
world.setProperty("portalform", section.getString("portalform"), true);
} catch (NoSuchPropertyException e) {
throw new RuntimeException("Who forgot to update the migrator?", e);
}
}
// migrate environment
if (section.isString("environment")) {
try {
world.setProperty("environment", section.getString("environment"), true);
} catch (NoSuchPropertyException e) {
throw new RuntimeException("Who forgot to update the migrator?", e);
}
}
// migrate generator
if (section.isString("generator")) {
world.setGenerator(section.getString("generator"));
}
// migrate seed
if (section.isLong("seed")) {
world.setSeed(section.getLong("seed"));
}
// migrate weather
if (section.isBoolean("allowweather")) {
world.setEnableWeather(section.getBoolean("allowweather"));
}
// migrate adjustspawn
if (section.isBoolean("adjustspawn")) {
world.setAdjustSpawn(section.getBoolean("adjustspawn"));
}
// migrate autoload
if (section.isBoolean("autoload")) {
world.setAutoLoad(section.getBoolean("autoload"));
}
// migrate bedrespawn
if (section.isBoolean("bedrespawn")) {
world.setBedRespawn(section.getBoolean("bedrespawn"));
}
// migrate spawn
if (section.isConfigurationSection("spawn")) {
ConfigurationSection spawnSect = section.getConfigurationSection("spawn");
Location spawnLoc = new NullLocation();
if (spawnSect.isDouble("yaw"))
spawnLoc.setYaw((float) spawnSect.getDouble("yaw"));
if (spawnSect.isDouble("pitch"))
spawnLoc.setPitch((float) spawnSect.getDouble("pitch"));
if (spawnSect.isDouble("x"))
spawnLoc.setX(spawnSect.getDouble("x"));
if (spawnSect.isDouble("y"))
spawnLoc.setY(spawnSect.getDouble("y"));
if (spawnSect.isDouble("z"))
spawnLoc.setZ(spawnSect.getDouble("z"));
world.setSpawnLocation(spawnLoc);
}
// migrate difficulty
if (section.isString("difficulty")) {
final Difficulty difficulty = Difficulty.valueOf(section.getString("difficulty").toUpperCase());
if (difficulty != null) {
world.setDifficulty(difficulty);
}
}
// migrate keepspawninmemory
if (section.isBoolean("keepspawninmemory")) {
world.setKeepSpawnInMemory(section.getBoolean("keepspawninmemory"));
}
newValues.put(entry.getKey(), world);
wasChanged = true;
} else {
// huh?
this.log(Level.WARNING, "Removing unknown entry in the config: " + entry);
// just don't add to newValues
wasChanged = true;
}
}
if (wasChanged) {
// clear config
wconf.set("worlds", null);
// and rebuild it
ConfigurationSection rootSection = wconf.createSection("worlds");
for (Map.Entry<String, Object> entry : newValues.entrySet()) {
rootSection.set(entry.getKey(), entry.getValue());
}
try {
wconf.save(new File(getDataFolder(), "worlds.yml"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}