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

143 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;
2014-12-16 06:03:20 +01:00
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
2014-11-16 10:48:18 +01:00
import com.intellectualcrafters.plot.PlotMain;
import com.intellectualcrafters.plot.config.C;
2014-11-10 10:13:07 +01:00
import com.intellectualcrafters.plot.database.DBFunc;
2014-11-08 20:27:09 +01:00
import com.intellectualcrafters.plot.events.PlayerClaimPlotEvent;
2014-11-16 10:48:18 +01:00
import com.intellectualcrafters.plot.flag.Flag;
import com.intellectualcrafters.plot.flag.FlagManager;
2014-11-08 20:27:09 +01:00
import com.intellectualcrafters.plot.generator.DefaultPlotWorld;
2014-11-16 10:48:18 +01:00
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotWorld;
import com.intellectualcrafters.plot.util.PlayerFunctions;
import com.intellectualcrafters.plot.util.PlotHelper;
import com.intellectualcrafters.plot.util.SchematicHandler;
import com.intellectualcrafters.plot.util.SetBlockFast;
2014-09-22 13:02:14 +02:00
/**
* @author Citymonstret
*/
public class Claim extends SubCommand {
2014-09-22 13:02:14 +02:00
2014-11-05 04:42:08 +01:00
public Claim() {
super(Command.CLAIM, "Claim the current plot you're standing on.", "claim", CommandCategory.CLAIMING, true);
}
2014-12-16 06:03:20 +01:00
public static boolean claimPlot(final Player player, final Plot plot, final boolean teleport, final boolean auto) {
return claimPlot(player, plot, teleport, "", auto);
2014-11-05 04:42:08 +01:00
}
2014-12-16 06:03:20 +01:00
public static boolean claimPlot(final Player player, final Plot plot, final boolean teleport, final String schematic, final boolean auto) {
final PlayerClaimPlotEvent event = new PlayerClaimPlotEvent(player, plot, auto);
2014-11-05 04:42:08 +01:00
Bukkit.getPluginManager().callEvent(event);
if (!event.isCancelled()) {
PlotHelper.createPlot(player, plot);
PlotHelper.setSign(player, plot);
PlayerFunctions.sendMessage(player, C.CLAIMED);
if (teleport) {
PlotMain.teleportPlayer(player, player.getLocation(), plot);
}
final PlotWorld world = PlotMain.getWorldSettings(plot.getWorld());
2014-09-22 13:02:14 +02:00
2014-11-05 04:42:08 +01:00
final Plot plot2 = PlotMain.getPlots(player.getWorld()).get(plot.id);
2014-11-05 04:42:08 +01:00
if (world.SCHEMATIC_ON_CLAIM) {
SchematicHandler.Schematic sch;
if (schematic.equals("")) {
sch = SchematicHandler.getSchematic(world.SCHEMATIC_FILE);
2014-12-16 06:03:20 +01:00
}
else {
2014-11-05 04:42:08 +01:00
sch = SchematicHandler.getSchematic(schematic);
if (sch == null) {
sch = SchematicHandler.getSchematic(world.SCHEMATIC_FILE);
}
}
SchematicHandler.paste(player.getLocation(), sch, plot2, 0, 0);
}
if ((world.DEFAULT_FLAGS != null) && (world.DEFAULT_FLAGS.size() > 0)) {
2014-12-16 06:03:20 +01:00
final Flag[] flags = FlagManager.parseFlags(world.DEFAULT_FLAGS);
2014-11-10 10:13:07 +01:00
plot2.settings.setFlags(flags);
DBFunc.setFlags(plot.world, plot2, flags);
2014-11-05 04:42:08 +01:00
}
2014-11-02 20:01:04 +01:00
if (world instanceof DefaultPlotWorld) {
2014-11-05 04:42:08 +01:00
final DefaultPlotWorld pW = (DefaultPlotWorld) world;
if (pW.CLAIMED_WALL_BLOCK != pW.WALL_BLOCK) {
2014-11-02 20:01:04 +01:00
PlotMain.getPlotManager(plot.getWorld()).setWall(plot.getWorld(), world, plot.getId(), pW.CLAIMED_WALL_BLOCK);
2014-11-09 02:45:45 +01:00
if (PlotHelper.canSetFast) {
SetBlockFast.update(player);
}
2014-11-02 20:01:04 +01:00
}
}
2014-11-05 04:42:08 +01:00
}
return event.isCancelled();
}
2014-11-16 10:48:18 +01:00
@Override
public boolean execute(final Player plr, final String... args) {
String schematic = "";
if (args.length >= 1) {
schematic = args[0];
}
if (!PlayerFunctions.isInPlot(plr)) {
return sendMessage(plr, C.NOT_IN_PLOT);
}
if (PlayerFunctions.getPlayerPlotCount(plr.getWorld(), plr) >= PlayerFunctions.getAllowedPlots(plr)) {
return sendMessage(plr, C.CANT_CLAIM_MORE_PLOTS);
}
final Plot plot = PlayerFunctions.getCurrentPlot(plr);
if (plot.hasOwner()) {
return sendMessage(plr, C.PLOT_IS_CLAIMED);
}
final PlotWorld world = PlotMain.getWorldSettings(plot.getWorld());
if (PlotMain.useEconomy && world.USE_ECONOMY) {
final double cost = world.PLOT_PRICE;
if (cost > 0d) {
final Economy economy = PlotMain.economy;
if (economy.getBalance(plr) < cost) {
return sendMessage(plr, C.CANNOT_AFFORD_PLOT, "" + cost);
}
economy.withdrawPlayer(plr, cost);
sendMessage(plr, C.REMOVED_BALANCE, cost + "");
}
}
if (!schematic.equals("")) {
if (world.SCHEMATIC_CLAIM_SPECIFY) {
if (!world.SCHEMATICS.contains(schematic.toLowerCase())) {
return sendMessage(plr, C.SCHEMATIC_INVALID, "non-existent: " + schematic);
}
if (!PlotMain.hasPermission(plr, "plots.claim." + schematic) && !plr.hasPermission("plots.admin")) {
return sendMessage(plr, C.NO_SCHEMATIC_PERMISSION, schematic);
}
}
}
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
}