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

137 lines
7.1 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 /
////////////////////////////////////////////////////////////////////////////////////////////////////
2014-09-22 13:02:14 +02:00
package com.intellectualcrafters.plot.commands;
import com.intellectualcrafters.plot.PS;
2014-11-16 10:48:18 +01:00
import com.intellectualcrafters.plot.config.C;
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.PlotPlayer;
import com.intellectualcrafters.plot.object.PlotWorld;
import com.intellectualcrafters.plot.object.RunnableVal;
import com.intellectualcrafters.plot.util.EconHandler;
import com.intellectualcrafters.plot.util.EventUtil;
import com.intellectualcrafters.plot.util.MainUtil;
import com.intellectualcrafters.plot.util.Permissions;
import com.intellectualcrafters.plot.util.SchematicHandler;
2015-02-25 23:25:28 +11:00
import com.intellectualcrafters.plot.util.SchematicHandler.Schematic;
2015-07-27 19:50:04 +02:00
import com.plotsquared.general.commands.CommandDeclaration;
2014-12-28 23:02:30 +11:00
2015-07-26 21:33:54 +02:00
@CommandDeclaration(
2015-09-11 20:09:22 +10:00
command = "claim",
aliases = { "c" },
description = "Claim the current plot you're standing on",
category = CommandCategory.CLAIMING,
requiredType = RequiredType.NONE,
permission = "plots.claim",
usage = "/plot claim")
2015-09-13 14:04:31 +10:00
public class Claim extends SubCommand {
public static boolean claimPlot(final PlotPlayer player, final Plot plot, final boolean teleport, final boolean auto) {
return claimPlot(player, plot, teleport, "", auto);
2014-11-05 14:42:08 +11:00
}
2015-09-13 14:04:31 +10:00
public static boolean claimPlot(final PlotPlayer player, final Plot plot, final boolean teleport, final String schematic, final boolean auto) {
2015-09-22 23:23:28 +10:00
if (plot.hasOwner() || plot.isMerged()) {
2015-09-13 14:04:31 +10:00
return false;
}
2015-02-23 16:29:45 +11:00
final boolean result = EventUtil.manager.callClaim(player, plot, false);
2015-09-13 14:04:31 +10:00
if (result) {
2015-02-22 01:13:34 +11:00
MainUtil.createPlot(player.getUUID(), plot);
MainUtil.setSign(player.getName(), plot);
MainUtil.sendMessage(player, C.CLAIMED);
2015-02-23 12:32:27 +11:00
final Location loc = player.getLocation();
2015-09-13 14:04:31 +10:00
if (teleport) {
2015-02-22 01:17:31 +11:00
MainUtil.teleportPlayer(player, loc, plot);
2014-11-05 14:42:08 +11:00
}
2015-02-22 01:17:31 +11:00
final String world = plot.world;
final PlotWorld plotworld = PS.get().getPlotWorld(world);
final Plot plot2 = PS.get().getPlot(world, plot.id);
2015-09-13 14:04:31 +10:00
if (plotworld.SCHEMATIC_ON_CLAIM) {
2015-02-25 23:25:28 +11:00
Schematic sch;
2015-09-13 14:04:31 +10:00
if (schematic.equals("")) {
2015-02-25 23:25:28 +11:00
sch = SchematicHandler.manager.getSchematic(plotworld.SCHEMATIC_FILE);
2015-09-13 14:04:31 +10:00
} else {
2015-02-25 23:25:28 +11:00
sch = SchematicHandler.manager.getSchematic(schematic);
2015-09-13 14:04:31 +10:00
if (sch == null) {
2015-02-25 23:25:28 +11:00
sch = SchematicHandler.manager.getSchematic(plotworld.SCHEMATIC_FILE);
2014-11-05 14:42:08 +11:00
}
}
2015-09-13 14:04:31 +10:00
SchematicHandler.manager.paste(sch, plot2, 0, 0, new RunnableVal<Boolean>() {
@Override
2015-09-13 14:04:31 +10:00
public void run() {
if (value) {
MainUtil.sendMessage(player, C.SCHEMATIC_PASTE_SUCCESS);
2015-09-13 14:04:31 +10:00
} else {
MainUtil.sendMessage(player, C.SCHEMATIC_PASTE_FAILED);
}
}
});
2014-11-05 14:42:08 +11:00
}
PS.get().getPlotManager(world).claimPlot(plotworld, plot);
2014-11-05 14:42:08 +11:00
}
2015-02-23 16:29:45 +11:00
return result;
2014-11-05 14:42:08 +11:00
}
2015-09-13 14:04:31 +10:00
2014-11-16 10:48:18 +01:00
@Override
2015-09-13 14:04:31 +10:00
public boolean onCommand(final PlotPlayer plr, final String... args) {
2014-11-16 10:48:18 +01:00
String schematic = "";
2015-09-13 14:04:31 +10:00
if (args.length >= 1) {
2014-11-16 10:48:18 +01:00
schematic = args[0];
}
2015-02-23 12:32:27 +11: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);
}
final int currentPlots = Settings.GLOBAL_LIMIT ? MainUtil.getPlayerPlotCount(plr) : MainUtil.getPlayerPlotCount(loc.getWorld(), plr);
2015-09-13 14:04:31 +10:00
if (currentPlots >= MainUtil.getAllowedPlots(plr)) {
return sendMessage(plr, C.CANT_CLAIM_MORE_PLOTS);
}
if (!MainUtil.canClaim(plr, plot)) {
return sendMessage(plr, C.PLOT_IS_CLAIMED);
}
final PlotWorld world = PS.get().getPlotWorld(plot.world);
2015-09-13 14:04:31 +10:00
if ((EconHandler.manager != null) && world.USE_ECONOMY) {
2014-11-16 10:48:18 +01:00
final double cost = world.PLOT_PRICE;
2015-09-13 14:04:31 +10:00
if (cost > 0d) {
if (EconHandler.manager.getMoney(plr) < cost) {
return sendMessage(plr, C.CANNOT_AFFORD_PLOT, "" + cost);
}
2015-06-05 22:39:48 +10:00
EconHandler.manager.withdrawMoney(plr, cost);
2014-11-16 10:48:18 +01:00
sendMessage(plr, C.REMOVED_BALANCE, cost + "");
}
}
2015-09-13 14:04:31 +10:00
if (!schematic.equals("")) {
if (world.SCHEMATIC_CLAIM_SPECIFY) {
if (!world.SCHEMATICS.contains(schematic.toLowerCase())) {
return sendMessage(plr, C.SCHEMATIC_INVALID, "non-existent: " + schematic);
}
if (!Permissions.hasPermission(plr, "plots.claim." + schematic) && !Permissions.hasPermission(plr, "plots.admin.command.schematic")) {
return sendMessage(plr, C.NO_SCHEMATIC_PERMISSION, schematic);
}
2014-11-16 10:48:18 +01:00
}
}
return claimPlot(plr, plot, false, schematic, false) || sendMessage(plr, C.PLOT_NOT_CLAIMED);
2014-11-16 10:48:18 +01:00
}
2014-09-22 13:02:14 +02:00
}