2011-02-19 05:44:57 +00:00
|
|
|
// $Id$
|
|
|
|
/*
|
|
|
|
* WorldEdit
|
2012-01-05 21:38:23 +00:00
|
|
|
* Copyright (C) 2010, 2011 sk89q <http://www.sk89q.com> and contributors
|
2011-02-19 05:44:57 +00:00
|
|
|
*
|
|
|
|
* 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/>.
|
2011-08-05 10:29:49 +00:00
|
|
|
*/
|
2011-02-19 05:44:57 +00:00
|
|
|
|
|
|
|
package com.sk89q.worldedit.commands;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
import com.sk89q.minecraft.util.commands.Command;
|
|
|
|
import com.sk89q.minecraft.util.commands.CommandContext;
|
|
|
|
import com.sk89q.minecraft.util.commands.CommandPermissions;
|
|
|
|
import com.sk89q.minecraft.util.commands.NestedCommand;
|
|
|
|
import com.sk89q.worldedit.EditSession;
|
|
|
|
import com.sk89q.worldedit.LocalConfiguration;
|
|
|
|
import com.sk89q.worldedit.LocalPlayer;
|
|
|
|
import com.sk89q.worldedit.LocalSession;
|
|
|
|
import com.sk89q.worldedit.WorldEdit;
|
|
|
|
import com.sk89q.worldedit.WorldEditException;
|
|
|
|
import com.sk89q.worldedit.data.ChunkStore;
|
|
|
|
import com.sk89q.worldedit.data.DataException;
|
2011-08-05 10:29:49 +00:00
|
|
|
import com.sk89q.worldedit.data.MissingWorldException;
|
2011-02-19 05:44:57 +00:00
|
|
|
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 {
|
2011-08-05 10:29:49 +00:00
|
|
|
|
2011-12-18 07:45:12 +00:00
|
|
|
private static final Logger logger = Logger.getLogger("Minecraft.WorldEdit");
|
|
|
|
|
|
|
|
private final WorldEdit we;
|
|
|
|
|
|
|
|
public SnapshotUtilCommands(WorldEdit we) {
|
|
|
|
this.we = we;
|
|
|
|
}
|
2011-08-05 10:29:49 +00:00
|
|
|
|
2011-09-19 05:14:43 +00:00
|
|
|
@Command(
|
|
|
|
aliases = { "snapshot", "snap" },
|
|
|
|
desc = "Snapshot commands"
|
|
|
|
)
|
|
|
|
@NestedCommand(SnapshotCommands.class)
|
2011-12-18 07:45:12 +00:00
|
|
|
public void snapshot(CommandContext args, LocalSession session, LocalPlayer player,
|
|
|
|
EditSession editSession) throws WorldEditException {
|
2011-02-19 05:44:57 +00:00
|
|
|
}
|
|
|
|
|
2011-09-19 05:14:43 +00:00
|
|
|
@Command(
|
|
|
|
aliases = { "restore", "/restore" },
|
|
|
|
usage = "[snapshot]",
|
|
|
|
desc = "Restore the selection from a snapshot",
|
|
|
|
min = 0,
|
|
|
|
max = 1
|
|
|
|
)
|
|
|
|
@CommandPermissions("worldedit.snapshots.restore")
|
2011-12-18 07:45:12 +00:00
|
|
|
public void restore(CommandContext args, LocalSession session, LocalPlayer player,
|
|
|
|
EditSession editSession) throws WorldEditException {
|
2011-08-05 10:29:49 +00:00
|
|
|
|
2011-02-19 05:44:57 +00:00
|
|
|
LocalConfiguration config = we.getConfiguration();
|
|
|
|
|
|
|
|
if (config.snapshotRepo == null) {
|
|
|
|
player.printError("Snapshot/backup restore is not configured.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-20 01:44:39 +00:00
|
|
|
Region region = session.getSelection(player.getWorld());
|
2011-02-19 05:44:57 +00:00
|
|
|
Snapshot snapshot;
|
|
|
|
|
|
|
|
if (args.argsLength() > 0) {
|
|
|
|
try {
|
|
|
|
snapshot = config.snapshotRepo.getSnapshot(args.getString(0));
|
|
|
|
} catch (InvalidSnapshotException e) {
|
|
|
|
player.printError("That snapshot does not exist or is not available.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
snapshot = session.getSnapshot();
|
|
|
|
}
|
|
|
|
|
|
|
|
// No snapshot set?
|
|
|
|
if (snapshot == null) {
|
2011-08-05 10:29:49 +00:00
|
|
|
try {
|
|
|
|
snapshot = config.snapshotRepo.getDefaultSnapshot(player.getWorld().getName());
|
|
|
|
|
|
|
|
if (snapshot == null) {
|
|
|
|
player.printError("No snapshots were found. See console for details.");
|
|
|
|
|
|
|
|
// Okay, let's toss some debugging information!
|
|
|
|
File dir = config.snapshotRepo.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());
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
2011-02-19 05:44:57 +00:00
|
|
|
}
|
2011-08-05 10:29:49 +00:00
|
|
|
} catch (MissingWorldException ex) {
|
|
|
|
player.printError("No snapshots were found for this world.");
|
2011-02-19 05:44:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-05 10:29:49 +00:00
|
|
|
ChunkStore chunkStore = null;
|
|
|
|
|
2011-02-19 05:44:57 +00:00
|
|
|
// Load chunk store
|
|
|
|
try {
|
|
|
|
chunkStore = snapshot.getChunkStore();
|
|
|
|
player.print("Snapshot '" + snapshot.getName() + "' loaded; now restoring...");
|
|
|
|
} catch (DataException e) {
|
|
|
|
player.printError("Failed to load snapshot: " + e.getMessage());
|
|
|
|
return;
|
|
|
|
} catch (IOException e) {
|
|
|
|
player.printError("Failed to load snapshot: " + e.getMessage());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Restore snapshot
|
|
|
|
SnapshotRestore restore = new SnapshotRestore(chunkStore, region);
|
|
|
|
//player.print(restore.getChunksAffected() + " chunk(s) will be loaded.");
|
|
|
|
|
|
|
|
restore.restore(editSession);
|
|
|
|
|
|
|
|
if (restore.hadTotalFailure()) {
|
|
|
|
String error = restore.getLastErrorMessage();
|
|
|
|
if (error != null) {
|
|
|
|
player.printError("Errors prevented any blocks from being restored.");
|
|
|
|
player.printError("Last error: " + error);
|
|
|
|
} else {
|
|
|
|
player.printError("No chunks could be loaded. (Bad archive?)");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
player.print(String.format("Restored; %d "
|
|
|
|
+ "missing chunks and %d other errors.",
|
|
|
|
restore.getMissingChunks().size(),
|
|
|
|
restore.getErrorChunks().size()));
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
chunkStore.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|