2014-11-08 20:27:09 +01: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 /
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
2014-09-22 13:02:14 +02:00
|
|
|
package com.intellectualcrafters.plot.commands;
|
2014-10-03 04:36:30 +02:00
|
|
|
|
2015-01-13 17:38:15 +01:00
|
|
|
import net.milkbowl.vault.economy.Economy;
|
|
|
|
|
2015-02-19 09:51:10 +01:00
|
|
|
import com.intellectualcrafters.plot.PlotSquared;
|
2014-11-16 10:48:18 +01:00
|
|
|
import com.intellectualcrafters.plot.config.C;
|
2014-12-15 02:04:13 +01:00
|
|
|
import com.intellectualcrafters.plot.config.Settings;
|
2015-02-21 12:38:44 +01:00
|
|
|
import com.intellectualcrafters.plot.object.Location;
|
2014-11-16 10:48:18 +01:00
|
|
|
import com.intellectualcrafters.plot.object.Plot;
|
2015-01-27 22:51:56 +01:00
|
|
|
import com.intellectualcrafters.plot.object.PlotCluster;
|
2014-11-16 10:48:18 +01:00
|
|
|
import com.intellectualcrafters.plot.object.PlotId;
|
2015-02-21 12:24:46 +01:00
|
|
|
import com.intellectualcrafters.plot.object.PlotPlayer;
|
2014-11-16 10:48:18 +01:00
|
|
|
import com.intellectualcrafters.plot.object.PlotWorld;
|
2015-01-27 22:51:56 +01:00
|
|
|
import com.intellectualcrafters.plot.util.ClusterManager;
|
2015-02-20 12:23:48 +01:00
|
|
|
import com.intellectualcrafters.plot.util.MainUtil;
|
2015-02-21 12:38:44 +01:00
|
|
|
import com.intellectualcrafters.plot.util.Permissions;
|
2014-12-15 02:04:13 +01:00
|
|
|
|
2015-01-14 00:51:26 +01:00
|
|
|
public class Auto extends SubCommand {
|
2014-11-05 04:42:08 +01:00
|
|
|
public Auto() {
|
|
|
|
super("auto", "plots.auto", "Claim the nearest plot", "auto", "a", CommandCategory.CLAIMING, true);
|
|
|
|
}
|
2015-02-20 07:34:19 +01:00
|
|
|
|
2014-11-16 10:48:18 +01:00
|
|
|
public static PlotId getNextPlot(final PlotId id, final int step) {
|
|
|
|
final int absX = Math.abs(id.x);
|
|
|
|
final int absY = Math.abs(id.y);
|
|
|
|
if (absX > absY) {
|
|
|
|
if (id.x > 0) {
|
|
|
|
return new PlotId(id.x, id.y + 1);
|
2014-12-18 03:15:11 +01:00
|
|
|
} else {
|
2014-11-16 10:48:18 +01:00
|
|
|
return new PlotId(id.x, id.y - 1);
|
|
|
|
}
|
2014-12-18 03:15:11 +01:00
|
|
|
} else if (absY > absX) {
|
2014-11-16 10:48:18 +01:00
|
|
|
if (id.y > 0) {
|
|
|
|
return new PlotId(id.x - 1, id.y);
|
2014-12-18 03:15:11 +01:00
|
|
|
} else {
|
2014-11-16 10:48:18 +01:00
|
|
|
return new PlotId(id.x + 1, id.y);
|
|
|
|
}
|
2014-12-18 03:15:11 +01:00
|
|
|
} else {
|
2014-11-16 10:48:18 +01:00
|
|
|
if (id.x.equals(id.y) && (id.x > 0)) {
|
|
|
|
return new PlotId(id.x, id.y + step);
|
|
|
|
}
|
|
|
|
if (id.x == absX) {
|
|
|
|
return new PlotId(id.x, id.y + 1);
|
|
|
|
}
|
|
|
|
if (id.y == absY) {
|
|
|
|
return new PlotId(id.x, id.y - 1);
|
|
|
|
}
|
|
|
|
return new PlotId(id.x + 1, id.y);
|
|
|
|
}
|
|
|
|
}
|
2015-02-20 07:34:19 +01:00
|
|
|
|
2014-11-05 04:42:08 +01:00
|
|
|
// TODO auto claim a mega plot with schematic
|
|
|
|
@Override
|
2015-02-21 08:30:55 +01:00
|
|
|
public boolean execute(final PlotPlayer plr, final String... args) {
|
2015-02-21 12:24:46 +01:00
|
|
|
String world;
|
2014-11-05 04:42:08 +01:00
|
|
|
int size_x = 1;
|
|
|
|
int size_z = 1;
|
|
|
|
String schematic = "";
|
2015-02-19 09:51:10 +01:00
|
|
|
if (PlotSquared.getPlotWorlds().size() == 1) {
|
2015-02-21 12:24:46 +01:00
|
|
|
world = PlotSquared.getPlotWorlds().iterator().next();
|
2014-12-18 03:15:11 +01:00
|
|
|
} else {
|
2015-02-21 12:24:46 +01:00
|
|
|
world = plr.getLocation().getWorld();
|
|
|
|
if (!PlotSquared.isPlotWorld(world)) {
|
|
|
|
MainUtil.sendMessage(plr, C.NOT_IN_PLOT_WORLD);
|
2014-11-05 04:42:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (args.length > 0) {
|
2015-02-21 12:24:46 +01:00
|
|
|
if (Permissions.hasPermission(plr, "plots.auto.mega")) {
|
2014-11-05 04:42:08 +01:00
|
|
|
try {
|
|
|
|
final String[] split = args[0].split(",");
|
|
|
|
size_x = Integer.parseInt(split[0]);
|
|
|
|
size_z = Integer.parseInt(split[1]);
|
|
|
|
if ((size_x < 1) || (size_z < 1)) {
|
2015-02-21 12:24:46 +01:00
|
|
|
MainUtil.sendMessage(plr, "&cError: size<=0");
|
2014-11-05 04:42:08 +01:00
|
|
|
}
|
|
|
|
if ((size_x > 4) || (size_z > 4)) {
|
2015-02-21 12:24:46 +01:00
|
|
|
MainUtil.sendMessage(plr, "&cError: size>4");
|
2014-11-05 04:42:08 +01:00
|
|
|
}
|
|
|
|
if (args.length > 1) {
|
|
|
|
schematic = args[1];
|
|
|
|
}
|
2014-12-18 03:15:11 +01:00
|
|
|
} catch (final Exception e) {
|
2014-12-15 02:04:13 +01:00
|
|
|
size_x = 1;
|
|
|
|
size_z = 1;
|
2014-11-05 04:42:08 +01:00
|
|
|
schematic = args[0];
|
|
|
|
// PlayerFunctions.sendMessage(plr,
|
|
|
|
// "&cError: Invalid size (X,Y)");
|
|
|
|
// return false;
|
|
|
|
}
|
2014-12-18 03:15:11 +01:00
|
|
|
} else {
|
2014-11-05 04:42:08 +01:00
|
|
|
schematic = args[0];
|
|
|
|
// PlayerFunctions.sendMessage(plr, C.NO_PERMISSION);
|
|
|
|
// return false;
|
|
|
|
}
|
|
|
|
}
|
2014-12-16 06:03:20 +01:00
|
|
|
if ((size_x * size_z) > Settings.MAX_AUTO_SIZE) {
|
2015-02-21 12:24:46 +01:00
|
|
|
MainUtil.sendMessage(plr, C.CANT_CLAIM_MORE_PLOTS_NUM, Settings.MAX_AUTO_SIZE + "");
|
2014-12-15 02:04:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-02-21 12:38:44 +01:00
|
|
|
int currentPlots = MainUtil.getPlayerPlotCount(world, plr);
|
|
|
|
final int diff = currentPlots - MainUtil.getAllowedPlots(plr, currentPlots);
|
2014-12-20 05:44:24 +01:00
|
|
|
if ((diff + (size_x * size_z)) > 0) {
|
2014-12-15 02:04:13 +01:00
|
|
|
if (diff < 0) {
|
2015-02-21 12:24:46 +01:00
|
|
|
MainUtil.sendMessage(plr, C.CANT_CLAIM_MORE_PLOTS_NUM, (-diff) + "");
|
2014-12-18 03:15:11 +01:00
|
|
|
} else {
|
2015-02-21 12:24:46 +01:00
|
|
|
MainUtil.sendMessage(plr, C.CANT_CLAIM_MORE_PLOTS);
|
2014-12-15 02:04:13 +01:00
|
|
|
}
|
2014-11-05 04:42:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-02-21 12:38:44 +01:00
|
|
|
final PlotWorld pWorld = PlotSquared.getPlotWorld(world);
|
2015-02-19 09:51:10 +01:00
|
|
|
if ((PlotSquared.economy != null) && pWorld.USE_ECONOMY) {
|
2014-11-05 04:42:08 +01:00
|
|
|
double cost = pWorld.PLOT_PRICE;
|
|
|
|
cost = (size_x * size_z) * cost;
|
|
|
|
if (cost > 0d) {
|
2015-02-19 09:51:10 +01:00
|
|
|
final Economy economy = PlotSquared.economy;
|
2015-02-21 12:38:44 +01:00
|
|
|
if (economy.getBalance(plr.getName()) < cost) {
|
2014-11-05 04:42:08 +01:00
|
|
|
sendMessage(plr, C.CANNOT_AFFORD_PLOT, "" + cost);
|
|
|
|
return true;
|
|
|
|
}
|
2015-02-21 13:09:20 +01:00
|
|
|
EconHandler.withdrawPlayer(plr.getName(), cost);
|
2014-11-05 04:42:08 +01:00
|
|
|
sendMessage(plr, C.REMOVED_BALANCE, cost + "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!schematic.equals("")) {
|
2014-12-16 06:03:20 +01:00
|
|
|
// if (pWorld.SCHEMATIC_CLAIM_SPECIFY) {
|
2014-12-15 02:04:13 +01:00
|
|
|
if (!pWorld.SCHEMATICS.contains(schematic.toLowerCase())) {
|
|
|
|
sendMessage(plr, C.SCHEMATIC_INVALID, "non-existent: " + schematic);
|
|
|
|
return true;
|
2014-11-05 04:42:08 +01:00
|
|
|
}
|
2015-02-21 12:24:46 +01:00
|
|
|
if (!Permissions.hasPermission(plr, "plots.claim." + schematic) && !plr.hasPermission("plots.admin.command.schematic")) {
|
|
|
|
MainUtil.sendMessage(plr, C.NO_SCHEMATIC_PERMISSION, schematic);
|
2014-12-15 02:04:13 +01:00
|
|
|
return true;
|
|
|
|
}
|
2014-12-16 06:03:20 +01:00
|
|
|
// }
|
2014-11-05 04:42:08 +01:00
|
|
|
}
|
2015-02-21 12:38:44 +01:00
|
|
|
final String worldname = world;
|
2015-02-20 09:55:04 +01:00
|
|
|
final PlotWorld plotworld = PlotSquared.getPlotWorld(worldname);
|
2015-02-08 00:44:53 +01:00
|
|
|
if (plotworld.TYPE == 2) {
|
2015-02-21 12:38:44 +01:00
|
|
|
final Location loc = plr.getLocation();
|
|
|
|
final Plot plot = MainUtil.getPlot(new Location(worldname, loc.getX(), loc.getY(), loc.getZ()));
|
2015-02-20 07:34:19 +01:00
|
|
|
if (plot == null) {
|
|
|
|
return sendMessage(plr, C.NOT_IN_PLOT);
|
|
|
|
}
|
|
|
|
final PlotCluster cluster = ClusterManager.getCluster(loc);
|
|
|
|
// Must be standing in a cluster
|
|
|
|
if (cluster == null) {
|
2015-02-21 12:24:46 +01:00
|
|
|
MainUtil.sendMessage(plr, C.NOT_IN_CLUSTER);
|
2015-01-29 07:02:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-02-20 07:34:19 +01:00
|
|
|
final PlotId bot = cluster.getP1();
|
|
|
|
final PlotId top = cluster.getP2();
|
|
|
|
final PlotId origin = new PlotId((bot.x + top.x) / 2, (bot.y + top.y) / 2);
|
|
|
|
PlotId id = new PlotId(0, 0);
|
|
|
|
final int width = Math.max((top.x - bot.x) + 1, (top.y - bot.y) + 1);
|
|
|
|
final int max = width * width;
|
|
|
|
//
|
|
|
|
for (int i = 0; i <= max; i++) {
|
|
|
|
final PlotId currentId = new PlotId(origin.x + id.x, origin.y + id.y);
|
2015-02-20 12:23:48 +01:00
|
|
|
final Plot current = MainUtil.getPlot(worldname, currentId);
|
2015-02-20 07:34:19 +01:00
|
|
|
if ((current != null) && (current.hasOwner() == false) && (current.settings.isMerged() == false) && cluster.equals(ClusterManager.getCluster(current))) {
|
|
|
|
Claim.claimPlot(plr, current, true, true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
id = getNextPlot(id, 1);
|
|
|
|
}
|
|
|
|
// no free plots
|
2015-02-21 12:24:46 +01:00
|
|
|
MainUtil.sendMessage(plr, C.NO_FREE_PLOTS);
|
2015-02-20 07:34:19 +01:00
|
|
|
return false;
|
2015-01-27 22:51:56 +01:00
|
|
|
}
|
2014-11-05 04:42:08 +01:00
|
|
|
boolean br = false;
|
|
|
|
if ((size_x == 1) && (size_z == 1)) {
|
|
|
|
while (!br) {
|
2015-02-20 12:23:48 +01:00
|
|
|
final Plot plot = MainUtil.getPlot(worldname, getLastPlot(worldname));
|
2015-01-02 15:38:04 +01:00
|
|
|
if ((plot.owner == null)) {
|
2014-11-19 18:05:09 +01:00
|
|
|
Claim.claimPlot(plr, plot, true, true);
|
2014-10-31 23:29:08 +01:00
|
|
|
br = true;
|
2014-10-29 07:18:17 +01:00
|
|
|
}
|
2015-02-20 12:23:48 +01:00
|
|
|
MainUtil.lastPlot.put(worldname, getNextPlot(getLastPlot(worldname), 1));
|
2014-11-05 04:42:08 +01:00
|
|
|
}
|
2014-12-18 03:15:11 +01:00
|
|
|
} else {
|
2014-12-15 02:04:13 +01:00
|
|
|
boolean lastPlot = true;
|
2014-11-05 04:42:08 +01:00
|
|
|
while (!br) {
|
2015-01-14 00:51:26 +01:00
|
|
|
final PlotId start = getNextPlot(getLastPlot(worldname), 1);
|
2014-11-10 10:15:55 +01:00
|
|
|
// Checking if the current set of plots is a viable option.
|
2015-02-20 12:23:48 +01:00
|
|
|
MainUtil.lastPlot.put(worldname, start);
|
2014-12-15 02:04:13 +01:00
|
|
|
if (lastPlot) {
|
2014-11-05 04:42:08 +01:00
|
|
|
}
|
2015-02-19 11:12:26 +01:00
|
|
|
if ((PlotSquared.getPlots(worldname).get(start) != null) && (PlotSquared.getPlots(worldname).get(start).owner != null)) {
|
2014-12-15 02:04:13 +01:00
|
|
|
continue;
|
2014-12-18 03:15:11 +01:00
|
|
|
} else {
|
2014-12-15 02:04:13 +01:00
|
|
|
lastPlot = false;
|
|
|
|
}
|
2014-11-05 04:42:08 +01:00
|
|
|
final PlotId end = new PlotId((start.x + size_x) - 1, (start.y + size_z) - 1);
|
2015-02-20 12:23:48 +01:00
|
|
|
if (MainUtil.isUnowned(worldname, start, end)) {
|
2014-10-29 07:18:17 +01:00
|
|
|
for (int i = start.x; i <= end.x; i++) {
|
|
|
|
for (int j = start.y; j <= end.y; j++) {
|
2015-02-20 12:23:48 +01:00
|
|
|
final Plot plot = MainUtil.getPlot(worldname, new PlotId(i, j));
|
2014-11-05 04:42:08 +01:00
|
|
|
final boolean teleport = ((i == end.x) && (j == end.y));
|
2014-11-19 18:05:09 +01:00
|
|
|
Claim.claimPlot(plr, plot, teleport, true);
|
2014-10-29 07:18:17 +01:00
|
|
|
}
|
|
|
|
}
|
2015-02-21 12:38:44 +01:00
|
|
|
if (!MainUtil.mergePlots(worldname, MainUtil.getPlotSelectionIds(start, end), true)) {
|
2014-10-29 07:18:17 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
br = true;
|
|
|
|
}
|
2014-11-05 04:42:08 +01:00
|
|
|
}
|
|
|
|
}
|
2015-02-20 12:23:48 +01:00
|
|
|
MainUtil.lastPlot.put(worldname, new PlotId(0, 0));
|
2014-11-05 04:42:08 +01:00
|
|
|
return true;
|
|
|
|
}
|
2015-01-14 00:51:26 +01:00
|
|
|
|
2015-02-20 07:34:19 +01:00
|
|
|
public PlotId getLastPlot(final String world) {
|
2015-02-20 12:23:48 +01:00
|
|
|
if ((MainUtil.lastPlot == null) || !MainUtil.lastPlot.containsKey(world)) {
|
|
|
|
MainUtil.lastPlot.put(world, new PlotId(0, 0));
|
2015-02-20 07:34:19 +01:00
|
|
|
}
|
2015-02-20 12:23:48 +01:00
|
|
|
return MainUtil.lastPlot.get(world);
|
2015-02-20 07:34:19 +01:00
|
|
|
}
|
2014-10-03 04:36:30 +02:00
|
|
|
}
|