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

203 lines
9.8 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-31 00:25:16 +10:00
import java.util.ArrayList;
import java.util.HashSet;
import java.util.UUID;
2015-07-27 19:50:04 +02:00
import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.config.C;
2015-08-25 00:00:29 +10:00
import com.intellectualcrafters.plot.config.Settings;
2015-07-31 00:25:16 +10:00
import com.intellectualcrafters.plot.object.Location;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotId;
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.object.PlotWorld;
import com.intellectualcrafters.plot.util.CmdConfirm;
import com.intellectualcrafters.plot.util.EconHandler;
import com.intellectualcrafters.plot.util.EventUtil;
import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.Permissions;
2015-07-31 03:24:01 +10:00
import com.intellectualcrafters.plot.util.StringMan;
2015-07-31 00:25:16 +10:00
import com.intellectualcrafters.plot.util.UUIDHandler;
2015-07-27 19:50:04 +02:00
import com.plotsquared.general.commands.CommandDeclaration;
2014-12-31 00:09:11 +11:00
@CommandDeclaration(
2015-09-11 20:09:22 +10:00
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,
requiredType = RequiredType.NONE)
2015-09-13 14:04:31 +10:00
public class Merge extends SubCommand {
2015-09-22 23:23:28 +10:00
public final static String[] values = new String[] { "north", "east", "south", "west", "auto" };
public final static String[] aliases = new String[] { "n", "e", "s", "w", "all" };
2015-09-13 14:04:31 +10:00
public static String direction(float yaw) {
2014-11-05 14:42:08 +11:00
yaw = yaw / 90;
final int i = Math.round(yaw);
2015-09-13 14:04:31 +10:00
switch (i) {
2014-11-05 14:42:08 +11:00
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-09-13 14:04:31 +10:00
2014-11-05 14:42:08 +11:00
@Override
2015-09-13 14:04:31 +10:00
public boolean onCommand(final PlotPlayer plr, final String[] args) {
2015-02-23 22:37:36 +11:00
final Location loc = plr.getLocationFull();
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 == null) || !plot.hasOwner()) {
2015-02-21 22:24:46 +11:00
MainUtil.sendMessage(plr, C.PLOT_UNOWNED);
2014-11-05 14:42:08 +11:00
return false;
}
final boolean admin = Permissions.hasPermission(plr, "plots.admin.command.merge");
2015-09-13 14:04:31 +10:00
if (!plot.isOwner(plr.getUUID()) && !admin) {
2015-02-21 22:24:46 +11:00
MainUtil.sendMessage(plr, C.NO_PLOT_PERMS);
2014-11-05 14:42:08 +11:00
return false;
}
2015-09-22 23:23:28 +10:00
final PlotWorld plotworld = PS.get().getPlotWorld(plot.world);
if ((EconHandler.manager != null) && plotworld.USE_ECONOMY && plotworld.MERGE_PRICE > 0d && EconHandler.manager.getMoney(plr) < plotworld.MERGE_PRICE) {
sendMessage(plr, C.CANNOT_AFFORD_MERGE, plotworld.MERGE_PRICE + "");
return false;
}
2014-11-05 14:42:08 +11:00
int direction = -1;
2015-09-22 23:23:28 +10:00
final int size = plot.getConnectedPlots().size();
final int maxSize = Permissions.hasPermissionRange(plr, "plots.merge", Settings.MAX_PLOTS);
if (size >= maxSize) {
MainUtil.sendMessage(plr, C.NO_PERMISSION, "plots.merge." + (size + 1));
2015-09-22 23:23:28 +10:00
return false;
}
2015-09-13 14:04:31 +10:00
if (args.length == 0) {
2015-09-22 23:23:28 +10:00
// switch (direction(plr.getLocationFull().getYaw())) {
// case "NORTH":
// direction = 0;
// break;
// case "EAST":
// direction = 1;
// break;
// case "SOUTH":
// direction = 2;
// break;
// case "WEST":
// direction = 3;
// break;
// }
2015-09-13 14:04:31 +10:00
} else {
2015-09-22 23:23:28 +10:00
if (args[0].equalsIgnoreCase("all") || args[0].equalsIgnoreCase("auto")) {
if (MainUtil.autoMerge(plot, -1, maxSize - size, plr.getUUID(), (args.length != 2) || !args[1].equalsIgnoreCase("false"))) {
if ((EconHandler.manager != null) && plotworld.USE_ECONOMY && plotworld.MERGE_PRICE > 0d) {
EconHandler.manager.withdrawMoney(plr, plotworld.MERGE_PRICE);
sendMessage(plr, C.REMOVED_BALANCE, plotworld.MERGE_PRICE + "");
}
MainUtil.sendMessage(plr, C.SUCCESS_MERGE);
return true;
}
MainUtil.sendMessage(plr, C.NO_AVAILABLE_AUTOMERGE);
return false;
2015-09-03 22:40:36 +10:00
}
2015-09-13 14:04:31 +10:00
for (int i = 0; i < values.length; i++) {
if (args[0].equalsIgnoreCase(values[i]) || args[0].equalsIgnoreCase(aliases[i])) {
2015-07-31 04:46:06 +10:00
direction = i;
break;
}
2014-11-05 14:42:08 +11:00
}
}
2015-09-13 14:04:31 +10:00
if (direction == -1) {
2015-09-22 23:23:28 +10:00
MainUtil.sendMessage(plr, C.COMMAND_SYNTAX, "/plot merge <" + StringMan.join(values, "|") + ">");
2015-02-26 22:42:28 +11:00
MainUtil.sendMessage(plr, C.DIRECTION.s().replaceAll("%dir%", direction(loc.getYaw())));
2014-11-05 14:42:08 +11:00
return false;
}
2015-09-22 23:23:28 +10:00
if (MainUtil.autoMerge(plot, direction, maxSize - size, plot.owner, (args.length != 2) || !args[1].equalsIgnoreCase("false"))) {
if ((EconHandler.manager != null) && plotworld.USE_ECONOMY && plotworld.MERGE_PRICE > 0d) {
EconHandler.manager.withdrawMoney(plr, plotworld.MERGE_PRICE);
sendMessage(plr, C.REMOVED_BALANCE, plotworld.MERGE_PRICE + "");
}
MainUtil.sendMessage(plr, C.SUCCESS_MERGE);
return true;
2014-11-05 14:42:08 +11:00
}
2015-09-22 23:23:28 +10:00
Plot adjacent = MainUtil.getPlotAbs(plot.world, MainUtil.getPlotIdRelative(plot.id, direction));
if (adjacent == null || !adjacent.hasOwner() || adjacent.getMerged((direction + 2) % 4)) {
MainUtil.sendMessage(plr, C.NO_AVAILABLE_AUTOMERGE);
2015-08-25 00:00:29 +10:00
return false;
}
2015-09-22 23:23:28 +10:00
if (!Permissions.hasPermission(plr, C.PERMISSION_MERGE_OTHER)) {
MainUtil.sendMessage(plr, C.NO_PERMISSION, C.PERMISSION_MERGE_OTHER.s());
return false;
2015-03-20 13:13:27 +11:00
}
2015-09-22 23:23:28 +10:00
HashSet<UUID> uuids = adjacent.getOwners();
boolean isOnline = false;
for (final UUID uuid : uuids) {
final PlotPlayer accepter = UUIDHandler.getPlayer(uuid);
if (accepter == null) {
continue;
2015-07-14 03:42:02 +10:00
}
2015-09-22 23:23:28 +10:00
isOnline = true;
final int dir = direction;
CmdConfirm.addPending(accepter, C.MERGE_REQUEST_CONFIRM.s().replaceAll("%s", plr.getName()), new Runnable() {
@Override
public void run() {
MainUtil.sendMessage(accepter, C.MERGE_ACCEPTED);
MainUtil.autoMerge(plot, dir, maxSize - size, uuid, true);
final PlotPlayer pp = UUIDHandler.getPlayer(plr.getUUID());
if (pp == null) {
sendMessage(accepter, C.MERGE_NOT_VALID);
return;
}
if ((EconHandler.manager != null) && plotworld.USE_ECONOMY && plotworld.MERGE_PRICE > 0d) {
if (EconHandler.manager.getMoney(plr) < plotworld.MERGE_PRICE) {
sendMessage(plr, C.CANNOT_AFFORD_MERGE, plotworld.MERGE_PRICE + "");
return;
2015-03-20 13:13:27 +11:00
}
2015-09-22 23:23:28 +10:00
EconHandler.manager.withdrawMoney(plr, plotworld.MERGE_PRICE);
sendMessage(plr, C.REMOVED_BALANCE, plotworld.MERGE_PRICE + "");
2015-03-20 13:13:27 +11:00
}
2015-09-22 23:23:28 +10:00
MainUtil.sendMessage(plr, C.SUCCESS_MERGE);
2014-11-05 14:42:08 +11:00
}
2015-09-22 23:23:28 +10:00
});
2014-11-05 14:42:08 +11:00
}
2015-09-22 23:23:28 +10:00
if (isOnline == false) {
MainUtil.sendMessage(plr, C.NO_AVAILABLE_AUTOMERGE);
2014-11-05 14:42:08 +11:00
return false;
}
2015-09-22 23:23:28 +10:00
MainUtil.sendMessage(plr, C.MERGE_REQUESTED);
2014-11-05 14:42:08 +11:00
return true;
}
}