TF-PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/SchematicCmd.java

244 lines
12 KiB
Java
Raw Normal View History

2015-02-25 23:25:28 +11:00
////////////////////////////////////////////////////////////////////////////////////////////////////
// PlotSquared - A plot manager and world generator for the Bukkit API /
// Copyright (c) 2014 IntellectualSites/IntellectualCrafters /
// /
// 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, write to the Free Software Foundation, /
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA /
// /
// You can contact us via: support@intellectualsites.com /
////////////////////////////////////////////////////////////////////////////////////////////////////
package com.intellectualcrafters.plot.commands;
2015-07-31 00:25:16 +10:00
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.UUID;
import com.intellectualcrafters.plot.PS;
2015-02-25 23:25:28 +11:00
import com.intellectualcrafters.plot.config.C;
2015-07-24 03:14:36 +10:00
import com.intellectualcrafters.plot.config.Settings;
2015-07-31 00:25:16 +10:00
import com.intellectualcrafters.plot.object.ConsolePlayer;
import com.intellectualcrafters.plot.object.Location;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.object.RunnableVal;
2015-07-06 01:44:10 +10:00
import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.Permissions;
2015-07-06 01:44:10 +10:00
import com.intellectualcrafters.plot.util.SchematicHandler;
2015-02-25 23:25:28 +11:00
import com.intellectualcrafters.plot.util.SchematicHandler.Schematic;
2015-07-06 01:44:10 +10:00
import com.intellectualcrafters.plot.util.TaskManager;
2015-07-27 19:50:04 +02:00
import com.plotsquared.general.commands.CommandDeclaration;
2015-07-26 21:33:54 +02:00
@CommandDeclaration(
2015-09-11 20:09:22 +10:00
command = "schematic",
permission = "plots.schematic",
description = "Schematic command",
aliases = { "sch" },
category = CommandCategory.ACTIONS,
usage = "/plot schematic <arg...>")
2015-09-13 14:04:31 +10:00
public class SchematicCmd extends SubCommand {
2015-02-25 23:25:28 +11:00
private boolean running = false;
2015-09-13 14:04:31 +10:00
2015-02-25 23:25:28 +11:00
@Override
2015-09-13 14:04:31 +10:00
public boolean onCommand(final PlotPlayer plr, final String... args) {
if (args.length < 1) {
2015-02-25 23:25:28 +11:00
sendMessage(plr, C.SCHEMATIC_MISSING_ARG);
return true;
}
final String arg = args[0].toLowerCase();
final String file;
final Schematic schematic;
2015-09-13 14:04:31 +10:00
switch (arg) {
case "paste": {
if (!Permissions.hasPermission(plr, "plots.schematic.paste")) {
2015-02-25 23:25:28 +11:00
MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.schematic.paste");
return false;
}
2015-09-13 14:04:31 +10:00
if (args.length < 2) {
2015-02-25 23:25:28 +11:00
sendMessage(plr, C.SCHEMATIC_MISSING_ARG);
break;
}
final Location loc = plr.getLocation();
2015-09-22 23:23:28 +10:00
final Plot plot = MainUtil.getPlotAbs(loc);
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.schematic.paste")) {
2015-07-25 03:37:39 +10:00
MainUtil.sendMessage(plr, C.NO_PLOT_PERMS);
return false;
2015-02-25 23:25:28 +11:00
}
2015-09-13 14:04:31 +10:00
if (running) {
2015-02-25 23:25:28 +11:00
MainUtil.sendMessage(plr, "&cTask is already running.");
return false;
}
2015-07-24 03:14:36 +10:00
final String location = args[1];
2015-09-11 20:09:22 +10:00
running = true;
2015-09-13 14:04:31 +10:00
TaskManager.runTaskAsync(new Runnable() {
2015-02-25 23:25:28 +11:00
@Override
2015-09-13 14:04:31 +10:00
public void run() {
2015-07-24 03:14:36 +10:00
Schematic schematic;
2015-09-13 14:04:31 +10:00
if (location.startsWith("url:")) {
try {
2015-09-11 20:09:22 +10:00
final UUID uuid = UUID.fromString(location.substring(4));
final URL base = new URL(Settings.WEB_URL);
final URL url = new URL(base, "uploads/" + uuid + ".schematic");
2015-07-24 03:14:36 +10:00
schematic = SchematicHandler.manager.getSchematic(url);
2015-09-13 14:04:31 +10:00
} catch (final Exception e) {
2015-07-24 03:14:36 +10:00
e.printStackTrace();
sendMessage(plr, C.SCHEMATIC_INVALID, "non-existent url: " + location);
2015-09-11 20:09:22 +10:00
running = false;
2015-07-24 03:14:36 +10:00
return;
}
2015-09-13 14:04:31 +10:00
} else {
2015-07-24 03:14:36 +10:00
schematic = SchematicHandler.manager.getSchematic(location);
}
2015-09-13 14:04:31 +10:00
if (schematic == null) {
2015-09-11 20:09:22 +10:00
running = false;
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>() {
@Override
2015-09-13 14:04:31 +10:00
public void run() {
2015-09-11 20:09:22 +10:00
running = false;
2015-09-13 14:04:31 +10:00
if (value) {
sendMessage(plr, C.SCHEMATIC_PASTE_SUCCESS);
2015-09-13 14:04:31 +10:00
} else {
sendMessage(plr, C.SCHEMATIC_PASTE_FAILED);
}
}
});
2015-02-25 23:25:28 +11:00
}
});
break;
}
2015-09-22 23:23:28 +10:00
// TODO test
// case "test": {
// if (!Permissions.hasPermission(plr, "plots.schematic.test")) {
// MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.schematic.test");
// return false;
// }
// if (args.length < 2) {
// sendMessage(plr, C.SCHEMATIC_MISSING_ARG);
// return false;
// }
// final Location loc = plr.getLocation();
// final Plot plot = MainUtil.getPlot(loc);
// if (plot == null) {
// sendMessage(plr, C.NOT_IN_PLOT);
// return false;
// }
// file = args[1];
// schematic = SchematicHandler.manager.getSchematic(file);
// if (schematic == null) {
// sendMessage(plr, C.SCHEMATIC_INVALID, "non-existent");
// return false;
// }
// final int l1 = schematic.getSchematicDimension().getX();
// final int l2 = schematic.getSchematicDimension().getZ();
// final int length = MainUtil.getPlotWidth(loc.getWorld(), plot.id);
// if ((l1 < length) || (l2 < length)) {
// sendMessage(plr, C.SCHEMATIC_INVALID, String.format("Wrong size (x: %s, z: %d) vs %d ", l1, l2, length));
// break;
// }
// sendMessage(plr, C.SCHEMATIC_VALID);
// break;
// }
2015-02-25 23:25:28 +11:00
case "saveall":
2015-09-13 14:04:31 +10:00
case "exportall": {
if (!ConsolePlayer.isConsole(plr)) {
2015-02-25 23:25:28 +11:00
MainUtil.sendMessage(plr, C.NOT_CONSOLE);
return false;
}
2015-09-13 14:04:31 +10:00
if (args.length != 2) {
2015-10-19 17:27:51 +11:00
MainUtil.sendMessage(plr, "&cNeed world arg. Use &7/plots sch exportall <world>");
2015-02-25 23:25:28 +11:00
return false;
}
2015-09-11 20:09:22 +10:00
final Collection<Plot> plots = PS.get().getPlotsInWorld(args[1]);
2015-09-13 14:04:31 +10:00
if ((plots.size() == 0)) {
2015-04-16 14:04:11 +10:00
MainUtil.sendMessage(plr, "&cInvalid world. Use &7/plots sch exportall <world>");
2015-02-25 23:25:28 +11:00
return false;
}
2015-09-13 14:04:31 +10:00
final boolean result = SchematicHandler.manager.exportAll(plots, null, null, new Runnable() {
2015-09-11 20:09:22 +10:00
@Override
2015-09-13 14:04:31 +10:00
public void run() {
2015-09-11 20:09:22 +10:00
MainUtil.sendMessage(plr, "&aFinished mass export");
}
});
2015-09-13 14:04:31 +10:00
if (!result) {
2015-09-11 20:09:22 +10:00
MainUtil.sendMessage(plr, "&cTask is already running.");
2015-02-25 23:25:28 +11:00
return false;
2015-09-13 14:04:31 +10:00
} else {
2015-09-11 20:09:22 +10:00
MainUtil.sendMessage(plr, "&3PlotSquared&8->&3Schemaitc&8: &7Mass export has started. This may take a while.");
MainUtil.sendMessage(plr, "&3PlotSquared&8->&3Schemaitc&8: &7Found &c" + plots.size() + "&7 plots...");
2015-04-16 14:04:11 +10:00
}
2015-02-25 23:25:28 +11:00
break;
}
case "export":
2015-09-13 14:04:31 +10:00
case "save": {
if (!Permissions.hasPermission(plr, "plots.schematic.save")) {
2015-02-25 23:25:28 +11:00
MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.schematic.save");
return false;
}
2015-09-13 14:04:31 +10:00
if (running) {
2015-02-25 23:25:28 +11:00
MainUtil.sendMessage(plr, "&cTask is already running.");
return false;
}
final Plot p2;
2015-07-28 01:14:38 +10:00
final Location loc = plr.getLocation();
2015-09-22 23:23:28 +10:00
final Plot plot = MainUtil.getPlotAbs(loc);
2015-09-13 14:04:31 +10:00
if (plot == null) {
return !sendMessage(plr, C.NOT_IN_PLOT);
}
if (!plot.hasOwner()) {
2015-07-28 01:14:38 +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.schematic.save")) {
2015-07-28 01:14:38 +10:00
MainUtil.sendMessage(plr, C.NO_PLOT_PERMS);
return false;
2015-02-25 23:25:28 +11:00
}
2015-07-28 01:14:38 +10:00
p2 = plot;
2015-09-11 20:09:22 +10:00
loc.getWorld();
final Collection<Plot> plots = new ArrayList<Plot>();
2015-04-16 14:04:11 +10:00
plots.add(p2);
2015-09-13 14:04:31 +10:00
final boolean result = SchematicHandler.manager.exportAll(plots, null, null, new Runnable() {
2015-09-11 20:09:22 +10:00
@Override
2015-09-13 14:04:31 +10:00
public void run() {
2015-09-11 20:09:22 +10:00
MainUtil.sendMessage(plr, "&aFinished export");
running = false;
}
});
2015-09-13 14:04:31 +10:00
if (!result) {
2015-09-11 20:09:22 +10:00
MainUtil.sendMessage(plr, "&cTask is already running.");
2015-04-16 14:04:11 +10:00
return false;
2015-09-13 14:04:31 +10:00
} else {
2015-09-11 20:09:22 +10:00
MainUtil.sendMessage(plr, "&7Starting export...");
2015-04-16 14:04:11 +10:00
}
2015-02-25 23:25:28 +11:00
break;
}
2015-09-13 14:04:31 +10:00
default: {
2015-02-25 23:25:28 +11:00
sendMessage(plr, C.SCHEMATIC_MISSING_ARG);
break;
}
}
return true;
}
}