2011-01-29 10:05:22 +00:00
|
|
|
// $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/>.
|
2011-08-05 10:29:49 +00:00
|
|
|
*/
|
2011-01-29 10:05:22 +00:00
|
|
|
package com.sk89q.worldedit.commands;
|
|
|
|
|
2011-01-30 05:04:07 +00:00
|
|
|
import java.io.File;
|
2011-01-29 10:05:22 +00:00
|
|
|
import java.io.IOException;
|
2011-03-14 03:23:55 +00:00
|
|
|
import java.text.DateFormat;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.List;
|
2011-01-30 05:04:07 +00:00
|
|
|
import java.util.logging.Logger;
|
2011-02-18 06:53:44 +00:00
|
|
|
import com.sk89q.minecraft.util.commands.Command;
|
|
|
|
import com.sk89q.minecraft.util.commands.CommandContext;
|
|
|
|
import com.sk89q.minecraft.util.commands.CommandPermissions;
|
2011-01-29 10:05:22 +00:00
|
|
|
import com.sk89q.worldedit.*;
|
2011-08-05 10:29:49 +00:00
|
|
|
import com.sk89q.worldedit.data.MissingWorldException;
|
2011-01-29 10:05:22 +00:00
|
|
|
import com.sk89q.worldedit.snapshots.InvalidSnapshotException;
|
|
|
|
import com.sk89q.worldedit.snapshots.Snapshot;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Snapshot commands.
|
|
|
|
*
|
|
|
|
* @author sk89q
|
|
|
|
*/
|
|
|
|
public class SnapshotCommands {
|
2011-01-30 05:04:07 +00:00
|
|
|
private static Logger logger = Logger.getLogger("Minecraft.WorldEdit");
|
2011-03-14 03:23:55 +00:00
|
|
|
private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
|
2011-08-05 10:29:49 +00:00
|
|
|
|
|
|
|
@Command(aliases = {"list"},
|
|
|
|
usage = "[num]",
|
|
|
|
desc = "List snapshots",
|
|
|
|
min = 0,
|
|
|
|
max = 1)
|
2011-01-29 10:05:22 +00:00
|
|
|
@CommandPermissions({"worldedit.snapshots.list"})
|
|
|
|
public static void list(CommandContext args, WorldEdit we,
|
|
|
|
LocalSession session, LocalPlayer player, EditSession editSession)
|
|
|
|
throws WorldEditException {
|
2011-08-05 10:29:49 +00:00
|
|
|
|
2011-01-29 10:05:22 +00:00
|
|
|
LocalConfiguration config = we.getConfiguration();
|
|
|
|
|
2011-08-05 10:29:49 +00:00
|
|
|
if (config.snapshotRepo == null) {
|
|
|
|
player.printError("Snapshot/backup restore is not configured.");
|
2011-07-07 17:23:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-01-29 10:05:22 +00:00
|
|
|
|
2011-08-05 10:29:49 +00:00
|
|
|
try {
|
|
|
|
List<Snapshot> snapshots = config.snapshotRepo.getSnapshots(true, player.getWorld().getName());
|
2011-01-29 10:05:22 +00:00
|
|
|
|
2011-03-14 03:23:55 +00:00
|
|
|
if (snapshots.size() > 0) {
|
2011-08-05 10:29:49 +00:00
|
|
|
|
|
|
|
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++) {
|
2011-03-14 03:23:55 +00:00
|
|
|
player.print((i + 1) + ". " + snapshots.get(i).getName());
|
2011-01-29 10:05:22 +00:00
|
|
|
}
|
|
|
|
|
2011-02-19 05:44:57 +00:00
|
|
|
player.print("Use /snap use [snapshot] or /snap use latest.");
|
2011-01-29 10:05:22 +00:00
|
|
|
} else {
|
2011-01-30 05:04:07 +00:00
|
|
|
player.printError("No snapshots are available. See console for details.");
|
2011-07-07 17:23:12 +00:00
|
|
|
|
2011-01-30 05:04:07 +00:00
|
|
|
// Okay, let's toss some debugging information!
|
|
|
|
File dir = config.snapshotRepo.getDirectory();
|
2011-08-05 10:29:49 +00:00
|
|
|
|
2011-01-30 05:04:07 +00:00
|
|
|
try {
|
2011-08-05 10:29:49 +00:00
|
|
|
logger.info("WorldEdit found no snapshots: looked in: "
|
|
|
|
+ dir.getCanonicalPath());
|
2011-01-30 05:04:07 +00:00
|
|
|
} catch (IOException e) {
|
|
|
|
logger.info("WorldEdit found no snapshots: looked in "
|
2011-08-05 10:29:49 +00:00
|
|
|
+ "(NON-RESOLVABLE PATH - does it exist?): "
|
|
|
|
+ dir.getPath());
|
2011-01-30 05:04:07 +00:00
|
|
|
}
|
2011-01-29 10:05:22 +00:00
|
|
|
}
|
2011-08-05 10:29:49 +00:00
|
|
|
} catch (MissingWorldException ex) {
|
|
|
|
player.printError("No snapshots were found for this world.");
|
2011-01-29 10:05:22 +00:00
|
|
|
}
|
|
|
|
}
|
2011-08-05 10:29:49 +00:00
|
|
|
|
|
|
|
@Command(aliases = {"use"},
|
|
|
|
usage = "<snapshot>",
|
|
|
|
desc = "Choose a snapshot to use",
|
|
|
|
min = 1,
|
|
|
|
max = 1)
|
2011-01-29 10:05:22 +00:00
|
|
|
@CommandPermissions({"worldedit.snapshots.restore"})
|
|
|
|
public static void use(CommandContext args, WorldEdit we,
|
|
|
|
LocalSession session, LocalPlayer player, EditSession editSession)
|
|
|
|
throws WorldEditException {
|
2011-08-05 10:29:49 +00:00
|
|
|
|
2011-01-29 10:05:22 +00:00
|
|
|
LocalConfiguration config = we.getConfiguration();
|
2011-08-05 10:29:49 +00:00
|
|
|
|
2011-01-29 10:05:22 +00:00
|
|
|
if (config.snapshotRepo == null) {
|
|
|
|
player.printError("Snapshot/backup restore is not configured.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
String name = args.getString(0);
|
|
|
|
|
|
|
|
// Want the latest snapshot?
|
|
|
|
if (name.equalsIgnoreCase("latest")) {
|
2011-08-05 10:29:49 +00:00
|
|
|
try {
|
|
|
|
Snapshot snapshot = config.snapshotRepo.getDefaultSnapshot(player.getWorld().getName());
|
2011-01-29 10:05:22 +00:00
|
|
|
|
2011-08-05 10:29:49 +00:00
|
|
|
if (snapshot != null) {
|
|
|
|
session.setSnapshot(null);
|
|
|
|
player.print("Now using newest snapshot.");
|
|
|
|
} else {
|
|
|
|
player.printError("No snapshots were found.");
|
|
|
|
}
|
|
|
|
} catch (MissingWorldException ex) {
|
|
|
|
player.printError("No snapshots were found for this world.");
|
2011-01-29 10:05:22 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
session.setSnapshot(config.snapshotRepo.getSnapshot(name));
|
|
|
|
player.print("Snapshot set to: " + name);
|
|
|
|
} catch (InvalidSnapshotException e) {
|
|
|
|
player.printError("That snapshot does not exist or is not available.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-05 10:29:49 +00:00
|
|
|
|
|
|
|
@Command(aliases = {"before"},
|
|
|
|
usage = "<date>",
|
|
|
|
desc = "Choose the nearest snapshot before a date",
|
|
|
|
min = 1,
|
|
|
|
max = -1)
|
2011-03-14 03:23:55 +00:00
|
|
|
@CommandPermissions({"worldedit.snapshots.restore"})
|
|
|
|
public static void before(CommandContext args, WorldEdit we,
|
|
|
|
LocalSession session, LocalPlayer player, EditSession editSession)
|
|
|
|
throws WorldEditException {
|
2011-08-05 10:29:49 +00:00
|
|
|
|
2011-03-14 03:23:55 +00:00
|
|
|
LocalConfiguration config = we.getConfiguration();
|
2011-08-05 10:29:49 +00:00
|
|
|
|
2011-03-14 03:23:55 +00:00
|
|
|
if (config.snapshotRepo == null) {
|
|
|
|
player.printError("Snapshot/backup restore is not configured.");
|
|
|
|
return;
|
|
|
|
}
|
2011-08-05 10:29:49 +00:00
|
|
|
|
2011-03-14 03:23:55 +00:00
|
|
|
Calendar date = session.detectDate(args.getJoinedStrings(0));
|
2011-08-05 10:29:49 +00:00
|
|
|
|
2011-03-14 03:23:55 +00:00
|
|
|
if (date == null) {
|
|
|
|
player.printError("Could not detect the date inputted.");
|
|
|
|
} else {
|
2011-08-05 10:29:49 +00:00
|
|
|
try {
|
|
|
|
Snapshot snapshot = config.snapshotRepo.getSnapshotBefore(date, player.getWorld().getName());
|
|
|
|
|
|
|
|
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.");
|
2011-03-14 03:23:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-05 10:29:49 +00:00
|
|
|
|
|
|
|
@Command(aliases = {"after"},
|
|
|
|
usage = "<date>",
|
|
|
|
desc = "Choose the nearest snapshot after a date",
|
|
|
|
min = 1,
|
|
|
|
max = -1)
|
2011-03-14 03:23:55 +00:00
|
|
|
@CommandPermissions({"worldedit.snapshots.restore"})
|
|
|
|
public static void after(CommandContext args, WorldEdit we,
|
|
|
|
LocalSession session, LocalPlayer player, EditSession editSession)
|
|
|
|
throws WorldEditException {
|
2011-08-05 10:29:49 +00:00
|
|
|
|
2011-03-14 03:23:55 +00:00
|
|
|
LocalConfiguration config = we.getConfiguration();
|
2011-08-05 10:29:49 +00:00
|
|
|
|
2011-03-14 03:23:55 +00:00
|
|
|
if (config.snapshotRepo == null) {
|
|
|
|
player.printError("Snapshot/backup restore is not configured.");
|
|
|
|
return;
|
|
|
|
}
|
2011-08-05 10:29:49 +00:00
|
|
|
|
2011-03-14 03:23:55 +00:00
|
|
|
Calendar date = session.detectDate(args.getJoinedStrings(0));
|
2011-08-05 10:29:49 +00:00
|
|
|
|
2011-03-14 03:23:55 +00:00
|
|
|
if (date == null) {
|
|
|
|
player.printError("Could not detect the date inputted.");
|
|
|
|
} else {
|
2011-08-05 10:29:49 +00:00
|
|
|
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.");
|
2011-03-14 03:23:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-01-29 10:05:22 +00:00
|
|
|
}
|