Port snapshot commands, update some time stuff to new time

This commit is contained in:
Kenzie Togami
2019-04-23 23:47:22 -07:00
parent ea3605204c
commit 2ea30dc70e
8 changed files with 172 additions and 125 deletions

View File

@ -21,32 +21,34 @@
package com.sk89q.worldedit.command;
import com.sk89q.minecraft.util.commands.Command;
import com.sk89q.minecraft.util.commands.CommandContext;
import com.sk89q.minecraft.util.commands.CommandPermissions;
import com.sk89q.worldedit.LocalConfiguration;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.command.util.CommandPermissions;
import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.world.snapshot.InvalidSnapshotException;
import com.sk89q.worldedit.world.snapshot.Snapshot;
import com.sk89q.worldedit.world.storage.MissingWorldException;
import org.enginehub.piston.annotation.Command;
import org.enginehub.piston.annotation.CommandContainer;
import org.enginehub.piston.annotation.param.Arg;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
/**
* Snapshot commands.
*/
@CommandContainer(superTypes = CommandPermissionsConditionGenerator.Registration.class)
public class SnapshotCommands {
private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
private static final DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");
private final WorldEdit we;
public SnapshotCommands(WorldEdit we) {
@ -54,14 +56,13 @@ public class SnapshotCommands {
}
@Command(
aliases = { "list" },
usage = "[num]",
desc = "List snapshots",
min = 0,
max = 1
name = "list",
desc = "List snapshots"
)
@CommandPermissions("worldedit.snapshots.list")
public void list(Player player, CommandContext args) throws WorldEditException {
public void list(Player player,
@Arg(desc = "# of snapshots to list", def = "5")
int num) throws WorldEditException {
LocalConfiguration config = we.getConfiguration();
@ -75,7 +76,7 @@ public class SnapshotCommands {
if (!snapshots.isEmpty()) {
int num = args.argsLength() > 0 ? Math.min(40, Math.max(5, args.getInteger(0))) : 5;
num = Math.min(40, Math.max(5, num));
player.print("Snapshots for world: '" + player.getWorld().getName() + "'");
for (byte i = 0; i < Math.min(num, snapshots.size()); i++) {
@ -104,14 +105,13 @@ public class SnapshotCommands {
}
@Command(
aliases = { "use" },
usage = "<snapshot>",
desc = "Choose a snapshot to use",
min = 1,
max = 1
name = "use",
desc = "Choose a snapshot to use"
)
@CommandPermissions("worldedit.snapshots.restore")
public void use(Player player, LocalSession session, CommandContext args) throws WorldEditException {
public void use(Player player, LocalSession session,
@Arg(desc = "Snapeshot to use")
String name) throws WorldEditException {
LocalConfiguration config = we.getConfiguration();
@ -120,8 +120,6 @@ public class SnapshotCommands {
return;
}
String name = args.getString(0);
// Want the latest snapshot?
if (name.equalsIgnoreCase("latest")) {
try {
@ -147,14 +145,13 @@ public class SnapshotCommands {
}
@Command(
aliases = { "sel" },
usage = "<index>",
desc = "Choose the snapshot based on the list id",
min = 1,
max = 1
name = "sel",
desc = "Choose the snapshot based on the list id"
)
@CommandPermissions("worldedit.snapshots.restore")
public void sel(Player player, LocalSession session, CommandContext args) throws WorldEditException {
public void sel(Player player, LocalSession session,
@Arg(desc = "The list ID to select")
int index) throws WorldEditException {
LocalConfiguration config = we.getConfiguration();
if (config.snapshotRepo == null) {
@ -162,14 +159,6 @@ public class SnapshotCommands {
return;
}
int index = -1;
try {
index = Integer.parseInt(args.getString(0));
} catch (NumberFormatException e) {
player.printError("Invalid index, " + args.getString(0) + " is not a valid integer.");
return;
}
if (index < 1) {
player.printError("Invalid index, must be equal or higher then 1.");
return;
@ -194,14 +183,13 @@ public class SnapshotCommands {
}
@Command(
aliases = { "before" },
usage = "<date>",
desc = "Choose the nearest snapshot before a date",
min = 1,
max = -1
name = "before",
desc = "Choose the nearest snapshot before a date"
)
@CommandPermissions("worldedit.snapshots.restore")
public void before(Player player, LocalSession session, CommandContext args) throws WorldEditException {
public void before(Player player, LocalSession session,
@Arg(desc = "The soonest date that may be used")
ZonedDateTime date) throws WorldEditException {
LocalConfiguration config = we.getConfiguration();
@ -210,37 +198,29 @@ public class SnapshotCommands {
return;
}
Calendar date = session.detectDate(args.getJoinedStrings(0));
try {
Snapshot snapshot = config.snapshotRepo.getSnapshotBefore(date, player.getWorld().getName());
if (date == null) {
player.printError("Could not detect the date inputted.");
} else {
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.");
if (snapshot == null) {
player.printError("Couldn't find a snapshot before "
+ dateFormat.withZone(session.getTimeZone()).format(date) + ".");
} else {
session.setSnapshot(snapshot);
player.print("Snapshot set to: " + snapshot.getName());
}
} catch (MissingWorldException ex) {
player.printError("No snapshots were found for this world.");
}
}
@Command(
aliases = { "after" },
usage = "<date>",
desc = "Choose the nearest snapshot after a date",
min = 1,
max = -1
name = "after",
desc = "Choose the nearest snapshot after a date"
)
@CommandPermissions("worldedit.snapshots.restore")
public void after(Player player, LocalSession session, CommandContext args) throws WorldEditException {
public void after(Player player, LocalSession session,
@Arg(desc = "The soonest date that may be used")
ZonedDateTime date) throws WorldEditException {
LocalConfiguration config = we.getConfiguration();
@ -249,24 +229,17 @@ public class SnapshotCommands {
return;
}
Calendar date = session.detectDate(args.getJoinedStrings(0));
if (date == null) {
player.printError("Could not detect the date inputted.");
} else {
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.");
try {
Snapshot snapshot = config.snapshotRepo.getSnapshotAfter(date, player.getWorld().getName());
if (snapshot == null) {
player.printError("Couldn't find a snapshot after "
+ dateFormat.withZone(session.getTimeZone()).format(date) + ".");
} else {
session.setSnapshot(snapshot);
player.print("Snapshot set to: " + snapshot.getName());
}
} catch (MissingWorldException ex) {
player.printError("No snapshots were found for this world.");
}
}