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-01-13 17:38:15 +01:00
|
|
|
import net.milkbowl.vault.economy.Economy;
|
|
|
|
|
|
|
|
import org.bukkit.World;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
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;
|
|
|
|
import com.intellectualcrafters.plot.object.Plot;
|
|
|
|
import com.intellectualcrafters.plot.object.PlotId;
|
|
|
|
import com.intellectualcrafters.plot.object.PlotWorld;
|
2015-02-20 12:23:48 +01:00
|
|
|
import com.intellectualcrafters.plot.util.MainUtil;
|
2015-02-20 12:24:58 +01:00
|
|
|
import com.intellectualcrafters.plot.util.bukkit.BukkitPlayerFunctions;
|
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-20 07:34:19 +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-19 09:51:10 +01:00
|
|
|
if (!PlotSquared.useEconomy) {
|
2015-01-10 11:20:20 +01:00
|
|
|
return sendMessage(plr, C.ECON_DISABLED);
|
|
|
|
}
|
2015-02-20 07:34:19 +01:00
|
|
|
final World world = plr.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-20 12:24:58 +01:00
|
|
|
plot = BukkitPlayerFunctions.getCurrentPlot(plr);
|
2015-01-10 11:20:20 +01:00
|
|
|
}
|
|
|
|
if (plot == null) {
|
|
|
|
return sendMessage(plr, C.NOT_IN_PLOT);
|
|
|
|
}
|
2015-02-20 12:24:58 +01:00
|
|
|
if (BukkitPlayerFunctions.getPlayerPlotCount(world, plr) >= BukkitPlayerFunctions.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-01-11 16:31:09 +01:00
|
|
|
if (plot.owner.equals(UUIDHandler.getUUID(plr))) {
|
|
|
|
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-20 12:24:58 +01:00
|
|
|
final PlotId id2 = BukkitPlayerFunctions.getTopPlot(world, plot).id;
|
|
|
|
final int size = BukkitPlayerFunctions.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
|
|
|
}
|
|
|
|
if (price > 0d) {
|
2015-02-19 09:51:10 +01:00
|
|
|
final Economy economy = PlotSquared.economy;
|
2015-01-10 11:20:20 +01:00
|
|
|
if (economy.getBalance(plr) < price) {
|
|
|
|
return sendMessage(plr, C.CANNOT_AFFORD_PLOT, "" + price);
|
|
|
|
}
|
|
|
|
economy.withdrawPlayer(plr, price);
|
|
|
|
sendMessage(plr, C.REMOVED_BALANCE, price + "");
|
2015-01-10 11:37:46 +01:00
|
|
|
economy.depositPlayer(UUIDHandler.uuidWrapper.getOfflinePlayer(plot.owner), initPrice);
|
2015-02-20 07:34:19 +01:00
|
|
|
final Player owner = UUIDHandler.uuidWrapper.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
|
|
|
}
|
|
|
|
plot.owner = UUIDHandler.getUUID(plr);
|
|
|
|
DBFunc.setOwner(plot, plot.owner);
|
2015-02-20 12:24:58 +01:00
|
|
|
BukkitPlayerFunctions.sendMessage(plr, C.CLAIMED);
|
2015-01-10 11:37:46 +01:00
|
|
|
return true;
|
2015-01-10 11:20:20 +01:00
|
|
|
}
|
|
|
|
}
|