Add multiworld snapshot support

This commit is contained in:
James Robinson 2011-07-07 18:23:12 +01:00
parent 1e9c5b2c76
commit 858f8d3c36
8 changed files with 95 additions and 55 deletions

View File

@ -24,6 +24,7 @@ import java.util.HashSet;
import java.util.Set; import java.util.Set;
import com.sk89q.worldedit.snapshots.SnapshotRepository; import com.sk89q.worldedit.snapshots.SnapshotRepository;
import java.util.Map;
/** /**
* Represents WorldEdit's configuration. * Represents WorldEdit's configuration.
@ -42,7 +43,7 @@ public abstract class LocalConfiguration {
public int defaultChangeLimit = -1; public int defaultChangeLimit = -1;
public int maxChangeLimit = -1; public int maxChangeLimit = -1;
public String shellSaveType = ""; public String shellSaveType = "";
public SnapshotRepository snapshotRepo = null; public Map<String, SnapshotRepository> snapshotRepositories = null;
public int maxRadius = -1; public int maxRadius = -1;
public int maxSuperPickaxeSize = 5; public int maxSuperPickaxeSize = 5;
public int maxBrushRadius = 6; public int maxBrushRadius = 6;

View File

@ -70,7 +70,7 @@ public class LocalSession {
= new HashMap<Integer, Tool>(); = new HashMap<Integer, Tool>();
private int maxBlocksChanged = -1; private int maxBlocksChanged = -1;
private boolean useInventory; private boolean useInventory;
private Snapshot snapshot; private Map<LocalWorld, Snapshot> snapshots = new HashMap();
private String lastScript; private String lastScript;
private boolean beenToldVersion = false; private boolean beenToldVersion = false;
private boolean hasCUISupport = false; private boolean hasCUISupport = false;
@ -410,8 +410,8 @@ public class LocalSession {
* *
* @return the snapshot * @return the snapshot
*/ */
public Snapshot getSnapshot() { public Snapshot getSnapshot(LocalWorld world) {
return snapshot; return snapshots.get(world);
} }
/** /**
@ -419,8 +419,12 @@ public class LocalSession {
* *
* @param snapshot * @param snapshot
*/ */
public void setSnapshot(Snapshot snapshot) { public void setSnapshot(LocalWorld world, Snapshot snapshot) {
this.snapshot = snapshot; if (snapshot == null && snapshots.containsKey(world)) {
snapshots.remove(world);
} else {
snapshots.put(world, snapshot);
}
} }
/** /**

View File

@ -35,6 +35,13 @@ public abstract class LocalWorld {
*/ */
protected Random random = new Random(); protected Random random = new Random();
/**
* Get the name of the world.
*
* @return
*/
public abstract String getName();
/** /**
* Set block type. * Set block type.
* *

View File

@ -30,6 +30,8 @@ import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.LogFormat; import com.sk89q.worldedit.LogFormat;
import com.sk89q.worldedit.snapshots.SnapshotRepository; import com.sk89q.worldedit.snapshots.SnapshotRepository;
import java.io.File;
import java.util.HashMap;
public class BukkitConfiguration extends LocalConfiguration { public class BukkitConfiguration extends LocalConfiguration {
private Configuration config; private Configuration config;
@ -86,10 +88,11 @@ public class BukkitConfiguration extends LocalConfiguration {
LocalSession.EXPIRATION_GRACE = config.getInt("history.expiration", 10) * 60 * 1000; LocalSession.EXPIRATION_GRACE = config.getInt("history.expiration", 10) * 60 * 1000;
String snapshotsDir = config.getString("snapshots.directory", ""); String snapshotsDir = config.getString("snapshots.directory", "");
snapshotRepositories = new HashMap();
if (!snapshotsDir.trim().equals("")) { if (!snapshotsDir.trim().equals("")) {
snapshotRepo = new SnapshotRepository(snapshotsDir); for (File repository : new File(snapshotsDir).listFiles()) {
} else { snapshotRepositories.put(repository.getName(), new SnapshotRepository(repository));
snapshotRepo = null; }
} }
String type = config.getString("shell-save-type", "").trim(); String type = config.getString("shell-save-type", "").trim();

View File

@ -70,6 +70,15 @@ public class BukkitWorld extends LocalWorld {
return world; return world;
} }
/**
* Get the name of the world
*
* @return
*/
public String getName() {
return world.getName();
}
/** /**
* Set block type. * Set block type.
* *

View File

@ -32,6 +32,7 @@ import com.sk89q.minecraft.util.commands.CommandPermissions;
import com.sk89q.worldedit.*; import com.sk89q.worldedit.*;
import com.sk89q.worldedit.snapshots.InvalidSnapshotException; import com.sk89q.worldedit.snapshots.InvalidSnapshotException;
import com.sk89q.worldedit.snapshots.Snapshot; import com.sk89q.worldedit.snapshots.Snapshot;
import com.sk89q.worldedit.snapshots.SnapshotRepository;
/** /**
* Snapshot commands. * Snapshot commands.
@ -59,32 +60,36 @@ public class SnapshotCommands {
int num = args.argsLength() > 0 ? int num = args.argsLength() > 0 ?
Math.min(40, Math.max(5, args.getInteger(0))) : 5; Math.min(40, Math.max(5, args.getInteger(0))) : 5;
if (config.snapshotRepo != null) { String worldName = player.getWorld().getName();
List<Snapshot> snapshots = config.snapshotRepo.getSnapshots(true); SnapshotRepository repo = config.snapshotRepositories.get(worldName);
if (snapshots.size() > 0) { if (repo == null) {
for (byte i = 0; i < Math.min(num, snapshots.size()); i++) { player.printError("Snapshot/backup restore is not configured for this world.");
player.print((i + 1) + ". " + snapshots.get(i).getName()); return;
} }
player.print("Use /snap use [snapshot] or /snap use latest."); List<Snapshot> snapshots = repo.getSnapshots(true);
} else {
player.printError("No snapshots are available. See console for details.");
// Okay, let's toss some debugging information! if (snapshots.size() > 0) {
File dir = config.snapshotRepo.getDirectory(); for (byte i = 0; i < Math.min(num, snapshots.size()); i++) {
player.print((i + 1) + ". " + snapshots.get(i).getName());
try {
logger.info("WorldEdit found no snapshots: looked in: " +
dir.getCanonicalPath());
} catch (IOException e) {
logger.info("WorldEdit found no snapshots: looked in "
+ "(NON-RESOLVABLE PATH - does it exist?): " +
dir.getPath());
}
} }
player.print("Use /snap use [snapshot] or /snap use latest.");
} else { } else {
player.printError("Snapshot/backup restore is not configured."); player.printError("No snapshots are available. See console for details.");
// Okay, let's toss some debugging information!
File dir = repo.getDirectory();
try {
logger.info("WorldEdit found no snapshots: looked in: " +
dir.getCanonicalPath());
} catch (IOException e) {
logger.info("WorldEdit found no snapshots: looked in "
+ "(NON-RESOLVABLE PATH - does it exist?): " +
dir.getPath());
}
} }
} }
@ -101,9 +106,11 @@ public class SnapshotCommands {
throws WorldEditException { throws WorldEditException {
LocalConfiguration config = we.getConfiguration(); LocalConfiguration config = we.getConfiguration();
String worldName = player.getWorld().getName();
SnapshotRepository repo = config.snapshotRepositories.get(worldName);
if (config.snapshotRepo == null) { if (repo == null) {
player.printError("Snapshot/backup restore is not configured."); player.printError("Snapshot/backup restore is not configured for this world.");
return; return;
} }
@ -111,17 +118,17 @@ public class SnapshotCommands {
// Want the latest snapshot? // Want the latest snapshot?
if (name.equalsIgnoreCase("latest")) { if (name.equalsIgnoreCase("latest")) {
Snapshot snapshot = config.snapshotRepo.getDefaultSnapshot(); Snapshot snapshot = repo.getDefaultSnapshot();
if (snapshot != null) { if (snapshot != null) {
session.setSnapshot(null); session.setSnapshot(player.getWorld(), null);
player.print("Now using newest snapshot."); player.print("Now using newest snapshot.");
} else { } else {
player.printError("No snapshots were found."); player.printError("No snapshots were found.");
} }
} else { } else {
try { try {
session.setSnapshot(config.snapshotRepo.getSnapshot(name)); session.setSnapshot(player.getWorld(), repo.getSnapshot(name));
player.print("Snapshot set to: " + name); player.print("Snapshot set to: " + name);
} catch (InvalidSnapshotException e) { } catch (InvalidSnapshotException e) {
player.printError("That snapshot does not exist or is not available."); player.printError("That snapshot does not exist or is not available.");
@ -142,9 +149,11 @@ public class SnapshotCommands {
throws WorldEditException { throws WorldEditException {
LocalConfiguration config = we.getConfiguration(); LocalConfiguration config = we.getConfiguration();
String worldName = player.getWorld().getName();
SnapshotRepository repo = config.snapshotRepositories.get(worldName);
if (config.snapshotRepo == null) { if (repo == null) {
player.printError("Snapshot/backup restore is not configured."); player.printError("Snapshot/backup restore is not configured for this world.");
return; return;
} }
@ -155,12 +164,12 @@ public class SnapshotCommands {
} else { } else {
dateFormat.setTimeZone(session.getTimeZone()); dateFormat.setTimeZone(session.getTimeZone());
Snapshot snapshot = config.snapshotRepo.getSnapshotBefore(date); Snapshot snapshot = repo.getSnapshotBefore(date);
if (snapshot == null) { if (snapshot == null) {
player.printError("Couldn't find a snapshot before " player.printError("Couldn't find a snapshot before "
+ dateFormat.format(date.getTime()) + "."); + dateFormat.format(date.getTime()) + ".");
} else { } else {
session.setSnapshot(snapshot); session.setSnapshot(player.getWorld(), snapshot);
player.print("Snapshot set to: " + snapshot.getName()); player.print("Snapshot set to: " + snapshot.getName());
} }
} }
@ -179,9 +188,11 @@ public class SnapshotCommands {
throws WorldEditException { throws WorldEditException {
LocalConfiguration config = we.getConfiguration(); LocalConfiguration config = we.getConfiguration();
String worldName = player.getWorld().getName();
SnapshotRepository repo = config.snapshotRepositories.get(worldName);
if (config.snapshotRepo == null) { if (repo == null) {
player.printError("Snapshot/backup restore is not configured."); player.printError("Snapshot/backup restore is not configured for this world.");
return; return;
} }
@ -192,12 +203,12 @@ public class SnapshotCommands {
} else { } else {
dateFormat.setTimeZone(session.getTimeZone()); dateFormat.setTimeZone(session.getTimeZone());
Snapshot snapshot = config.snapshotRepo.getSnapshotAfter(date); Snapshot snapshot = repo.getSnapshotAfter(date);
if (snapshot == null) { if (snapshot == null) {
player.printError("Couldn't find a snapshot after " player.printError("Couldn't find a snapshot after "
+ dateFormat.format(date.getTime()) + "."); + dateFormat.format(date.getTime()) + ".");
} else { } else {
session.setSnapshot(snapshot); session.setSnapshot(player.getWorld(), snapshot);
player.print("Snapshot set to: " + snapshot.getName()); player.print("Snapshot set to: " + snapshot.getName());
} }
} }

View File

@ -37,6 +37,7 @@ import com.sk89q.worldedit.data.DataException;
import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.snapshots.InvalidSnapshotException; import com.sk89q.worldedit.snapshots.InvalidSnapshotException;
import com.sk89q.worldedit.snapshots.Snapshot; import com.sk89q.worldedit.snapshots.Snapshot;
import com.sk89q.worldedit.snapshots.SnapshotRepository;
import com.sk89q.worldedit.snapshots.SnapshotRestore; import com.sk89q.worldedit.snapshots.SnapshotRestore;
public class SnapshotUtilCommands { public class SnapshotUtilCommands {
@ -65,9 +66,11 @@ public class SnapshotUtilCommands {
throws WorldEditException { throws WorldEditException {
LocalConfiguration config = we.getConfiguration(); LocalConfiguration config = we.getConfiguration();
String worldName = player.getWorld().getName();
SnapshotRepository repo = config.snapshotRepositories.get(worldName);
if (config.snapshotRepo == null) { if (repo == null) {
player.printError("Snapshot/backup restore is not configured."); player.printError("Snapshot/backup restore is not configured for this world.");
return; return;
} }
@ -76,26 +79,26 @@ public class SnapshotUtilCommands {
if (args.argsLength() > 0) { if (args.argsLength() > 0) {
try { try {
snapshot = config.snapshotRepo.getSnapshot(args.getString(0)); snapshot = repo.getSnapshot(args.getString(0));
} catch (InvalidSnapshotException e) { } catch (InvalidSnapshotException e) {
player.printError("That snapshot does not exist or is not available."); player.printError("That snapshot does not exist or is not available.");
return; return;
} }
} else { } else {
snapshot = session.getSnapshot(); snapshot = session.getSnapshot(player.getWorld());
} }
ChunkStore chunkStore = null; ChunkStore chunkStore = null;
// No snapshot set? // No snapshot set?
if (snapshot == null) { if (snapshot == null) {
snapshot = config.snapshotRepo.getDefaultSnapshot(); snapshot = repo.getDefaultSnapshot();
if (snapshot == null) { if (snapshot == null) {
player.printError("No snapshots were found. See console for details."); player.printError("No snapshots were found. See console for details.");
// Okay, let's toss some debugging information! // Okay, let's toss some debugging information!
File dir = config.snapshotRepo.getDirectory(); File dir = repo.getDirectory();
try { try {
logger.info("WorldEdit found no snapshots: looked in: " + logger.info("WorldEdit found no snapshots: looked in: " +

View File

@ -27,6 +27,7 @@ import com.sk89q.util.StringUtil;
import com.sk89q.worldedit.LocalConfiguration; import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.snapshots.SnapshotRepository; import com.sk89q.worldedit.snapshots.SnapshotRepository;
import java.util.HashMap;
/** /**
* Simple LocalConfiguration that loads settings using * Simple LocalConfiguration that loads settings using
@ -93,10 +94,11 @@ public class PropertiesConfiguration extends LocalConfiguration {
LocalSession.MAX_HISTORY_SIZE = Math.max(15, getInt("history-size", 15)); LocalSession.MAX_HISTORY_SIZE = Math.max(15, getInt("history-size", 15));
String snapshotsDir = getString("snapshots-dir", ""); String snapshotsDir = getString("snapshots-dir", "");
snapshotRepositories = new HashMap();
if (!snapshotsDir.trim().equals("")) { if (!snapshotsDir.trim().equals("")) {
snapshotRepo = new SnapshotRepository(snapshotsDir); for (File repository : new File(snapshotsDir).listFiles()) {
} else { snapshotRepositories.put(repository.getName(), new SnapshotRepository(repository));
snapshotRepo = null; }
} }
OutputStream output = null; OutputStream output = null;