Merge remote-tracking branch 'remotes/pull_117/multiworld-snapshots'

This commit is contained in:
hretsam 2011-08-05 12:29:49 +02:00
commit 990915b23f
17 changed files with 391 additions and 279 deletions

View File

@ -19,11 +19,11 @@
package com.sk89q.worldedit;
import com.sk89q.worldedit.snapshots.SnapshotRepository;
import java.io.File;
import java.util.HashSet;
import java.util.Set;
import com.sk89q.worldedit.snapshots.SnapshotRepository;
/**
* Represents WorldEdit's configuration.

View File

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

View File

@ -86,10 +86,8 @@ public class BukkitConfiguration extends LocalConfiguration {
LocalSession.EXPIRATION_GRACE = config.getInt("history.expiration", 10) * 60 * 1000;
String snapshotsDir = config.getString("snapshots.directory", "");
if (!snapshotsDir.trim().equals("")) {
if (!snapshotsDir.isEmpty()){
snapshotRepo = new SnapshotRepository(snapshotsDir);
} else {
snapshotRepo = null;
}
String type = config.getString("shell-save-type", "").trim();

View File

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

View File

@ -15,8 +15,7 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
*/
package com.sk89q.worldedit.commands;
import java.io.File;
@ -30,6 +29,7 @@ import com.sk89q.minecraft.util.commands.Command;
import com.sk89q.minecraft.util.commands.CommandContext;
import com.sk89q.minecraft.util.commands.CommandPermissions;
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.data.MissingWorldException;
import com.sk89q.worldedit.snapshots.InvalidSnapshotException;
import com.sk89q.worldedit.snapshots.Snapshot;
@ -39,16 +39,15 @@ import com.sk89q.worldedit.snapshots.Snapshot;
* @author sk89q
*/
public class SnapshotCommands {
private static Logger logger = Logger.getLogger("Minecraft.WorldEdit");
private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
@Command(
aliases = {"list"},
@Command(aliases = {"list"},
usage = "[num]",
desc = "List snapshots",
min = 0,
max = 1
)
max = 1)
@CommandPermissions({"worldedit.snapshots.list"})
public static void list(CommandContext args, WorldEdit we,
LocalSession session, LocalPlayer player, EditSession editSession)
@ -56,14 +55,20 @@ public class SnapshotCommands {
LocalConfiguration config = we.getConfiguration();
int num = args.argsLength() > 0 ?
Math.min(40, Math.max(5, args.getInteger(0))) : 5;
if (config.snapshotRepo == null) {
player.printError("Snapshot/backup restore is not configured.");
return;
}
if (config.snapshotRepo != null) {
List<Snapshot> snapshots = config.snapshotRepo.getSnapshots(true);
try {
List<Snapshot> snapshots = config.snapshotRepo.getSnapshots(true, player.getWorld().getName());
if (snapshots.size() > 0) {
for (byte i = 0; i < Math.min(num, snapshots.size()); ++i) {
int num = args.argsLength() > 0 ? Math.min(40, Math.max(5, args.getInteger(0))) : 5;
player.print("Snapshots for world: '" + player.getWorld().getName() + "'");
for (byte i = 0; i < Math.min(num, snapshots.size()); i++) {
player.print((i + 1) + ". " + snapshots.get(i).getName());
}
@ -75,26 +80,24 @@ public class SnapshotCommands {
File dir = config.snapshotRepo.getDirectory();
try {
logger.info("WorldEdit found no snapshots: looked in: " +
dir.getCanonicalPath());
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());
+ "(NON-RESOLVABLE PATH - does it exist?): "
+ dir.getPath());
}
}
} else {
player.printError("Snapshot/backup restore is not configured.");
} catch (MissingWorldException ex) {
player.printError("No snapshots were found for this world.");
}
}
@Command(
aliases = {"use"},
@Command(aliases = {"use"},
usage = "<snapshot>",
desc = "Choose a snapshot to use",
min = 1,
max = 1
)
max = 1)
@CommandPermissions({"worldedit.snapshots.restore"})
public static void use(CommandContext args, WorldEdit we,
LocalSession session, LocalPlayer player, EditSession editSession)
@ -111,7 +114,8 @@ public class SnapshotCommands {
// Want the latest snapshot?
if (name.equalsIgnoreCase("latest")) {
Snapshot snapshot = config.snapshotRepo.getDefaultSnapshot();
try {
Snapshot snapshot = config.snapshotRepo.getDefaultSnapshot(player.getWorld().getName());
if (snapshot != null) {
session.setSnapshot(null);
@ -119,6 +123,9 @@ public class SnapshotCommands {
} else {
player.printError("No snapshots were found.");
}
} catch (MissingWorldException ex) {
player.printError("No snapshots were found for this world.");
}
} else {
try {
session.setSnapshot(config.snapshotRepo.getSnapshot(name));
@ -129,13 +136,11 @@ public class SnapshotCommands {
}
}
@Command(
aliases = {"before"},
@Command(aliases = {"before"},
usage = "<date>",
desc = "Choose the nearest snapshot before a date",
min = 1,
max = -1
)
max = -1)
@CommandPermissions({"worldedit.snapshots.restore"})
public static void before(CommandContext args, WorldEdit we,
LocalSession session, LocalPlayer player, EditSession editSession)
@ -153,26 +158,28 @@ public class SnapshotCommands {
if (date == null) {
player.printError("Could not detect the date inputted.");
} else {
dateFormat.setTimeZone(session.getTimeZone());
try {
Snapshot snapshot = config.snapshotRepo.getSnapshotBefore(date, player.getWorld().getName());
Snapshot snapshot = config.snapshotRepo.getSnapshotBefore(date);
if (snapshot == null) {
dateFormat.setTimeZone(session.getTimeZone());
player.printError("Couldn't find a snapshot before "
+ dateFormat.format(date.getTime()) + ".");
} else {
session.setSnapshot(snapshot);
player.print("Snapshot set to: " + snapshot.getName());
}
} catch (MissingWorldException ex) {
player.printError("No snapshots were found for this world.");
}
}
}
@Command(
aliases = {"after"},
@Command(aliases = {"after"},
usage = "<date>",
desc = "Choose the nearest snapshot after a date",
min = 1,
max = -1
)
max = -1)
@CommandPermissions({"worldedit.snapshots.restore"})
public static void after(CommandContext args, WorldEdit we,
LocalSession session, LocalPlayer player, EditSession editSession)
@ -190,16 +197,19 @@ public class SnapshotCommands {
if (date == null) {
player.printError("Could not detect the date inputted.");
} else {
dateFormat.setTimeZone(session.getTimeZone());
Snapshot snapshot = config.snapshotRepo.getSnapshotAfter(date);
try {
Snapshot snapshot = config.snapshotRepo.getSnapshotAfter(date, player.getWorld().getName());
if (snapshot == null) {
dateFormat.setTimeZone(session.getTimeZone());
player.printError("Couldn't find a snapshot after "
+ dateFormat.format(date.getTime()) + ".");
} else {
session.setSnapshot(snapshot);
player.print("Snapshot set to: " + snapshot.getName());
}
} catch (MissingWorldException ex) {
player.printError("No snapshots were found for this world.");
}
}
}
}

View File

@ -15,7 +15,7 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
*/
package com.sk89q.worldedit.commands;
@ -34,31 +34,29 @@ import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.data.ChunkStore;
import com.sk89q.worldedit.data.DataException;
import com.sk89q.worldedit.data.MissingWorldException;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.snapshots.InvalidSnapshotException;
import com.sk89q.worldedit.snapshots.Snapshot;
import com.sk89q.worldedit.snapshots.SnapshotRestore;
public class SnapshotUtilCommands {
private static Logger logger = Logger.getLogger("Minecraft.WorldEdit");
@Command(
aliases = {"snapshot", "snap"},
desc = "Snapshot commands"
)
@Command(aliases = {"snapshot", "snap"},
desc = "Snapshot commands")
@NestedCommand({SnapshotCommands.class})
public static void snapshot(CommandContext args, WorldEdit we,
LocalSession session, LocalPlayer player, EditSession editSession)
throws WorldEditException {
}
@Command(
aliases = {"restore", "/restore"},
@Command(aliases = {"restore", "/restore"},
usage = "[snapshot]",
desc = "Restore the selection from a snapshot",
min = 0,
max = 1
)
max = 1)
@CommandPermissions({"worldedit.snapshots.restore"})
public static void restore(CommandContext args, WorldEdit we,
LocalSession session, LocalPlayer player, EditSession editSession)
@ -85,11 +83,10 @@ public class SnapshotUtilCommands {
snapshot = session.getSnapshot();
}
ChunkStore chunkStore = null;
// No snapshot set?
if (snapshot == null) {
snapshot = config.snapshotRepo.getDefaultSnapshot();
try {
snapshot = config.snapshotRepo.getDefaultSnapshot(player.getWorld().getName());
if (snapshot == null) {
player.printError("No snapshots were found. See console for details.");
@ -98,17 +95,23 @@ public class SnapshotUtilCommands {
File dir = config.snapshotRepo.getDirectory();
try {
logger.info("WorldEdit found no snapshots: looked in: " +
dir.getCanonicalPath());
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());
+ "(NON-RESOLVABLE PATH - does it exist?): "
+ dir.getPath());
}
return;
}
} catch (MissingWorldException ex) {
player.printError("No snapshots were found for this world.");
return;
}
}
ChunkStore chunkStore = null;
// Load chunk store
try {

View File

@ -50,7 +50,7 @@ public abstract class ChunkStore {
* @throws DataException
* @throws IOException
*/
public abstract CompoundTag getChunkTag(Vector2D pos)
public abstract CompoundTag getChunkTag(Vector2D pos, String world)
throws DataException, IOException;
/**
@ -62,9 +62,9 @@ public abstract class ChunkStore {
* @throws IOException
* @throws DataException
*/
public Chunk getChunk(Vector2D pos)
public Chunk getChunk(Vector2D pos, String world)
throws DataException, IOException {
return new Chunk(getChunkTag(pos));
return new Chunk(getChunkTag(pos, world));
}
/**

View File

@ -42,13 +42,16 @@ public class FileMcRegionChunkStore extends McRegionChunkStore {
}
@Override
protected InputStream getInputStream(String name) throws IOException,
protected InputStream getInputStream(String name, String world) throws IOException,
DataException {
String file = "region" + File.separator + name;
String fileName = "region" + File.separator + name;
File file = new File(path, fileName);
if (!file.exists()) {
file = new File(path, "DIM-1" + File.separator + fileName);
}
try {
return new FileInputStream(new File(path, file));
return new FileInputStream(file);
} catch (FileNotFoundException e) {
throw new MissingChunkException();
}
@ -56,7 +59,8 @@ public class FileMcRegionChunkStore extends McRegionChunkStore {
@Override
public boolean isValid() {
return new File(path, "region").isDirectory();
return new File(path, "region").isDirectory() ||
new File(path, "DIM-1" + File.separator + "region").isDirectory();
}
}

View File

@ -74,7 +74,7 @@ public abstract class LegacyChunkStore extends ChunkStore {
* @throws IOException
*/
@Override
public CompoundTag getChunkTag(Vector2D pos)
public CompoundTag getChunkTag(Vector2D pos, String world)
throws DataException, IOException {
int x = pos.getBlockX();
int z = pos.getBlockZ();

View File

@ -46,7 +46,7 @@ public abstract class McRegionChunkStore extends ChunkStore {
return filename;
}
protected McRegionReader getReader(Vector2D pos) throws DataException, IOException {
protected McRegionReader getReader(Vector2D pos, String worldname) throws DataException, IOException {
String filename = getFilename(pos);
if (curFilename != null) {
if (curFilename.equals(filename)) {
@ -58,17 +58,17 @@ public abstract class McRegionChunkStore extends ChunkStore {
}
}
}
InputStream stream = getInputStream(filename);
InputStream stream = getInputStream(filename, worldname);
cachedReader = new McRegionReader(stream);
//curFilename = filename;
return cachedReader;
}
@Override
public CompoundTag getChunkTag(Vector2D pos) throws DataException,
public CompoundTag getChunkTag(Vector2D pos, String worldname) throws DataException,
IOException {
McRegionReader reader = getReader(pos);
McRegionReader reader = getReader(pos, worldname);
InputStream stream = reader.getChunkInputStream(pos);
NBTInputStream nbt = new NBTInputStream(stream);
Tag tag;
@ -113,7 +113,7 @@ public abstract class McRegionChunkStore extends ChunkStore {
* @return
* @throws IOException
*/
protected abstract InputStream getInputStream(String name)
protected abstract InputStream getInputStream(String name, String worldname)
throws IOException, DataException;

View File

@ -0,0 +1,54 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.data;
/**
*
* @author sk89q
*/
public class MissingWorldException extends ChunkStoreException {
private static final long serialVersionUID = 6487395784195658467L;
private String worldname;
public MissingWorldException() {
super();
}
public MissingWorldException(String worldname) {
super();
this.worldname = worldname;
}
public MissingWorldException(String msg, String worldname) {
super(msg);
this.worldname = worldname;
}
/**
* Get name of the world in question. May be null if unknown.
*
* @return
*/
public String getWorldname() {
return worldname;
}
}

View File

@ -15,8 +15,7 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
*/
package com.sk89q.worldedit.data;
import java.io.*;
@ -32,6 +31,7 @@ import de.schlichtherle.util.zip.*;
* @author sk89q
*/
public class TrueZipMcRegionChunkStore extends McRegionChunkStore {
/**
* ZIP file.
*/
@ -88,54 +88,46 @@ public class TrueZipMcRegionChunkStore extends McRegionChunkStore {
*/
@Override
@SuppressWarnings("unchecked")
protected InputStream getInputStream(String name)
protected InputStream getInputStream(String name, String worldname)
throws IOException, DataException {
String file = "region/" + name;
// Detect subfolder for the world's files
if (folder != null) {
if (!folder.equals("")) {
file = folder + "/" + file;
name = folder + "/" + name;
}
} else {
ZipEntry testEntry = zip.getEntry("level.dat");
// So, the data is not in the root directory
if (testEntry == null) {
// Let's try a world/ sub-directory
testEntry = getEntry("world/level.dat");
Pattern pattern = Pattern.compile(".*[\\\\/]level\\.dat$");
// So not there either...
if (testEntry == null) {
Pattern pattern = Pattern.compile(".*\\.mcr$");
// World pattern
Pattern worldPattern = Pattern.compile(worldname + "\\$");
for (Enumeration<? extends ZipEntry> e = zip.entries();
e.hasMoreElements(); ) {
testEntry = e.nextElement();
// Whoo, found level.dat!
e.hasMoreElements();) {
ZipEntry testEntry = (ZipEntry) e.nextElement();
// Check for world
if (worldPattern.matcher(worldname).matches()) {
// Check for file
if (pattern.matcher(testEntry.getName()).matches()) {
folder = testEntry.getName().replaceAll("level\\.dat$", "");
folder = folder.substring(0, folder.length() - 1);
file = folder + file;
folder = testEntry.getName().substring(0, testEntry.getName().lastIndexOf("/"));
name = folder + "/" + name;
break;
}
}
} else {
file = "world/" + file;
}
// Check if world is found
if (folder == null) {
throw new MissingWorldException("Target world is not present in ZIP.", worldname);
}
}
ZipEntry entry = getEntry(file);
ZipEntry entry = getEntry(name);
if (entry == null) {
throw new MissingChunkException();
}
try {
return zip.getInputStream(entry);
} catch (ZipException e) {
throw new IOException("Failed to read " + file + " in ZIP");
throw new IOException("Failed to read " + name + " in ZIP");
}
}
@ -167,7 +159,7 @@ public class TrueZipMcRegionChunkStore extends McRegionChunkStore {
@SuppressWarnings("unchecked")
public boolean isValid() {
for (Enumeration<? extends ZipEntry> e = zip.entries();
e.hasMoreElements(); ) {
e.hasMoreElements();) {
ZipEntry testEntry = e.nextElement();

View File

@ -15,8 +15,7 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
*/
package com.sk89q.worldedit.data;
import java.io.*;
@ -30,6 +29,7 @@ import java.util.Enumeration;
* @author sk89q
*/
public class ZippedMcRegionChunkStore extends McRegionChunkStore {
/**
* ZIP file.
*/
@ -85,54 +85,44 @@ public class ZippedMcRegionChunkStore extends McRegionChunkStore {
* @throws DataException
*/
@Override
protected InputStream getInputStream(String name)
protected InputStream getInputStream(String name, String worldname)
throws IOException, DataException {
String file = "region/" + name;
// Detect subfolder for the world's files
if (folder != null) {
if (!folder.equals("")) {
file = folder + "/" + file;
name = folder + "/" + name;
}
} else {
ZipEntry testEntry = zip.getEntry("level.dat");
// So, the data is not in the root directory
if (testEntry == null) {
// Let's try a world/ sub-directory
testEntry = getEntry("world/level.dat");
Pattern pattern = Pattern.compile(".*[\\\\/]level\\.dat$");
// So not there either...
if (testEntry == null) {
Pattern pattern = Pattern.compile(".*\\.mcr$");
for (Enumeration<? extends ZipEntry> e = zip.entries();
e.hasMoreElements(); ) {
testEntry = (ZipEntry)e.nextElement();
// Whoo, found level.dat!
e.hasMoreElements();) {
ZipEntry testEntry = (ZipEntry) e.nextElement();
// Check for world
if (testEntry.getName().startsWith(worldname + "/")) {
if (pattern.matcher(testEntry.getName()).matches()) {
folder = testEntry.getName().replaceAll("level\\.dat$", "");
folder = folder.substring(0, folder.length() - 1);
file = folder + file;
folder = testEntry.getName().substring(0, testEntry.getName().lastIndexOf("/"));
name = folder + "/" + name;
break;
}
}
} else {
file = "world/" + file;
}
}
}
ZipEntry entry = getEntry(file);
// Check if world is found
if (folder == null) {
throw new MissingWorldException("Target world is not present in ZIP.", worldname);
}
}
ZipEntry entry = getEntry(name);
if (entry == null) {
throw new MissingChunkException();
}
try {
return zip.getInputStream(entry);
} catch (ZipException e) {
throw new IOException("Failed to read " + file + " in ZIP");
throw new IOException("Failed to read " + name + " in ZIP");
}
}
@ -163,7 +153,7 @@ public class ZippedMcRegionChunkStore extends McRegionChunkStore {
@Override
public boolean isValid() {
for (Enumeration<? extends ZipEntry> e = zip.entries();
e.hasMoreElements(); ) {
e.hasMoreElements();) {
ZipEntry testEntry = e.nextElement();

View File

@ -15,22 +15,22 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
*/
package com.sk89q.worldedit.snapshots;
import com.sk89q.worldedit.data.*;
import java.io.*;
import java.util.Calendar;
import java.util.logging.Logger;
import java.util.zip.ZipFile;
/**
*
* @author sk89q
*/
public class Snapshot implements Comparable<Snapshot> {
protected static Logger logger = Logger.getLogger("Minecraft.WorldEdit");
protected static Logger logger = Logger.getLogger("Minecraft.WorldEdit");
/**
* Stores snapshot file.
*/
@ -122,6 +122,40 @@ public class Snapshot implements Comparable<Snapshot> {
}
}
/**
* Check the zip/tar file it contains the given world.
*
* @return true if the zip/tar file contains the given world
*/
public boolean containsWorld(String worldname) {
try {
if (file.getName().toLowerCase().endsWith(".zip")) {
ZipFile entry = new ZipFile(file);
return entry.getEntry(worldname) != null;
} else if (file.getName().toLowerCase().endsWith(".tar.bz2")
|| file.getName().toLowerCase().endsWith(".tar.gz")
|| file.getName().toLowerCase().endsWith(".tar")) {
try {
de.schlichtherle.util.zip.ZipFile entry = new de.schlichtherle.util.zip.ZipFile(file);
return entry.getEntry(worldname) != null;
} catch (NoClassDefFoundError e) {
throw new DataException("TrueZIP is required for .tar support");
}
} else {
return (file.getName().equalsIgnoreCase(worldname));
}
} catch (IOException ex) {
// Skip the file, but print an error
logger.info("Could not load snapshot: "
+ file.getPath());
} catch (DataException ex) {
// No truezip, so tar file not supported.
// Dont print, just skip the file.
}
return false;
}
/**
* Get the snapshot's name.
*
@ -160,7 +194,9 @@ public class Snapshot implements Comparable<Snapshot> {
public int compareTo(Snapshot o) {
if (o.date == null || date == null) {
return name.compareTo(o.name);
// Remove the folder from the name
int i = name.indexOf("/"), j = o.name.indexOf("/");
return name.substring((i > 0 ? 0 : i)).compareTo(o.name.substring((j > 0 ? 0 : j)));
} else {
return date.compareTo(o.date);
}

View File

@ -15,10 +15,10 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
*/
package com.sk89q.worldedit.snapshots;
import com.sk89q.worldedit.data.MissingWorldException;
import java.io.*;
import java.util.ArrayList;
import java.util.Calendar;
@ -30,16 +30,15 @@ import java.util.List;
* @author sk89q
*/
public class SnapshotRepository {
/**
* Stores the directory the snapshots come from.
*/
protected File dir;
/**
* List of date parsers.
*/
protected List<SnapshotDateParser> dateParsers
= new ArrayList<SnapshotDateParser>();
protected List<SnapshotDateParser> dateParsers = new ArrayList<SnapshotDateParser>();
/**
* Create a new instance of a repository.
@ -48,6 +47,8 @@ public class SnapshotRepository {
*/
public SnapshotRepository(File dir) {
this.dir = dir;
// If folder dont exist, make it.
dir.mkdirs();
dateParsers.add(new YYMMDDHHIISSParser());
dateParsers.add(new ModificationTimerParser());
@ -69,7 +70,7 @@ public class SnapshotRepository {
* @param newestFirst
* @return
*/
public List<Snapshot> getSnapshots(boolean newestFirst) {
public List<Snapshot> getSnapshots(boolean newestFirst, String worldname) throws MissingWorldException {
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
File f = new File(dir, name);
@ -77,14 +78,24 @@ public class SnapshotRepository {
}
};
String[] snapshotNames = dir.list(filter);
List<Snapshot> list = new ArrayList<Snapshot>(snapshotNames.length);
File[] snapshotFiles = dir.listFiles();
List<Snapshot> list = new ArrayList<Snapshot>(snapshotFiles.length);
for (String name : snapshotNames) {
Snapshot snapshot = new Snapshot(this, name);
for (File file : snapshotFiles) {
if (isValidSnapshot(file)) {
Snapshot snapshot = new Snapshot(this, file.getName());
if (snapshot.containsWorld(worldname)) {
detectDate(snapshot);
list.add(snapshot);
}
} else if (file.isDirectory() && file.getName().equalsIgnoreCase(worldname)) {
for (String name : file.list(filter)) {
Snapshot snapshot = new Snapshot(this, file.getName() + "/" + name);
detectDate(snapshot);
list.add(snapshot);
}
}
}
if (newestFirst) {
Collections.sort(list, Collections.reverseOrder());
@ -101,8 +112,8 @@ public class SnapshotRepository {
* @param date
* @return
*/
public Snapshot getSnapshotAfter(Calendar date) {
List<Snapshot> snapshots = getSnapshots(true);
public Snapshot getSnapshotAfter(Calendar date, String world) throws MissingWorldException {
List<Snapshot> snapshots = getSnapshots(true, world);
Snapshot last = null;
for (Snapshot snapshot : snapshots) {
@ -123,8 +134,8 @@ public class SnapshotRepository {
* @param date
* @return
*/
public Snapshot getSnapshotBefore(Calendar date) {
List<Snapshot> snapshots = getSnapshots(false);
public Snapshot getSnapshotBefore(Calendar date, String world) throws MissingWorldException {
List<Snapshot> snapshots = getSnapshots(false, world);
Snapshot last = null;
for (Snapshot snapshot : snapshots) {
@ -160,8 +171,8 @@ public class SnapshotRepository {
*
* @return
*/
public Snapshot getDefaultSnapshot() {
List<Snapshot> snapshots = getSnapshots(true);
public Snapshot getDefaultSnapshot(String world) throws MissingWorldException {
List<Snapshot> snapshots = getSnapshots(true, world);
if (snapshots.size() == 0) {
return null;
@ -186,18 +197,16 @@ public class SnapshotRepository {
* @param f
* @return whether it is a valid snapshot
*/
public boolean isValidSnapshot(File f) {
protected boolean isValidSnapshot(File f) {
if (!f.getName().matches("^[A-Za-z0-9_\\- \\./\\\\'\\$@~!%\\^\\*\\(\\)\\[\\]\\+\\{\\},\\?]+$")) {
return false;
}
return (f.isDirectory() && (new File(f, "level.dat")).exists())
|| (f.isFile() && (
f.getName().toLowerCase().endsWith(".zip")
|| (f.isFile() && (f.getName().toLowerCase().endsWith(".zip")
|| f.getName().toLowerCase().endsWith(".tar.bz2")
|| f.getName().toLowerCase().endsWith(".tar.gz")
|| f.getName().toLowerCase().endsWith(".tar")
));
|| f.getName().toLowerCase().endsWith(".tar")));
}
/**

View File

@ -147,7 +147,7 @@ public class SnapshotRestore {
Chunk chunk;
try {
chunk = chunkStore.getChunk(chunkPos);
chunk = chunkStore.getChunk(chunkPos, editSession.getWorld().getName());
// Good, the chunk could be at least loaded
// Now just copy blocks!
@ -157,6 +157,9 @@ public class SnapshotRestore {
}
} catch (MissingChunkException me) {
missingChunks.add(chunkPos);
} catch (MissingWorldException me) {
errorChunks.add(chunkPos);
lastErrorMessage = me.getMessage();
} catch (DataException de) {
errorChunks.add(chunkPos);
lastErrorMessage = de.getMessage();

View File

@ -15,8 +15,7 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
*/
package com.sk89q.worldedit.util;
import java.io.*;
@ -35,6 +34,7 @@ import com.sk89q.worldedit.snapshots.SnapshotRepository;
* @author sk89q
*/
public class PropertiesConfiguration extends LocalConfiguration {
protected Properties properties;
protected File path;
@ -93,10 +93,8 @@ public class PropertiesConfiguration extends LocalConfiguration {
LocalSession.MAX_HISTORY_SIZE = Math.max(15, getInt("history-size", 15));
String snapshotsDir = getString("snapshots-dir", "");
if (!snapshotsDir.trim().equals("")) {
if (!snapshotsDir.isEmpty()) {
snapshotRepo = new SnapshotRepository(snapshotsDir);
} else {
snapshotRepo = null;
}
OutputStream output = null;
@ -229,5 +227,4 @@ public class PropertiesConfiguration extends LocalConfiguration {
return set;
}
}
}