Added a storage migration class.

This commit is contained in:
Tobias Ottenweller 2013-05-26 12:36:25 +02:00
parent c3a24a940f
commit d9194c7497
2 changed files with 45 additions and 1 deletions

View File

@ -20,7 +20,9 @@ public class GatesManager
{
private File gatesConfigFile;
private FileConfiguration gatesConfig;
private String gatesPath = "gates"; // path to gates inside the yaml file
private static final String gatesPath = "gates"; // path to gates inside the yaml file
private static final String storageVersionPath = "version";
private static final int storageVersion = 1;
private Map<String, Gate> gatesById;
private Map<SimpleChunk, Set<Gate>> gatesByChunk;
@ -52,6 +54,7 @@ public class GatesManager
public void saveGatesToDisk()
{
gatesConfig.set(gatesPath, new ArrayList<Object>(gatesById.values()));
gatesConfig.set(storageVersionPath, storageVersion);
try {
gatesConfig.save(gatesConfigFile);
@ -95,6 +98,14 @@ public class GatesManager
fillGatesByLocation();
Plugin.log("Loaded " + this.gates.size() + " gates.");
// migration
int fileStorageVersion = gatesConfig.getInt(storageVersionPath);
if (fileStorageVersion < storageVersion) {
Plugin.log("Outdated storage version detected. Performing data migration...");
MigrationUtil.performMigration(fileStorageVersion, storageVersion, this.gates);
}
}

View File

@ -0,0 +1,33 @@
package de.craftinc.gates;
import de.craftinc.gates.Gate;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import java.util.List;
public class MigrationUtil
{
public static void performMigration(int storageVersion, int currentVersion, List<Gate> gates)
{
if (currentVersion == 1 && storageVersion == 0) {
for (Gate g : gates) {
for (Location l : g.getGateBlockLocations()) {
Block b = l.getBlock();
if (b.getType() == Material.PORTAL) {
b.setType(Material.AIR);
}
}
}
}
else {
throw new IllegalArgumentException("Supplied storage version is currently not supported! Make sure you have the latest version of Craft Inc. Gates installed.");
}
}
}