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-26 02:22:25 +02:00
|
|
|
package com.intellectualcrafters.plot.commands;
|
|
|
|
|
2015-01-13 17:38:15 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
|
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;
|
2015-02-22 08:03:25 +01:00
|
|
|
import com.intellectualcrafters.plot.object.Location;
|
2014-11-16 10:48:18 +01:00
|
|
|
import com.intellectualcrafters.plot.object.Plot;
|
|
|
|
import com.intellectualcrafters.plot.object.PlotId;
|
2015-02-21 12:38:44 +01:00
|
|
|
import com.intellectualcrafters.plot.object.PlotPlayer;
|
2014-11-16 10:48:18 +01:00
|
|
|
import com.intellectualcrafters.plot.object.PlotWorld;
|
2015-02-22 07:06:59 +01:00
|
|
|
import com.intellectualcrafters.plot.util.EconHandler;
|
2015-02-23 06:29:45 +01:00
|
|
|
import com.intellectualcrafters.plot.util.EventUtil;
|
2015-02-20 12:23:48 +01:00
|
|
|
import com.intellectualcrafters.plot.util.MainUtil;
|
2015-02-22 08:23:10 +01:00
|
|
|
import com.intellectualcrafters.plot.util.Permissions;
|
2015-02-20 07:28:21 +01:00
|
|
|
import com.intellectualcrafters.plot.util.bukkit.UUIDHandler;
|
2014-12-30 14:09:11 +01:00
|
|
|
|
2014-09-26 02:22:25 +02:00
|
|
|
/**
|
|
|
|
* @author Citymonstret
|
|
|
|
*/
|
|
|
|
public class Merge extends SubCommand {
|
2015-02-20 07:34:19 +01:00
|
|
|
public final static String[] values = new String[] { "north", "east", "south", "west" };
|
|
|
|
public final static String[] aliases = new String[] { "n", "e", "s", "w" };
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2014-11-05 04:42:08 +01:00
|
|
|
public Merge() {
|
|
|
|
super(Command.MERGE, "Merge the plot you are standing on with another plot.", "merge", CommandCategory.ACTIONS, true);
|
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2014-11-05 04:42:08 +01:00
|
|
|
public static String direction(float yaw) {
|
|
|
|
yaw = yaw / 90;
|
|
|
|
final int i = Math.round(yaw);
|
|
|
|
switch (i) {
|
|
|
|
case -4:
|
|
|
|
case 0:
|
|
|
|
case 4:
|
|
|
|
return "SOUTH";
|
|
|
|
case -1:
|
|
|
|
case 3:
|
|
|
|
return "EAST";
|
|
|
|
case -2:
|
|
|
|
case 2:
|
|
|
|
return "NORTH";
|
|
|
|
case -3:
|
|
|
|
case 1:
|
|
|
|
return "WEST";
|
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2014-11-05 04:42:08 +01:00
|
|
|
@Override
|
2015-02-21 08:30:55 +01:00
|
|
|
public boolean execute(final PlotPlayer plr, final String... args) {
|
2015-02-23 02:32:27 +01:00
|
|
|
final Location loc = plr.getLocation();
|
2015-02-21 13:01:15 +01:00
|
|
|
final Plot plot = MainUtil.getPlot(loc);
|
2015-02-22 08:03:25 +01:00
|
|
|
if (plot == null) {
|
|
|
|
return !sendMessage(plr, C.NOT_IN_PLOT);
|
|
|
|
}
|
2014-11-05 04:42:08 +01:00
|
|
|
if ((plot == null) || !plot.hasOwner()) {
|
2015-02-21 12:24:46 +01:00
|
|
|
MainUtil.sendMessage(plr, C.PLOT_UNOWNED);
|
2014-11-05 04:42:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-02-21 12:24:46 +01:00
|
|
|
final boolean admin = Permissions.hasPermission(plr, "plots.admin.command.merge");
|
2015-01-01 14:28:57 +01:00
|
|
|
if (!plot.getOwner().equals(UUIDHandler.getUUID(plr)) && !admin) {
|
2015-02-21 12:24:46 +01:00
|
|
|
MainUtil.sendMessage(plr, C.NO_PLOT_PERMS);
|
2014-11-05 04:42:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (args.length < 1) {
|
2015-02-21 12:24:46 +01:00
|
|
|
MainUtil.sendMessage(plr, C.SUBCOMMAND_SET_OPTIONS_HEADER.s() + StringUtils.join(values, C.BLOCK_LIST_SEPARATER.s()));
|
|
|
|
MainUtil.sendMessage(plr, C.DIRECTION.s().replaceAll("%dir%", direction(plr.getLocation().getYaw())));
|
2014-11-05 04:42:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int direction = -1;
|
|
|
|
for (int i = 0; i < values.length; i++) {
|
|
|
|
if (args[0].equalsIgnoreCase(values[i]) || args[0].equalsIgnoreCase(aliases[i])) {
|
|
|
|
direction = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (direction == -1) {
|
2015-02-21 12:24:46 +01:00
|
|
|
MainUtil.sendMessage(plr, C.SUBCOMMAND_SET_OPTIONS_HEADER.s() + StringUtils.join(values, C.BLOCK_LIST_SEPARATER.s()));
|
|
|
|
MainUtil.sendMessage(plr, C.DIRECTION.s().replaceAll("%dir%", direction(plr.getLocation().getYaw())));
|
2014-11-05 04:42:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-02-22 08:23:10 +01:00
|
|
|
PlotId bot = MainUtil.getBottomPlot(plot).id;
|
|
|
|
PlotId top = MainUtil.getTopPlot(plot).id;
|
2014-11-05 04:42:08 +01:00
|
|
|
ArrayList<PlotId> plots;
|
2015-02-23 02:32:27 +01:00
|
|
|
final String world = plr.getLocation().getWorld();
|
2014-11-05 04:42:08 +01:00
|
|
|
switch (direction) {
|
|
|
|
case 0: // north = -y
|
2015-02-22 08:23:10 +01:00
|
|
|
plots = MainUtil.getMaxPlotSelectionIds(world, new PlotId(bot.x, bot.y - 1), new PlotId(top.x, top.y));
|
2014-11-05 04:42:08 +01:00
|
|
|
break;
|
|
|
|
case 1: // east = +x
|
2015-02-22 08:23:10 +01:00
|
|
|
plots = MainUtil.getMaxPlotSelectionIds(world, new PlotId(bot.x, bot.y), new PlotId(top.x + 1, top.y));
|
2014-11-05 04:42:08 +01:00
|
|
|
break;
|
|
|
|
case 2: // south = +y
|
2015-02-22 08:23:10 +01:00
|
|
|
plots = MainUtil.getMaxPlotSelectionIds(world, new PlotId(bot.x, bot.y), new PlotId(top.x, top.y + 1));
|
2014-11-05 04:42:08 +01:00
|
|
|
break;
|
|
|
|
case 3: // west = -x
|
2015-02-22 08:23:10 +01:00
|
|
|
plots = MainUtil.getMaxPlotSelectionIds(world, new PlotId(bot.x - 1, bot.y), new PlotId(top.x, top.y));
|
2014-11-05 04:42:08 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-20 07:34:19 +01:00
|
|
|
final PlotId botId = plots.get(0);
|
|
|
|
final PlotId topId = plots.get(plots.size() - 1);
|
2015-02-22 08:23:10 +01:00
|
|
|
final PlotId bot1 = MainUtil.getBottomPlot(MainUtil.getPlot(world, botId)).id;
|
|
|
|
final PlotId bot2 = MainUtil.getBottomPlot(MainUtil.getPlot(world, topId)).id;
|
|
|
|
final PlotId top1 = MainUtil.getTopPlot(MainUtil.getPlot(world, topId)).id;
|
|
|
|
final PlotId top2 = MainUtil.getTopPlot(MainUtil.getPlot(world, botId)).id;
|
2015-01-05 16:51:35 +01:00
|
|
|
bot = new PlotId(Math.min(bot1.x, bot2.x), Math.min(bot1.y, bot2.y));
|
2015-01-06 04:33:37 +01:00
|
|
|
top = new PlotId(Math.max(top1.x, top2.x), Math.max(top1.y, top2.y));
|
2015-02-22 08:23:10 +01:00
|
|
|
plots = MainUtil.getMaxPlotSelectionIds(world, bot, top);
|
2014-11-05 04:42:08 +01:00
|
|
|
for (final PlotId myid : plots) {
|
2015-02-19 09:51:10 +01:00
|
|
|
final Plot myplot = PlotSquared.getPlots(world).get(myid);
|
2015-01-01 14:22:59 +01:00
|
|
|
if ((myplot == null) || !myplot.hasOwner() || !(myplot.getOwner().equals(UUIDHandler.getUUID(plr)) || admin)) {
|
2015-02-21 12:24:46 +01:00
|
|
|
MainUtil.sendMessage(plr, C.NO_PERM_MERGE.s().replaceAll("%plot%", myid.toString()));
|
2014-11-05 04:42:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-02-20 09:55:04 +01:00
|
|
|
final PlotWorld plotWorld = PlotSquared.getPlotWorld(world);
|
2015-02-23 02:32:27 +01:00
|
|
|
if ((PlotSquared.economy != null) && plotWorld.USE_ECONOMY) {
|
2014-11-05 04:42:08 +01:00
|
|
|
double cost = plotWorld.MERGE_PRICE;
|
|
|
|
cost = plots.size() * cost;
|
|
|
|
if (cost > 0d) {
|
2015-02-22 08:23:10 +01:00
|
|
|
if (EconHandler.getBalance(plr) < cost) {
|
2014-11-05 04:42:08 +01:00
|
|
|
sendMessage(plr, C.CANNOT_AFFORD_MERGE, cost + "");
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-21 13:09:20 +01:00
|
|
|
EconHandler.withdrawPlayer(plr, cost);
|
2014-11-05 04:42:08 +01:00
|
|
|
sendMessage(plr, C.REMOVED_BALANCE, cost + "");
|
|
|
|
}
|
|
|
|
}
|
2015-02-23 06:29:45 +01:00
|
|
|
final boolean result = EventUtil.manager.callMerge(world, plot, plots);
|
|
|
|
if (!result) {
|
2015-02-21 12:24:46 +01:00
|
|
|
MainUtil.sendMessage(plr, "&cMerge has been cancelled");
|
2014-11-05 04:42:08 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-02-21 12:24:46 +01:00
|
|
|
MainUtil.sendMessage(plr, "&cPlots have been merged");
|
2015-02-20 12:23:48 +01:00
|
|
|
MainUtil.mergePlots(world, plots, true);
|
2015-02-22 08:23:10 +01:00
|
|
|
MainUtil.setSign(UUIDHandler.getName(plot.owner), plot);
|
2015-02-20 12:23:48 +01:00
|
|
|
MainUtil.update(plr.getLocation());
|
2014-11-05 04:42:08 +01:00
|
|
|
return true;
|
|
|
|
}
|
2014-09-26 02:22:25 +02:00
|
|
|
}
|