PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Merge.java

233 lines
11 KiB
Java
Raw Normal View History

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 /
////////////////////////////////////////////////////////////////////////////////////////////////////
package com.intellectualcrafters.plot.commands;
2015-07-27 19:50:04 +02:00
import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.object.*;
import com.intellectualcrafters.plot.util.*;
import com.plotsquared.general.commands.Argument;
import com.plotsquared.general.commands.CommandDeclaration;
import org.apache.commons.lang.StringUtils;
2015-07-03 11:30:26 +02:00
import java.util.ArrayList;
import java.util.HashSet;
import java.util.UUID;
2014-12-30 14:09:11 +01:00
@CommandDeclaration(
command = "merge",
aliases = {"m"},
description = "Merge the plot you are standing on, with another plot",
permission = "plots.merge",
usage = "/plot merge [direction]",
category = CommandCategory.ACTIONS,
2015-07-27 15:10:14 +02:00
requiredType = RequiredType.PLAYER
)
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() {
requiredArguments = new Argument[] {
Argument.String
};
2014-11-05 04:42:08 +01:00
}
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-07-27 15:10:14 +02:00
public boolean onCommand(final PlotPlayer plr, final String[] args) {
2015-02-23 12:37:36 +01:00
final Location loc = plr.getLocationFull();
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;
}
final boolean admin = Permissions.hasPermission(plr, "plots.admin.command.merge");
2015-03-20 04:00:02 +01:00
if (!plot.isOwner(plr.getUUID()) && !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()));
2015-02-26 12:42:28 +01:00
MainUtil.sendMessage(plr, C.DIRECTION.s().replaceAll("%dir%", direction(loc.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()));
2015-02-26 12:42:28 +01:00
MainUtil.sendMessage(plr, C.DIRECTION.s().replaceAll("%dir%", direction(loc.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;
2015-03-20 03:13:27 +01:00
ArrayList<PlotId> selPlots;
2015-02-26 12:42:28 +01:00
final String world = loc.getWorld();
2014-11-05 04:42:08 +01:00
switch (direction) {
case 0: // north = -y
2015-03-20 03:13:27 +01:00
selPlots = 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-03-20 03:13:27 +01:00
selPlots = 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-03-20 03:13:27 +01:00
selPlots = 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-03-20 03:13:27 +01:00
selPlots = 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-03-20 03:13:27 +01:00
final PlotId botId = selPlots.get(0);
final PlotId topId = selPlots.get(selPlots.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-03-20 03:13:27 +01:00
final ArrayList<PlotId> plots = MainUtil.getMaxPlotSelectionIds(world, bot, top);
boolean multiMerge = false;
final HashSet<UUID> multiUUID = new HashSet<UUID>();
HashSet<PlotId> multiPlots = new HashSet<>();
2015-03-20 05:11:02 +01:00
final UUID u1 = plot.owner;
2014-11-05 04:42:08 +01:00
for (final PlotId myid : plots) {
final Plot myplot = PS.get().getPlots(world).get(myid);
2015-03-20 14:53:24 +01:00
if (myplot == null || myplot.owner == null) {
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-03-20 14:53:24 +01:00
UUID u2 = myplot.owner;
2015-03-20 03:13:27 +01:00
if (u2.equals(u1)) {
continue;
}
PlotPlayer p2 = UUIDHandler.getPlayer(u2);
if (p2 == null) {
MainUtil.sendMessage(plr, C.NO_PERM_MERGE.s().replaceAll("%plot%", myid.toString()));
return false;
}
multiMerge = true;
multiPlots.add(myid);
2015-03-20 14:14:53 +01:00
multiUUID.add(u2);
2015-03-20 03:13:27 +01:00
}
if (multiMerge) {
2015-07-13 19:42:02 +02:00
if (!Permissions.hasPermission(plr, Permissions.MERGE_OTHER)) {
2015-07-13 19:44:33 +02:00
MainUtil.sendMessage(plr, C.NO_PERMISSION, Permissions.MERGE_OTHER.s);
2015-07-13 19:42:02 +02:00
return false;
}
2015-03-20 03:13:27 +01:00
for (final UUID uuid : multiUUID) {
2015-03-20 14:09:51 +01:00
PlotPlayer accepter = UUIDHandler.getPlayer(uuid);
2015-03-20 14:43:21 +01:00
CmdConfirm.addPending(accepter, C.MERGE_REQUEST_CONFIRM.s().replaceAll("%s", plr.getName()), new Runnable() {
2015-03-20 03:13:27 +01:00
@Override
public void run() {
PlotPlayer accepter = UUIDHandler.getPlayer(uuid);
multiUUID.remove(uuid);
if (multiUUID.size() == 0) {
PlotPlayer pp = UUIDHandler.getPlayer(u1);
if (pp == null) {
2015-03-20 14:43:21 +01:00
sendMessage(accepter, C.MERGE_NOT_VALID);
2015-03-20 03:13:27 +01:00
return;
}
final PlotWorld plotWorld = PS.get().getPlotWorld(world);
2015-06-05 14:39:48 +02:00
if ((EconHandler.manager != null) && plotWorld.USE_ECONOMY) {
2015-03-20 03:13:27 +01:00
double cost = plotWorld.MERGE_PRICE;
cost = plots.size() * cost;
if (cost > 0d) {
2015-06-05 14:39:48 +02:00
if (EconHandler.manager.getMoney(plr) < cost) {
2015-03-20 03:13:27 +01:00
sendMessage(plr, C.CANNOT_AFFORD_MERGE, cost + "");
return;
}
2015-06-05 14:39:48 +02:00
EconHandler.manager.withdrawMoney(plr, cost);
2015-03-20 03:13:27 +01:00
sendMessage(plr, C.REMOVED_BALANCE, cost + "");
}
}
final boolean result = EventUtil.manager.callMerge(world, plot, plots);
if (!result) {
MainUtil.sendMessage(plr, "&cMerge has been cancelled");
return;
}
MainUtil.sendMessage(plr, C.SUCCESS_MERGE);
MainUtil.mergePlots(world, plots, true);
2015-03-20 05:11:02 +01:00
MainUtil.setSign(UUIDHandler.getName(plot.owner), plot);
2015-03-20 03:13:27 +01:00
}
MainUtil.sendMessage(accepter, C.MERGE_ACCEPTED);
}
});
}
2015-03-20 14:43:21 +01:00
MainUtil.sendMessage(plr, C.MERGE_REQUESTED);
2015-03-20 03:13:27 +01:00
return true;
2014-11-05 04:42:08 +01:00
}
final PlotWorld plotWorld = PS.get().getPlotWorld(world);
2015-06-05 14:39:48 +02:00
if ((EconHandler.manager != 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-06-05 14:39:48 +02:00
if (EconHandler.manager.getMoney(plr) < cost) {
2014-11-05 04:42:08 +01:00
sendMessage(plr, C.CANNOT_AFFORD_MERGE, cost + "");
return false;
}
2015-06-05 14:39:48 +02:00
EconHandler.manager.withdrawMoney(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-03-20 03:13:27 +01:00
MainUtil.sendMessage(plr, C.SUCCESS_MERGE);
2015-02-20 12:23:48 +01:00
MainUtil.mergePlots(world, plots, true);
2015-03-20 05:11:02 +01:00
MainUtil.setSign(UUIDHandler.getName(plot.owner), plot);
2014-11-05 04:42:08 +01:00
return true;
}
}