204 lines
8.0 KiB
Java
Raw Normal View History

2015-07-25 03:37:39 +10:00
package com.intellectualcrafters.plot.commands;
import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.config.Settings;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotId;
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.object.RunnableVal;
import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.Permissions;
import com.intellectualcrafters.plot.util.SchematicHandler;
import com.intellectualcrafters.plot.util.SchematicHandler.Schematic;
import com.intellectualcrafters.plot.util.TaskManager;
2015-07-27 19:50:04 +02:00
import com.plotsquared.general.commands.CommandDeclaration;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
@CommandDeclaration(
2015-09-11 20:09:22 +10:00
command = "load",
aliases = { "restore" },
category = CommandCategory.SCHEMATIC,
2015-09-11 20:09:22 +10:00
requiredType = RequiredType.NONE,
description = "Load your plot",
permission = "plots.load",
usage = "/plot restore")
2015-09-13 14:04:31 +10:00
public class Load extends SubCommand {
2015-07-25 03:37:39 +10:00
@Override
2015-09-13 14:04:31 +10:00
public boolean onCommand(final PlotPlayer plr, final String[] args) {
if (!Settings.METRICS) {
2015-07-25 03:37:39 +10:00
MainUtil.sendMessage(plr, "&cPlease enable metrics in order to use this command.\n&7 - Or host it yourself if you don't like the free service");
return false;
}
final String world = plr.getLocation().getWorld();
2016-02-11 05:59:51 +11:00
if (!PS.get().hasPlotArea(world)) {
2015-09-13 14:04:31 +10:00
return !sendMessage(plr, C.NOT_IN_PLOT_WORLD);
}
2016-02-11 05:59:51 +11:00
final Plot plot = plr.getCurrentPlot();
2015-09-13 14:04:31 +10:00
if (plot == null) {
return !sendMessage(plr, C.NOT_IN_PLOT);
}
if (!plot.hasOwner()) {
2015-07-25 03:37:39 +10:00
MainUtil.sendMessage(plr, C.PLOT_UNOWNED);
return false;
}
2015-09-13 14:04:31 +10:00
if (!plot.isOwner(plr.getUUID()) && !Permissions.hasPermission(plr, "plots.admin.command.load")) {
2015-07-25 03:37:39 +10:00
MainUtil.sendMessage(plr, C.NO_PLOT_PERMS);
return false;
}
2015-09-22 23:23:28 +10:00
if (plot.getRunning() > 0) {
2015-07-25 03:37:39 +10:00
MainUtil.sendMessage(plr, C.WAIT_FOR_TIMER);
return false;
}
2015-09-13 14:04:31 +10:00
if (args.length != 0) {
if (args.length == 1) {
2015-09-11 20:09:22 +10:00
final List<String> schematics = (List<String>) plr.getMeta("plot_schematics");
2015-09-13 14:04:31 +10:00
if (schematics == null) {
2015-07-25 03:37:39 +10:00
// No schematics found:
MainUtil.sendMessage(plr, C.LOAD_NULL);
return false;
}
String schem;
2015-09-13 14:04:31 +10:00
try {
2015-07-25 03:37:39 +10:00
schem = schematics.get(Integer.parseInt(args[0]) - 1);
2015-09-13 14:04:31 +10:00
} catch (final Exception e) {
2015-07-25 03:37:39 +10:00
// use /plot load <index>
MainUtil.sendMessage(plr, C.NOT_VALID_NUMBER, "(1, " + schematics.size() + ")");
return false;
}
final URL url;
2015-09-13 14:04:31 +10:00
try {
2015-08-02 16:44:37 +10:00
url = new URL(Settings.WEB_URL + "saves/" + plr.getUUID() + "/" + schem + ".schematic");
2015-09-13 14:04:31 +10:00
} catch (final MalformedURLException e) {
2015-07-25 03:37:39 +10:00
e.printStackTrace();
MainUtil.sendMessage(plr, C.LOAD_FAILED);
return false;
}
2015-12-20 06:40:42 +11:00
plot.addRunning();
2015-07-25 03:37:39 +10:00
MainUtil.sendMessage(plr, C.GENERATING_COMPONENT);
2015-09-13 14:04:31 +10:00
TaskManager.runTaskAsync(new Runnable() {
2015-07-25 03:37:39 +10:00
@Override
2015-09-13 14:04:31 +10:00
public void run() {
2015-09-11 20:09:22 +10:00
final Schematic schematic = SchematicHandler.manager.getSchematic(url);
2015-09-13 14:04:31 +10:00
if (schematic == null) {
2015-12-20 06:40:42 +11:00
plot.removeRunning();
2015-07-25 03:37:39 +10:00
sendMessage(plr, C.SCHEMATIC_INVALID, "non-existent or not in gzip format");
return;
}
2015-09-13 14:04:31 +10:00
SchematicHandler.manager.paste(schematic, plot, 0, 0, new RunnableVal<Boolean>() {
2015-07-25 03:37:39 +10:00
@Override
2016-02-11 05:59:51 +11:00
public void run(Boolean value) {
2015-12-20 06:40:42 +11:00
plot.removeRunning();
2015-09-13 14:04:31 +10:00
if (value) {
2015-07-25 03:37:39 +10:00
sendMessage(plr, C.SCHEMATIC_PASTE_SUCCESS);
2015-09-13 14:04:31 +10:00
} else {
2015-07-25 03:37:39 +10:00
sendMessage(plr, C.SCHEMATIC_PASTE_FAILED);
}
}
});
}
});
return true;
}
2015-12-20 06:40:42 +11:00
plot.removeRunning();
2015-07-25 03:37:39 +10:00
MainUtil.sendMessage(plr, C.COMMAND_SYNTAX, "/plot load <index>");
return false;
}
2015-09-13 14:04:31 +10:00
2015-07-25 03:37:39 +10:00
// list schematics
2015-09-13 14:04:31 +10:00
2015-09-11 20:09:22 +10:00
final List<String> schematics = (List<String>) plr.getMeta("plot_schematics");
2015-09-13 14:04:31 +10:00
if (schematics == null) {
2015-12-20 06:40:42 +11:00
plot.addRunning();
2015-09-13 14:04:31 +10:00
TaskManager.runTaskAsync(new Runnable() {
2015-07-25 03:37:39 +10:00
@Override
2015-09-13 14:04:31 +10:00
public void run() {
2015-09-11 20:09:22 +10:00
final List<String> schematics = SchematicHandler.manager.getSaves(plr.getUUID());
2015-12-20 06:40:42 +11:00
plot.removeRunning();
if ((schematics == null) || (schematics.isEmpty())) {
2015-07-25 03:37:39 +10:00
MainUtil.sendMessage(plr, C.LOAD_FAILED);
return;
}
plr.setMeta("plot_schematics", schematics);
displaySaves(plr, 0);
}
});
2015-09-13 14:04:31 +10:00
} else {
2015-07-25 03:37:39 +10:00
displaySaves(plr, 0);
}
return true;
}
2015-09-13 14:04:31 +10:00
public void displaySaves(final PlotPlayer player, final int page) {
2015-09-11 20:09:22 +10:00
final List<String> schematics = (List<String>) player.getMeta("plot_schematics");
2015-09-13 14:04:31 +10:00
for (int i = 0; i < Math.min(schematics.size(), 32); i++) {
try {
2015-09-11 20:09:22 +10:00
final String schem = schematics.get(i);
final String[] split = schem.split("_");
2015-09-13 14:04:31 +10:00
if (split.length != 6) {
2015-07-25 03:37:39 +10:00
continue;
}
2015-09-11 20:09:22 +10:00
final String time = secToTime((System.currentTimeMillis() / 1000) - (Long.parseLong(split[0])));
final String world = split[1];
final PlotId id = PlotId.fromString(split[2] + ";" + split[3]);
final String size = split[4];
final String server = split[5].replaceAll(".schematic", "");
2015-07-25 03:37:39 +10:00
String color;
2015-09-13 14:04:31 +10:00
if (PS.get().IMP.getServerName().replaceAll("[^A-Za-z0-9]", "").equals(server)) {
2015-07-25 03:37:39 +10:00
color = "$4";
2015-09-13 14:04:31 +10:00
} else {
2015-07-25 03:37:39 +10:00
color = "$1";
}
MainUtil.sendMessage(player, "$3[$2" + (i + 1) + "$3] " + color + time + "$3 | " + color + world + ";" + id + "$3 | " + color + size + "x" + size);
2015-09-13 14:04:31 +10:00
} catch (final Exception e) {
2015-07-25 03:37:39 +10:00
e.printStackTrace();
}
}
MainUtil.sendMessage(player, C.LOAD_LIST);
}
2015-09-13 14:04:31 +10:00
public String secToTime(long time) {
2015-09-11 20:09:22 +10:00
final StringBuilder toreturn = new StringBuilder();
2015-07-25 03:37:39 +10:00
int years = 0;
int weeks = 0;
int days = 0;
int hours = 0;
int minutes = 0;
2015-09-13 14:04:31 +10:00
if (time >= 33868800) {
2015-09-11 20:09:22 +10:00
years = (int) (time / 33868800);
time -= years * 33868800;
toreturn.append(years + "y ");
}
2015-09-13 14:04:31 +10:00
if (time >= 604800) {
2015-09-11 20:09:22 +10:00
weeks = (int) (time / 604800);
time -= weeks * 604800;
toreturn.append(weeks + "w ");
}
2015-09-13 14:04:31 +10:00
if (time >= 86400) {
2015-09-11 20:09:22 +10:00
days = (int) (time / 86400);
time -= days * 86400;
toreturn.append(days + "d ");
}
2015-09-13 14:04:31 +10:00
if (time >= 3600) {
2015-09-11 20:09:22 +10:00
hours = (int) (time / 3600);
time -= hours * 3600;
toreturn.append(hours + "h ");
}
2015-09-13 14:04:31 +10:00
if (time >= 60) {
2015-09-11 20:09:22 +10:00
minutes = (int) (time / 60);
time -= minutes * 60;
toreturn.append(minutes + "m ");
}
2015-09-13 14:04:31 +10:00
if (toreturn.equals("") || (time > 0)) {
2015-09-11 20:09:22 +10:00
toreturn.append((time) + "s ");
2015-07-25 03:37:39 +10:00
}
return toreturn.toString().trim();
}
}