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

126 lines
6.3 KiB
Java
Raw Normal View History

2015-01-10 11:20:20 +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;
import com.intellectualcrafters.plot.PS;
2015-01-10 11:20:20 +01:00
import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.config.Settings;
2015-01-10 11:20:20 +01:00
import com.intellectualcrafters.plot.database.DBFunc;
import com.intellectualcrafters.plot.flag.Flag;
import com.intellectualcrafters.plot.flag.FlagManager;
2015-07-05 17:44:10 +02:00
import com.intellectualcrafters.plot.object.Location;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotHandler;
import com.intellectualcrafters.plot.object.PlotId;
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.object.PlotWorld;
2015-02-21 13:09:20 +01:00
import com.intellectualcrafters.plot.util.EconHandler;
2015-02-20 12:23:48 +01:00
import com.intellectualcrafters.plot.util.MainUtil;
2015-07-27 09:26:50 +02:00
import com.intellectualcrafters.plot.util.UUIDHandler;
2015-07-26 21:33:54 +02:00
import com.intellectualsites.commands.CommandDeclaration;
2015-07-27 15:10:14 +02:00
import com.intellectualcrafters.plot.object.PlotPlayer;
2015-07-26 21:33:54 +02:00
@CommandDeclaration(
command = "buy",
aliases = {"b"},
description = "Buy the plot you are standing on",
usage = "/plot buy",
permission = "plots.buy",
category = CommandCategory.CLAIMING,
2015-07-27 15:10:14 +02:00
requiredType = RequiredType.PLAYER
2015-07-26 21:33:54 +02:00
)
2015-01-10 11:20:20 +01:00
public class Buy extends SubCommand {
2015-02-23 02:32:27 +01:00
2015-01-10 11:20:20 +01:00
@Override
2015-07-27 15:10:14 +02:00
public boolean onCommand(final PlotPlayer plr, final String ... args) {
2015-06-05 14:39:48 +02:00
if (EconHandler.manager == null) {
2015-01-10 11:20:20 +01:00
return sendMessage(plr, C.ECON_DISABLED);
}
2015-02-23 02:32:27 +01:00
final Location loc = plr.getLocation();
2015-02-21 13:01:15 +01:00
final String world = loc.getWorld();
if (!PS.get().isPlotWorld(world)) {
2015-01-10 11:20:20 +01:00
return sendMessage(plr, C.NOT_IN_PLOT_WORLD);
}
Plot plot;
if (args.length > 0) {
try {
2015-02-20 07:34:19 +01:00
final String[] split = args[0].split(";");
final PlotId id = new PlotId(Integer.parseInt(split[0]), Integer.parseInt(split[1]));
2015-02-20 12:23:48 +01:00
plot = MainUtil.getPlot(world, id);
2015-02-20 07:34:19 +01:00
} catch (final Exception e) {
2015-01-10 11:20:20 +01:00
return sendMessage(plr, C.NOT_VALID_PLOT_ID);
}
2015-02-20 07:34:19 +01:00
} else {
2015-02-21 13:01:15 +01:00
plot = MainUtil.getPlot(loc);
2015-01-10 11:20:20 +01:00
}
if (plot == null) {
return sendMessage(plr, C.NOT_IN_PLOT);
}
final int currentPlots = Settings.GLOBAL_LIMIT ? MainUtil.getPlayerPlotCount(plr) : MainUtil.getPlayerPlotCount(world, plr);
2015-03-11 16:08:51 +01:00
if (currentPlots >= MainUtil.getAllowedPlots(plr)) {
2015-01-10 11:20:20 +01:00
return sendMessage(plr, C.CANT_CLAIM_MORE_PLOTS);
}
if (!plot.hasOwner()) {
return sendMessage(plr, C.PLOT_UNOWNED);
}
2015-03-20 03:13:27 +01:00
if (PlotHandler.isOwner(plot, plr.getUUID())) {
2015-01-11 16:31:09 +01:00
return sendMessage(plr, C.CANNOT_BUY_OWN);
}
2015-02-20 07:34:19 +01:00
final Flag flag = FlagManager.getPlotFlag(plot, "price");
2015-01-10 11:20:20 +01:00
if (flag == null) {
return sendMessage(plr, C.NOT_FOR_SALE);
}
double initPrice = (double) flag.getValue();
2015-01-10 11:37:46 +01:00
double price = initPrice;
2015-02-20 07:34:19 +01:00
final PlotId id = plot.id;
2015-02-22 07:37:44 +01:00
final PlotId id2 = MainUtil.getTopPlot(plot).id;
2015-02-21 13:52:50 +01:00
final int size = MainUtil.getPlotSelectionIds(id, id2).size();
final PlotWorld plotworld = PS.get().getPlotWorld(world);
2015-01-10 11:20:20 +01:00
if (plotworld.USE_ECONOMY) {
price += plotworld.PLOT_PRICE * size;
2015-01-10 11:37:46 +01:00
initPrice += plotworld.SELL_PRICE * size;
2015-01-10 11:20:20 +01:00
}
2015-06-05 14:39:48 +02:00
if ((EconHandler.manager != null) && (price > 0d)) {
if (EconHandler.manager.getMoney(plr) < price) {
2015-01-10 11:20:20 +01:00
return sendMessage(plr, C.CANNOT_AFFORD_PLOT, "" + price);
}
2015-06-05 14:39:48 +02:00
EconHandler.manager.withdrawMoney(plr, price);
2015-01-10 11:20:20 +01:00
sendMessage(plr, C.REMOVED_BALANCE, price + "");
2015-07-24 16:06:58 +02:00
EconHandler.manager.depositMoney(UUIDHandler.getUUIDWrapper().getOfflinePlayer(plot.owner), initPrice);
2015-03-20 05:11:02 +01:00
final PlotPlayer owner = UUIDHandler.getPlayer(plot.owner);
2015-01-10 11:37:46 +01:00
if (owner != null) {
sendMessage(plr, C.PLOT_SOLD, plot.id + "", plr.getName(), initPrice + "");
}
2015-01-11 16:31:09 +01:00
FlagManager.removePlotFlag(plot, "price");
2015-01-10 11:20:20 +01:00
}
2015-03-20 03:13:27 +01:00
Plot top = MainUtil.getTopPlot(plot);
for (PlotId myId : MainUtil.getPlotSelectionIds(plot.id, top.id)) {
Plot myPlot = MainUtil.getPlot(plot.world, myId);
2015-03-20 05:11:02 +01:00
myPlot.owner = plr.getUUID();
DBFunc.setOwner(plot, myPlot.owner);
2015-03-20 03:13:27 +01:00
}
2015-02-21 12:24:46 +01:00
MainUtil.sendMessage(plr, C.CLAIMED);
2015-01-10 11:37:46 +01:00
return true;
2015-01-10 11:20:20 +01:00
}
}