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;
|
|
|
|
|
2015-02-19 09:51:10 +01:00
|
|
|
import com.intellectualcrafters.plot.PlotSquared;
|
2015-01-10 11:20:20 +01:00
|
|
|
import com.intellectualcrafters.plot.config.C;
|
|
|
|
import com.intellectualcrafters.plot.database.DBFunc;
|
|
|
|
import com.intellectualcrafters.plot.flag.Flag;
|
|
|
|
import com.intellectualcrafters.plot.flag.FlagManager;
|
2015-02-21 13:01:15 +01:00
|
|
|
import com.intellectualcrafters.plot.object.Location;
|
2015-01-10 11:20:20 +01:00
|
|
|
import com.intellectualcrafters.plot.object.Plot;
|
2015-03-20 03:13:27 +01:00
|
|
|
import com.intellectualcrafters.plot.object.PlotHandler;
|
2015-01-10 11:20:20 +01:00
|
|
|
import com.intellectualcrafters.plot.object.PlotId;
|
2015-02-21 12:38:44 +01:00
|
|
|
import com.intellectualcrafters.plot.object.PlotPlayer;
|
2015-01-10 11:20:20 +01:00
|
|
|
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-02-20 07:28:21 +01:00
|
|
|
import com.intellectualcrafters.plot.util.bukkit.UUIDHandler;
|
2015-01-10 11:20:20 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Citymonstret
|
|
|
|
*/
|
|
|
|
public class Buy extends SubCommand {
|
|
|
|
public Buy() {
|
|
|
|
super(Command.BUY, "Buy the plot you are standing on", "b", CommandCategory.CLAIMING, true);
|
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
|
2015-01-10 11:20:20 +01:00
|
|
|
@Override
|
2015-02-21 08:30:55 +01:00
|
|
|
public boolean execute(final PlotPlayer plr, final String... args) {
|
2015-02-21 13:01:15 +01:00
|
|
|
if (PlotSquared.economy == 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();
|
2015-02-19 09:51:10 +01:00
|
|
|
if (!PlotSquared.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);
|
|
|
|
}
|
2015-02-23 02:32:27 +01:00
|
|
|
final int currentPlots = 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);
|
|
|
|
}
|
2015-01-25 05:40:50 +01:00
|
|
|
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();
|
2015-02-20 09:55:04 +01:00
|
|
|
final PlotWorld plotworld = PlotSquared.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-02-23 02:32:27 +01:00
|
|
|
if ((PlotSquared.economy != null) && (price > 0d)) {
|
2015-02-21 13:09:20 +01:00
|
|
|
if (EconHandler.getBalance(plr) < price) {
|
2015-01-10 11:20:20 +01:00
|
|
|
return sendMessage(plr, C.CANNOT_AFFORD_PLOT, "" + price);
|
|
|
|
}
|
2015-02-21 13:09:20 +01:00
|
|
|
EconHandler.withdrawPlayer(plr, price);
|
2015-01-10 11:20:20 +01:00
|
|
|
sendMessage(plr, C.REMOVED_BALANCE, price + "");
|
2015-03-20 03:13:27 +01:00
|
|
|
EconHandler.depositPlayer(UUIDHandler.uuidWrapper.getOfflinePlayer(plot.owner_), initPrice);
|
|
|
|
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);
|
|
|
|
myPlot.owner_ = plr.getUUID();
|
|
|
|
DBFunc.setOwner(plot, myPlot.owner_);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|