diff --git a/LICENSE.txt b/LICENSE.txt
index b5eb8c954..332b14517 100644
--- a/LICENSE.txt
+++ b/LICENSE.txt
@@ -168,8 +168,6 @@ permanent authorization for you to choose that version for the
Library.
-
-
JNBT License
------------
@@ -203,28 +201,27 @@ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
+JChronic License
+----------------
+The MIT License
-JOpt Simple License
--------------------
+Copyright (c) 2009 Mike Schrag, Sam Tingleff
-Copyright (c) 2009 Paul R. Holser, Jr.
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
-Permission is hereby granted, free of charge, to any person obtaining
-a copy of this software and associated documentation files (the
-"Software"), to deal in the Software without restriction, including
-without limitation the rights to use, copy, modify, merge, publish,
-distribute, sublicense, and/or sell copies of the Software, and to
-permit persons to whom the Software is furnished to do so, subject to
-the following conditions:
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/NOTICE.txt b/NOTICE.txt
index aff731624..31f7ca700 100644
--- a/NOTICE.txt
+++ b/NOTICE.txt
@@ -5,4 +5,6 @@ This product includes software from WorldEdit
This product includes software from JOpt Simple, under the MIT license.
-This product includes software by toi.
\ No newline at end of file
+This product includes software by toi.
+
+This product includes software from JChronic, under the MIT license.
\ No newline at end of file
diff --git a/build.xml b/build.xml
index 45cd0dde9..afc9ca6d5 100644
--- a/build.xml
+++ b/build.xml
@@ -17,6 +17,7 @@
+
@@ -48,7 +49,8 @@
-
+
+
diff --git a/lib/jchronic.jar b/lib/jchronic.jar
new file mode 100644
index 000000000..74b42371c
Binary files /dev/null and b/lib/jchronic.jar differ
diff --git a/src/com/sk89q/worldedit/LocalSession.java b/src/com/sk89q/worldedit/LocalSession.java
index fffbbf5d0..8d04e1b33 100644
--- a/src/com/sk89q/worldedit/LocalSession.java
+++ b/src/com/sk89q/worldedit/LocalSession.java
@@ -19,9 +19,15 @@
package com.sk89q.worldedit;
+import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
+import java.util.TimeZone;
+import com.sk89q.jchronic.Chronic;
+import com.sk89q.jchronic.Options;
+import com.sk89q.jchronic.utils.Span;
+import com.sk89q.jchronic.utils.Time;
import com.sk89q.worldedit.snapshots.Snapshot;
import com.sk89q.worldedit.tools.BrushTool;
import com.sk89q.worldedit.tools.SinglePickaxe;
@@ -65,7 +71,8 @@ public class LocalSession {
private String lastScript;
private boolean beenToldVersion = false;
private boolean hasCUISupport = false;
-
+ private TimeZone timezone = TimeZone.getDefault();
+
/**
* Construct the object.
*
@@ -74,6 +81,24 @@ public class LocalSession {
public LocalSession(LocalConfiguration config) {
this.config = config;
}
+
+ /**
+ * Get the session's timezone.
+ *
+ * @return
+ */
+ public TimeZone getTimeZone() {
+ return timezone;
+ }
+
+ /**
+ * Set the session's timezone.
+ *
+ * @param timezone
+ */
+ public void setTimezone(TimeZone timezone) {
+ this.timezone = timezone;
+ }
/**
* Clear history.
@@ -572,4 +597,22 @@ public class LocalSession {
public void setCUISupport(boolean support) {
hasCUISupport = true;
}
+
+ /**
+ * Detect date from a user's input.
+ *
+ * @param input
+ * @return
+ */
+ public Calendar detectDate(String input) {
+ Time.setTimeZone(getTimeZone());
+ Options opt = new com.sk89q.jchronic.Options();
+ opt.setNow(Calendar.getInstance(getTimeZone()));
+ Span date = Chronic.parse(input, opt);
+ if (date == null) {
+ return null;
+ } else {
+ return date.getBeginCalendar();
+ }
+ }
}
diff --git a/src/com/sk89q/worldedit/commands/SnapshotCommands.java b/src/com/sk89q/worldedit/commands/SnapshotCommands.java
index bcd92d27a..bff22425c 100644
--- a/src/com/sk89q/worldedit/commands/SnapshotCommands.java
+++ b/src/com/sk89q/worldedit/commands/SnapshotCommands.java
@@ -21,6 +21,10 @@ package com.sk89q.worldedit.commands;
import java.io.File;
import java.io.IOException;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.List;
import java.util.logging.Logger;
import com.sk89q.minecraft.util.commands.Command;
import com.sk89q.minecraft.util.commands.CommandContext;
@@ -36,6 +40,7 @@ import com.sk89q.worldedit.snapshots.Snapshot;
*/
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"},
@@ -55,11 +60,11 @@ public class SnapshotCommands {
Math.min(40, Math.max(5, args.getInteger(0))) : 5;
if (config.snapshotRepo != null) {
- Snapshot[] snapshots = config.snapshotRepo.getSnapshots();
+ List snapshots = config.snapshotRepo.getSnapshots(true);
- if (snapshots.length > 0) {
- for (byte i = 0; i < Math.min(num, snapshots.length); i++) {
- player.print((i + 1) + ". " + snapshots[i].getName());
+ if (snapshots.size() > 0) {
+ for (byte i = 0; i < Math.min(num, snapshots.size()); i++) {
+ player.print((i + 1) + ". " + snapshots.get(i).getName());
}
player.print("Use /snap use [snapshot] or /snap use latest.");
@@ -123,4 +128,78 @@ public class SnapshotCommands {
}
}
}
+
+ @Command(
+ aliases = {"before"},
+ usage = "",
+ desc = "Choose the nearest snapshot before a date",
+ min = 1,
+ max = -1
+ )
+ @CommandPermissions({"worldedit.snapshots.restore"})
+ public static void before(CommandContext args, WorldEdit we,
+ LocalSession session, LocalPlayer player, EditSession editSession)
+ throws WorldEditException {
+
+ LocalConfiguration config = we.getConfiguration();
+
+ if (config.snapshotRepo == null) {
+ player.printError("Snapshot/backup restore is not configured.");
+ return;
+ }
+
+ Calendar date = session.detectDate(args.getJoinedStrings(0));
+
+ if (date == null) {
+ player.printError("Could not detect the date inputted.");
+ } else {
+ dateFormat.setTimeZone(session.getTimeZone());
+
+ Snapshot snapshot = config.snapshotRepo.getSnapshotBefore(date);
+ if (snapshot == null) {
+ player.printError("Couldn't find a snapshot before "
+ + dateFormat.format(date.getTime()) + ".");
+ } else {
+ session.setSnapshot(snapshot);
+ player.print("Snapshot set to: " + snapshot.getName());
+ }
+ }
+ }
+
+ @Command(
+ aliases = {"after"},
+ usage = "",
+ desc = "Choose the nearest snapshot after a date",
+ min = 1,
+ max = -1
+ )
+ @CommandPermissions({"worldedit.snapshots.restore"})
+ public static void after(CommandContext args, WorldEdit we,
+ LocalSession session, LocalPlayer player, EditSession editSession)
+ throws WorldEditException {
+
+ LocalConfiguration config = we.getConfiguration();
+
+ if (config.snapshotRepo == null) {
+ player.printError("Snapshot/backup restore is not configured.");
+ return;
+ }
+
+ Calendar date = session.detectDate(args.getJoinedStrings(0));
+
+ if (date == null) {
+ player.printError("Could not detect the date inputted.");
+ } else {
+ dateFormat.setTimeZone(session.getTimeZone());
+
+ Snapshot snapshot = config.snapshotRepo.getSnapshotAfter(date);
+ if (snapshot == null) {
+ player.printError("Couldn't find a snapshot after "
+ + dateFormat.format(date.getTime()) + ".");
+ } else {
+ session.setSnapshot(snapshot);
+ player.print("Snapshot set to: " + snapshot.getName());
+ }
+ }
+ }
}
diff --git a/src/com/sk89q/worldedit/snapshots/ModificationTimerParser.java b/src/com/sk89q/worldedit/snapshots/ModificationTimerParser.java
new file mode 100644
index 000000000..7d723e682
--- /dev/null
+++ b/src/com/sk89q/worldedit/snapshots/ModificationTimerParser.java
@@ -0,0 +1,35 @@
+// $Id$
+/*
+ * WorldEdit
+ * Copyright (C) 2010, 2011 sk89q
+ *
+ * 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 .
+*/
+
+package com.sk89q.worldedit.snapshots;
+
+import java.io.File;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+
+public class ModificationTimerParser implements SnapshotDateParser {
+
+ @Override
+ public Calendar detectDate(File file) {
+ Calendar cal = new GregorianCalendar();
+ cal.setTimeInMillis(file.lastModified());
+ return cal;
+ }
+
+}
diff --git a/src/com/sk89q/worldedit/snapshots/Snapshot.java b/src/com/sk89q/worldedit/snapshots/Snapshot.java
index fd8dcf5b6..a1d422bfe 100644
--- a/src/com/sk89q/worldedit/snapshots/Snapshot.java
+++ b/src/com/sk89q/worldedit/snapshots/Snapshot.java
@@ -21,23 +21,28 @@ package com.sk89q.worldedit.snapshots;
import com.sk89q.worldedit.data.*;
import java.io.*;
+import java.util.Calendar;
import java.util.logging.Logger;
/**
*
* @author sk89q
*/
-public class Snapshot {
+public class Snapshot implements Comparable {
protected static Logger logger = Logger.getLogger("Minecraft.WorldEdit");
/**
* Stores snapshot file.
*/
- private File file;
+ protected File file;
/**
* Name of the snapshot;
*/
- private String name;
+ protected String name;
+ /**
+ * Stores the date associated with the snapshot.
+ */
+ protected Calendar date;
/**
* Construct a snapshot restoration operation.
@@ -125,4 +130,48 @@ public class Snapshot {
public String getName() {
return name;
}
+
+ /**
+ * Get the file for the snapshot.
+ *
+ * @return
+ */
+ public File getFile() {
+ return file;
+ }
+
+ /**
+ * Get the date associated with this snapshot.
+ *
+ * @return
+ */
+ public Calendar getDate() {
+ return date;
+ }
+
+ /**
+ * Set the date of the snapshot.
+ *
+ * @param date
+ */
+ public void setDate(Calendar date) {
+ this.date = date;
+ }
+
+ @Override
+ public int compareTo(Snapshot o) {
+ if (o.date == null || date == null) {
+ return name.compareTo(o.name);
+ } else {
+ return date.compareTo(o.date);
+ }
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (o instanceof Snapshot) {
+ return file.equals(((Snapshot) o).file);
+ }
+ return false;
+ }
}
diff --git a/src/com/sk89q/worldedit/snapshots/SnapshotDateParser.java b/src/com/sk89q/worldedit/snapshots/SnapshotDateParser.java
new file mode 100644
index 000000000..a17a40471
--- /dev/null
+++ b/src/com/sk89q/worldedit/snapshots/SnapshotDateParser.java
@@ -0,0 +1,38 @@
+// $Id$
+/*
+ * WorldEdit
+ * Copyright (C) 2010, 2011 sk89q
+ *
+ * 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 .
+*/
+
+package com.sk89q.worldedit.snapshots;
+
+import java.io.File;
+import java.util.Calendar;
+
+/**
+ * A name parser attempts to make sense of a filename for a snapshot.
+ *
+ * @author sk89q
+ */
+public interface SnapshotDateParser {
+ /**
+ * Attempt to detect a date from a file.
+ *
+ * @param file
+ * @return date or null
+ */
+ public Calendar detectDate(File file);
+}
diff --git a/src/com/sk89q/worldedit/snapshots/SnapshotRepository.java b/src/com/sk89q/worldedit/snapshots/SnapshotRepository.java
index ea284d04b..9472e4de5 100644
--- a/src/com/sk89q/worldedit/snapshots/SnapshotRepository.java
+++ b/src/com/sk89q/worldedit/snapshots/SnapshotRepository.java
@@ -20,8 +20,10 @@
package com.sk89q.worldedit.snapshots;
import java.io.*;
-import java.util.Arrays;
+import java.util.ArrayList;
+import java.util.Calendar;
import java.util.Collections;
+import java.util.List;
/**
*
@@ -31,7 +33,13 @@ public class SnapshotRepository {
/**
* Stores the directory the snapshots come from.
*/
- private File dir;
+ protected File dir;
+
+ /**
+ * List of date parsers.
+ */
+ protected List dateParsers
+ = new ArrayList();
/**
* Create a new instance of a repository.
@@ -40,6 +48,9 @@ public class SnapshotRepository {
*/
public SnapshotRepository(File dir) {
this.dir = dir;
+
+ dateParsers.add(new YYMMDDHHIISSParser());
+ dateParsers.add(new ModificationTimerParser());
}
/**
@@ -48,16 +59,17 @@ public class SnapshotRepository {
* @param dir
*/
public SnapshotRepository(String dir) {
- this.dir = new File(dir);
+ this(new File(dir));
}
/**
* Get a list of snapshots in a directory. The newest snapshot is
* near the top of the array.
*
+ * @param newestFirst
* @return
*/
- public Snapshot[] getSnapshots() {
+ public List getSnapshots(boolean newestFirst) {
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
File f = new File(dir, name);
@@ -66,22 +78,81 @@ public class SnapshotRepository {
};
String[] snapshotNames = dir.list(filter);
+ List list = new ArrayList(snapshotNames.length);
+
+ for (String name : snapshotNames) {
+ Snapshot snapshot = new Snapshot(this, name);
+ detectDate(snapshot);
+ list.add(snapshot);
+ }
- if (snapshotNames == null || snapshotNames.length == 0) {
- return new Snapshot[0];
+ if (newestFirst) {
+ Collections.sort(list, Collections.reverseOrder());
+ } else {
+ Collections.sort(list);
+ }
+
+ return list;
+ }
+
+ /**
+ * Get the first snapshot after a date.
+ *
+ * @param date
+ * @return
+ */
+ public Snapshot getSnapshotAfter(Calendar date) {
+ List snapshots = getSnapshots(true);
+ Snapshot last = null;
+
+ for (Snapshot snapshot : snapshots) {
+ if (snapshot.getDate() != null
+ && snapshot.getDate().before(date)) {
+ return last;
+ }
+
+ last = snapshot;
}
- Snapshot[] snapshots = new Snapshot[snapshotNames.length];
-
- Arrays.sort(snapshotNames, Collections.reverseOrder());
-
- int i = 0;
- for (String name : snapshotNames) {
- snapshots[i] = new Snapshot(this, name);
- i++;
+ return last;
+ }
+
+ /**
+ * Get the first snapshot before a date.
+ *
+ * @param date
+ * @return
+ */
+ public Snapshot getSnapshotBefore(Calendar date) {
+ List snapshots = getSnapshots(false);
+ Snapshot last = null;
+
+ for (Snapshot snapshot : snapshots) {
+ if (snapshot.getDate().after(date)) {
+ return last;
+ }
+
+ last = snapshot;
}
-
- return snapshots;
+
+ return last;
+ }
+
+ /**
+ * Attempt to detect a snapshot's date and assign it.
+ *
+ * @param snapshot
+ */
+ protected void detectDate(Snapshot snapshot) {
+ for (SnapshotDateParser parser : dateParsers) {
+ Calendar date = parser.detectDate(snapshot.getFile());
+ if (date != null) {
+ snapshot.setDate(date);
+ return;
+ }
+ }
+
+ snapshot.setDate(null);
}
/**
@@ -90,13 +161,13 @@ public class SnapshotRepository {
* @return
*/
public Snapshot getDefaultSnapshot() {
- Snapshot[] snapshots = getSnapshots();
+ List snapshots = getSnapshots(true);
- if (snapshots.length == 0) {
+ if (snapshots.size() == 0) {
return null;
}
- return snapshots[0];
+ return snapshots.get(0);
}
/**
diff --git a/src/com/sk89q/worldedit/snapshots/YYMMDDHHIISSParser.java b/src/com/sk89q/worldedit/snapshots/YYMMDDHHIISSParser.java
new file mode 100644
index 000000000..35ec1f580
--- /dev/null
+++ b/src/com/sk89q/worldedit/snapshots/YYMMDDHHIISSParser.java
@@ -0,0 +1,51 @@
+// $Id$
+/*
+ * WorldEdit
+ * Copyright (C) 2010, 2011 sk89q
+ *
+ * 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 .
+*/
+
+package com.sk89q.worldedit.snapshots;
+
+import java.io.File;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class YYMMDDHHIISSParser implements SnapshotDateParser {
+
+ protected Pattern patt =
+ Pattern.compile("([0-9]+)[^0-9]?([0-9]+)[^0-9]?([0-9]+)[^0-9]?"
+ + "([0-9]+)[^0-9]?([0-9]+)[^0-9]?([0-9]+)");
+
+ @Override
+ public Calendar detectDate(File file) {
+ Matcher matcher = patt.matcher(file.getName());
+ if (matcher.matches()) {
+ int year = Integer.parseInt(matcher.group(1));
+ int month = Integer.parseInt(matcher.group(2));
+ int day = Integer.parseInt(matcher.group(3));
+ int hrs = Integer.parseInt(matcher.group(4));
+ int min = Integer.parseInt(matcher.group(5));
+ int sec = Integer.parseInt(matcher.group(6));
+ Calendar calender = new GregorianCalendar();
+ calender.set(year, month, day, hrs, min, sec);
+ return calender;
+ }
+ return null;
+ }
+
+}