Added /snap before [date] and /snap after [date]. Example: /snap before last Monday 2am

Dieser Commit ist enthalten in:
sk89q 2011-03-13 20:23:55 -07:00
Ursprung 5b4ac1233a
Commit 2d58fcb5b2
11 geänderte Dateien mit 418 neuen und 51 gelöschten Zeilen

Datei anzeigen

@ -168,8 +168,6 @@ permanent authorization for you to choose that version for the
Library. Library.
JNBT License 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. 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 The above copyright notice and this permission notice shall be included in
a copy of this software and associated documentation files (the all copies or substantial portions of the Software.
"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 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
included in all copies or substantial portions of the Software. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE THE SOFTWARE.
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.

Datei anzeigen

@ -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 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.

Datei anzeigen

@ -17,6 +17,7 @@
<include name="Permissions.jar" /> <include name="Permissions.jar" />
<include name="js.jar" /> <include name="js.jar" />
<include name="worldeditsunrhino.jar" /> <include name="worldeditsunrhino.jar" />
<include name="jchronic.jar" />
</fileset> </fileset>
<target name="init"> <target name="init">
@ -48,7 +49,8 @@
<mkdir dir="${build.dir}/defaults"/> <mkdir dir="${build.dir}/defaults"/>
<copy tofile="${build.dir}/defaults/config.yml" file="config.yml"/> <copy tofile="${build.dir}/defaults/config.yml" file="config.yml"/>
<jar jarfile="${dist.dir}/WorldEdit.jar" basedir="${build.dir}" manifest="manifest.mf"> <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> </jar>
</target> </target>

BIN
lib/jchronic.jar Normale Datei

Binäre Datei nicht angezeigt.

Datei anzeigen

@ -19,9 +19,15 @@
package com.sk89q.worldedit; package com.sk89q.worldedit;
import java.util.Calendar;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Map; 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.snapshots.Snapshot;
import com.sk89q.worldedit.tools.BrushTool; import com.sk89q.worldedit.tools.BrushTool;
import com.sk89q.worldedit.tools.SinglePickaxe; import com.sk89q.worldedit.tools.SinglePickaxe;
@ -65,7 +71,8 @@ public class LocalSession {
private String lastScript; private String lastScript;
private boolean beenToldVersion = false; private boolean beenToldVersion = false;
private boolean hasCUISupport = false; private boolean hasCUISupport = false;
private TimeZone timezone = TimeZone.getDefault();
/** /**
* Construct the object. * Construct the object.
* *
@ -74,6 +81,24 @@ public class LocalSession {
public LocalSession(LocalConfiguration config) { public LocalSession(LocalConfiguration config) {
this.config = 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. * Clear history.
@ -572,4 +597,22 @@ public class LocalSession {
public void setCUISupport(boolean support) { public void setCUISupport(boolean support) {
hasCUISupport = true; 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();
}
}
} }

Datei anzeigen

@ -21,6 +21,10 @@ package com.sk89q.worldedit.commands;
import java.io.File; import java.io.File;
import java.io.IOException; 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 java.util.logging.Logger;
import com.sk89q.minecraft.util.commands.Command; import com.sk89q.minecraft.util.commands.Command;
import com.sk89q.minecraft.util.commands.CommandContext; import com.sk89q.minecraft.util.commands.CommandContext;
@ -36,6 +40,7 @@ import com.sk89q.worldedit.snapshots.Snapshot;
*/ */
public class SnapshotCommands { public class SnapshotCommands {
private static Logger logger = Logger.getLogger("Minecraft.WorldEdit"); private static Logger logger = Logger.getLogger("Minecraft.WorldEdit");
private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
@Command( @Command(
aliases = {"list"}, aliases = {"list"},
@ -55,11 +60,11 @@ public class SnapshotCommands {
Math.min(40, Math.max(5, args.getInteger(0))) : 5; Math.min(40, Math.max(5, args.getInteger(0))) : 5;
if (config.snapshotRepo != null) { if (config.snapshotRepo != null) {
Snapshot[] snapshots = config.snapshotRepo.getSnapshots(); List<Snapshot> snapshots = config.snapshotRepo.getSnapshots(true);
if (snapshots.length > 0) { if (snapshots.size() > 0) {
for (byte i = 0; i < Math.min(num, snapshots.length); i++) { for (byte i = 0; i < Math.min(num, snapshots.size()); i++) {
player.print((i + 1) + ". " + snapshots[i].getName()); player.print((i + 1) + ". " + snapshots.get(i).getName());
} }
player.print("Use /snap use [snapshot] or /snap use latest."); player.print("Use /snap use [snapshot] or /snap use latest.");
@ -123,4 +128,78 @@ public class SnapshotCommands {
} }
} }
} }
@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());
}
}
}
} }

Datei anzeigen

@ -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;
}
}

Datei anzeigen

@ -21,23 +21,28 @@ package com.sk89q.worldedit.snapshots;
import com.sk89q.worldedit.data.*; import com.sk89q.worldedit.data.*;
import java.io.*; import java.io.*;
import java.util.Calendar;
import java.util.logging.Logger; import java.util.logging.Logger;
/** /**
* *
* @author sk89q * @author sk89q
*/ */
public class Snapshot { 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. * Stores snapshot file.
*/ */
private File file; protected File file;
/** /**
* Name of the snapshot; * 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. * Construct a snapshot restoration operation.
@ -125,4 +130,48 @@ public class Snapshot {
public String getName() { public String getName() {
return name; 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;
}
} }

Datei anzeigen

@ -0,0 +1,38 @@
// $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;
/**
* 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);
}

Datei anzeigen

@ -20,8 +20,10 @@
package com.sk89q.worldedit.snapshots; package com.sk89q.worldedit.snapshots;
import java.io.*; import java.io.*;
import java.util.Arrays; import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections; import java.util.Collections;
import java.util.List;
/** /**
* *
@ -31,7 +33,13 @@ public class SnapshotRepository {
/** /**
* Stores the directory the snapshots come from. * Stores the directory the snapshots come from.
*/ */
private File dir; protected File dir;
/**
* List of date parsers.
*/
protected List<SnapshotDateParser> dateParsers
= new ArrayList<SnapshotDateParser>();
/** /**
* Create a new instance of a repository. * Create a new instance of a repository.
@ -40,6 +48,9 @@ public class SnapshotRepository {
*/ */
public SnapshotRepository(File dir) { public SnapshotRepository(File dir) {
this.dir = dir; this.dir = dir;
dateParsers.add(new YYMMDDHHIISSParser());
dateParsers.add(new ModificationTimerParser());
} }
/** /**
@ -48,16 +59,17 @@ public class SnapshotRepository {
* @param dir * @param dir
*/ */
public SnapshotRepository(String 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 * Get a list of snapshots in a directory. The newest snapshot is
* near the top of the array. * near the top of the array.
* *
* @param newestFirst
* @return * @return
*/ */
public Snapshot[] getSnapshots() { public List<Snapshot> getSnapshots(boolean newestFirst) {
FilenameFilter filter = new FilenameFilter() { FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) { public boolean accept(File dir, String name) {
File f = new File(dir, name); File f = new File(dir, name);
@ -66,22 +78,81 @@ public class SnapshotRepository {
}; };
String[] snapshotNames = dir.list(filter); String[] snapshotNames = dir.list(filter);
List<Snapshot> list = new ArrayList<Snapshot>(snapshotNames.length);
for (String name : snapshotNames) {
Snapshot snapshot = new Snapshot(this, name);
detectDate(snapshot);
list.add(snapshot);
}
if (snapshotNames == null || snapshotNames.length == 0) { if (newestFirst) {
return new Snapshot[0]; 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<Snapshot> 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]; return last;
}
Arrays.sort(snapshotNames, Collections.reverseOrder());
/**
int i = 0; * Get the first snapshot before a date.
for (String name : snapshotNames) { *
snapshots[i] = new Snapshot(this, name); * @param date
i++; * @return
*/
public Snapshot getSnapshotBefore(Calendar date) {
List<Snapshot> 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 * @return
*/ */
public Snapshot getDefaultSnapshot() { public Snapshot getDefaultSnapshot() {
Snapshot[] snapshots = getSnapshots(); List<Snapshot> snapshots = getSnapshots(true);
if (snapshots.length == 0) { if (snapshots.size() == 0) {
return null; return null;
} }
return snapshots[0]; return snapshots.get(0);
} }
/** /**

Datei anzeigen

@ -0,0 +1,51 @@
// $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;
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;
}
}