Added /snap before [date] and /snap after [date]. Example: /snap before last Monday 2am
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<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.
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
38
src/com/sk89q/worldedit/snapshots/SnapshotDateParser.java
Normal file
38
src/com/sk89q/worldedit/snapshots/SnapshotDateParser.java
Normal file
@@ -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);
|
||||
}
|
||||
@@ -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<SnapshotDateParser> dateParsers
|
||||
= new ArrayList<SnapshotDateParser>();
|
||||
|
||||
/**
|
||||
* 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<Snapshot> 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<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) {
|
||||
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<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];
|
||||
|
||||
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<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
|
||||
*/
|
||||
public Snapshot getDefaultSnapshot() {
|
||||
Snapshot[] snapshots = getSnapshots();
|
||||
List<Snapshot> snapshots = getSnapshots(true);
|
||||
|
||||
if (snapshots.length == 0) {
|
||||
if (snapshots.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return snapshots[0];
|
||||
return snapshots.get(0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
51
src/com/sk89q/worldedit/snapshots/YYMMDDHHIISSParser.java
Normal file
51
src/com/sk89q/worldedit/snapshots/YYMMDDHHIISSParser.java
Normal file
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user