Skip to content

Commit

Permalink
Added /snap before [date] and /snap after [date]. Example: /snap befo…
Browse files Browse the repository at this point in the history
…re last Monday 2am
  • Loading branch information
sk89q committed Mar 14, 2011
1 parent 5b4ac12 commit 2d58fcb
Show file tree
Hide file tree
Showing 11 changed files with 418 additions and 51 deletions.
41 changes: 19 additions & 22 deletions LICENSE.txt
Expand Up @@ -168,8 +168,6 @@ permanent authorization for you to choose that version for the
Library.




JNBT License
------------

Expand Down Expand Up @@ -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 Paul R. Holser, Jr.
Copyright (c) 2009 Mike Schrag, Sam Tingleff

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.
4 changes: 3 additions & 1 deletion NOTICE.txt
Expand Up @@ -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.
This product includes software by toi.

This product includes software from JChronic, under the MIT license.
4 changes: 3 additions & 1 deletion build.xml
Expand Up @@ -17,6 +17,7 @@
<include name="Permissions.jar" />
<include name="js.jar" />
<include name="worldeditsunrhino.jar" />
<include name="jchronic.jar" />
</fileset>

<target name="init">
Expand Down Expand Up @@ -48,7 +49,8 @@
<mkdir dir="${build.dir}/defaults"/>
<copy tofile="${build.dir}/defaults/config.yml" file="config.yml"/>
<jar jarfile="${dist.dir}/WorldEdit.jar" basedir="${build.dir}" manifest="manifest.mf">
<zipgroupfileset dir="lib" includes="worldeditsunrhino.jar" />
<zipgroupfileset dir="lib" includes="worldeditsunrhino.jar" />
<zipgroupfileset dir="lib" includes="jchronic.jar" />
</jar>
</target>

Expand Down
Binary file added lib/jchronic.jar
Binary file not shown.
45 changes: 44 additions & 1 deletion src/com/sk89q/worldedit/LocalSession.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand All @@ -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.
Expand Down Expand Up @@ -572,4 +597,22 @@ public void dispatchCUISelection(LocalPlayer player) {
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();
}
}
}
87 changes: 83 additions & 4 deletions src/com/sk89q/worldedit/commands/SnapshotCommands.java
Expand Up @@ -21,6 +21,10 @@

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;
Expand All @@ -36,6 +40,7 @@
*/
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"},
Expand All @@ -55,11 +60,11 @@ public static void list(CommandContext args, WorldEdit we,
Math.min(40, Math.max(5, args.getInteger(0))) : 5;

if (config.snapshotRepo != null) {
Snapshot[] snapshots = config.snapshotRepo.getSnapshots();
List<Snapshot> 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.");
Expand Down Expand Up @@ -123,4 +128,78 @@ public static void use(CommandContext args, WorldEdit we,
}
}
}

@Command(
aliases = {"before"},
usage = "<date>",
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 = "<date>",
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());
}
}
}
}
35 changes: 35 additions & 0 deletions src/com/sk89q/worldedit/snapshots/ModificationTimerParser.java
@@ -0,0 +1,35 @@
// $Id$
/*
* WorldEdit
* Copyright (C) 2010, 2011 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.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;
}

}
55 changes: 52 additions & 3 deletions src/com/sk89q/worldedit/snapshots/Snapshot.java
Expand Up @@ -21,23 +21,28 @@

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<Snapshot> {
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.
Expand Down Expand Up @@ -125,4 +130,48 @@ public ChunkStore _getChunkStore() throws IOException, DataException {
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;
}
}

0 comments on commit 2d58fcb

Please sign in to comment.