2020-04-16 04:52:39 +02:00
|
|
|
/*
|
2022-06-15 20:17:17 +02:00
|
|
|
* PlotSquared, a land and world management plugin for Minecraft.
|
|
|
|
* Copyright (C) IntellectualSites <https://intellectualsites.com>
|
|
|
|
* Copyright (C) IntellectualSites team and contributors
|
2020-04-16 04:52:39 +02:00
|
|
|
*
|
2022-06-15 20:17:17 +02:00
|
|
|
* 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.
|
2020-04-16 04:52:39 +02:00
|
|
|
*
|
2022-06-15 20:17:17 +02:00
|
|
|
* 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.
|
2020-04-16 04:52:39 +02:00
|
|
|
*
|
2022-06-15 20:17:17 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2020-04-16 04:52:39 +02:00
|
|
|
*/
|
2020-04-15 21:26:54 +02:00
|
|
|
package com.plotsquared.core.command;
|
2015-01-10 11:20:20 +01:00
|
|
|
|
2020-07-11 17:19:19 +02:00
|
|
|
import com.google.inject.Inject;
|
2020-04-15 21:26:54 +02:00
|
|
|
import com.plotsquared.core.PlotSquared;
|
2020-07-18 15:06:51 +02:00
|
|
|
import com.plotsquared.core.configuration.caption.TranslatableCaption;
|
2020-04-15 21:26:54 +02:00
|
|
|
import com.plotsquared.core.events.PlotFlagRemoveEvent;
|
|
|
|
import com.plotsquared.core.events.Result;
|
2020-04-30 12:33:59 +02:00
|
|
|
import com.plotsquared.core.player.PlotPlayer;
|
|
|
|
import com.plotsquared.core.plot.Plot;
|
2020-08-23 00:13:00 +02:00
|
|
|
import com.plotsquared.core.plot.PlotArea;
|
2020-04-15 21:26:54 +02:00
|
|
|
import com.plotsquared.core.plot.flag.PlotFlag;
|
|
|
|
import com.plotsquared.core.plot.flag.implementations.PriceFlag;
|
|
|
|
import com.plotsquared.core.util.EconHandler;
|
2020-07-10 18:17:44 +02:00
|
|
|
import com.plotsquared.core.util.EventDispatcher;
|
2020-04-30 12:33:59 +02:00
|
|
|
import com.plotsquared.core.util.task.RunnableVal2;
|
|
|
|
import com.plotsquared.core.util.task.RunnableVal3;
|
2020-07-18 15:06:51 +02:00
|
|
|
import net.kyori.adventure.text.minimessage.Template;
|
2021-01-09 22:28:41 +01:00
|
|
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
2016-06-24 02:12:17 +02:00
|
|
|
|
2016-02-27 11:07:42 +01:00
|
|
|
import java.util.Set;
|
2019-01-21 09:20:33 +01:00
|
|
|
import java.util.concurrent.CompletableFuture;
|
2016-02-27 11:07:42 +01:00
|
|
|
|
2020-02-24 18:42:02 +01:00
|
|
|
@CommandDeclaration(command = "buy",
|
2021-01-09 22:28:41 +01:00
|
|
|
usage = "/plot buy",
|
|
|
|
permission = "plots.buy",
|
|
|
|
category = CommandCategory.CLAIMING,
|
|
|
|
requiredType = RequiredType.NONE)
|
2020-02-24 18:42:02 +01:00
|
|
|
public class Buy extends Command {
|
2016-05-21 21:29:19 +02:00
|
|
|
|
2020-07-10 18:17:44 +02:00
|
|
|
private final EventDispatcher eventDispatcher;
|
2020-07-11 17:19:19 +02:00
|
|
|
private final EconHandler econHandler;
|
|
|
|
|
2021-01-09 22:28:41 +01:00
|
|
|
@Inject
|
|
|
|
public Buy(
|
|
|
|
final @NonNull EventDispatcher eventDispatcher,
|
|
|
|
final @NonNull EconHandler econHandler
|
|
|
|
) {
|
2016-05-21 21:29:19 +02:00
|
|
|
super(MainCommand.getInstance(), true);
|
2020-07-10 18:17:44 +02:00
|
|
|
this.eventDispatcher = eventDispatcher;
|
2020-07-11 17:19:19 +02:00
|
|
|
this.econHandler = econHandler;
|
2016-05-21 21:29:19 +02:00
|
|
|
}
|
2016-03-23 02:41:37 +01:00
|
|
|
|
2020-04-30 12:33:59 +02:00
|
|
|
@Override
|
2021-01-09 22:28:41 +01:00
|
|
|
public CompletableFuture<Boolean> execute(
|
|
|
|
final PlotPlayer<?> player, String[] args,
|
|
|
|
RunnableVal3<Command, Runnable, Runnable> confirm,
|
|
|
|
final RunnableVal2<Command, CommandResult> whenDone
|
|
|
|
) {
|
2019-03-06 18:47:38 +01:00
|
|
|
|
2020-08-23 00:13:00 +02:00
|
|
|
PlotArea area = player.getPlotAreaAbs();
|
|
|
|
check(area, TranslatableCaption.of("errors.not_in_plot_world"));
|
|
|
|
check(this.econHandler.isEnabled(area), TranslatableCaption.of("economy.econ_disabled"));
|
2016-05-21 21:29:19 +02:00
|
|
|
final Plot plot;
|
|
|
|
if (args.length != 0) {
|
2020-07-18 15:06:51 +02:00
|
|
|
if (args.length != 1) {
|
|
|
|
sendUsage(player);
|
|
|
|
return CompletableFuture.completedFuture(false);
|
|
|
|
}
|
2020-07-19 14:23:55 +02:00
|
|
|
plot = check(Plot.getPlotFromString(player, args[0], true), null);
|
2015-09-13 06:04:31 +02:00
|
|
|
} else {
|
2020-08-04 19:01:25 +02:00
|
|
|
plot = check(player.getCurrentPlot(), TranslatableCaption.of("errors.not_in_plot"));
|
2015-09-13 06:04:31 +02:00
|
|
|
}
|
2020-08-04 19:01:25 +02:00
|
|
|
checkTrue(plot.hasOwner(), TranslatableCaption.of("info.plot_unowned"));
|
|
|
|
checkTrue(!plot.isOwner(player.getUUID()), TranslatableCaption.of("economy.cannot_buy_own"));
|
2016-05-21 21:29:19 +02:00
|
|
|
Set<Plot> plots = plot.getConnectedPlots();
|
2021-01-09 22:28:41 +01:00
|
|
|
checkTrue(
|
|
|
|
player.getPlotCount() + plots.size() <= player.getAllowedPlots(),
|
|
|
|
TranslatableCaption.of("permission.cant_claim_more_plots"),
|
2021-12-14 23:04:36 +01:00
|
|
|
Template.of("amount", String.valueOf(player.getAllowedPlots()))
|
2021-01-09 22:28:41 +01:00
|
|
|
);
|
2020-02-24 18:42:02 +01:00
|
|
|
double price = plot.getFlag(PriceFlag.class);
|
|
|
|
if (price <= 0) {
|
2020-08-04 19:01:25 +02:00
|
|
|
throw new CommandException(TranslatableCaption.of("economy.not_for_sale"));
|
2016-06-24 02:12:17 +02:00
|
|
|
}
|
2021-08-18 11:58:18 +02:00
|
|
|
checkTrue(
|
|
|
|
this.econHandler.isSupported(),
|
2021-07-31 10:43:11 +02:00
|
|
|
TranslatableCaption.of("economy.vault_or_consumer_null")
|
2021-05-07 09:59:17 +02:00
|
|
|
);
|
2021-01-09 22:28:41 +01:00
|
|
|
checkTrue(
|
2021-05-07 09:59:17 +02:00
|
|
|
this.econHandler.getMoney(player) >= price,
|
2020-11-19 16:32:30 +01:00
|
|
|
TranslatableCaption.of("economy.cannot_afford_plot"),
|
2021-12-14 23:04:36 +01:00
|
|
|
Template.of("money", this.econHandler.format(price)),
|
|
|
|
Template.of("balance", this.econHandler.format(this.econHandler.getMoney(player)))
|
2021-01-09 22:28:41 +01:00
|
|
|
);
|
2020-08-23 00:13:00 +02:00
|
|
|
this.econHandler.withdrawMoney(player, price);
|
2019-02-20 00:39:43 +01:00
|
|
|
// Failure
|
|
|
|
// Success
|
|
|
|
confirm.run(this, () -> {
|
2020-07-18 15:06:51 +02:00
|
|
|
player.sendMessage(
|
|
|
|
TranslatableCaption.of("economy.removed_balance"),
|
2021-12-14 23:04:36 +01:00
|
|
|
Template.of("money", this.econHandler.format(price))
|
2020-07-22 19:42:22 +02:00
|
|
|
);
|
2020-05-19 23:05:36 +02:00
|
|
|
|
2020-12-05 18:41:25 +01:00
|
|
|
this.econHandler.depositMoney(PlotSquared.platform().playerManager().getOfflinePlayer(plot.getOwnerAbs()), price);
|
2020-05-19 23:05:36 +02:00
|
|
|
|
2020-12-05 18:41:25 +01:00
|
|
|
PlotPlayer<?> owner = PlotSquared.platform().playerManager().getPlayerIfExists(plot.getOwnerAbs());
|
2019-02-20 00:39:43 +01:00
|
|
|
if (owner != null) {
|
2020-07-18 15:06:51 +02:00
|
|
|
owner.sendMessage(
|
|
|
|
TranslatableCaption.of("economy.plot_sold"),
|
2021-12-14 23:04:36 +01:00
|
|
|
Template.of("plot", plot.getId().toString()),
|
|
|
|
Template.of("player", player.getName()),
|
|
|
|
Template.of("price", this.econHandler.format(price))
|
2020-07-18 15:06:51 +02:00
|
|
|
);
|
2015-01-10 11:37:46 +01:00
|
|
|
}
|
2020-03-15 12:22:49 +01:00
|
|
|
PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(PriceFlag.class);
|
2020-07-10 18:17:44 +02:00
|
|
|
PlotFlagRemoveEvent event = this.eventDispatcher.callFlagRemove(plotFlag, plot);
|
2020-04-30 12:33:59 +02:00
|
|
|
if (event.getEventResult() != Result.DENY) {
|
2020-03-15 12:22:49 +01:00
|
|
|
plot.removeFlag(event.getFlag());
|
|
|
|
}
|
2019-02-20 00:39:43 +01:00
|
|
|
plot.setOwner(player.getUUID());
|
2021-05-11 18:50:37 +02:00
|
|
|
player.sendMessage(
|
|
|
|
TranslatableCaption.of("working.claimed"),
|
2021-12-14 23:04:36 +01:00
|
|
|
Template.of("plot", plot.getId().toString())
|
2021-05-11 18:50:37 +02:00
|
|
|
);
|
2019-02-20 00:39:43 +01:00
|
|
|
whenDone.run(Buy.this, CommandResult.SUCCESS);
|
|
|
|
}, () -> {
|
2020-08-23 00:13:00 +02:00
|
|
|
this.econHandler.depositMoney(player, price);
|
2019-02-20 00:39:43 +01:00
|
|
|
whenDone.run(Buy.this, CommandResult.FAILURE);
|
2016-05-21 21:29:19 +02:00
|
|
|
});
|
2019-01-21 09:20:33 +01:00
|
|
|
return CompletableFuture.completedFuture(true);
|
2015-01-10 11:20:20 +01:00
|
|
|
}
|
2021-01-09 22:28:41 +01:00
|
|
|
|
2015-01-10 11:20:20 +01:00
|
|
|
}
|