001/*
002 * PlotSquared, a land and world management plugin for Minecraft.
003 * Copyright (C) IntellectualSites <https://intellectualsites.com>
004 * Copyright (C) IntellectualSites team and contributors
005 *
006 * This program is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
018 */
019package com.plotsquared.core.command;
020
021import com.google.inject.Inject;
022import com.plotsquared.core.PlotSquared;
023import com.plotsquared.core.configuration.caption.TranslatableCaption;
024import com.plotsquared.core.events.PlotFlagRemoveEvent;
025import com.plotsquared.core.events.Result;
026import com.plotsquared.core.player.PlotPlayer;
027import com.plotsquared.core.plot.Plot;
028import com.plotsquared.core.plot.PlotArea;
029import com.plotsquared.core.plot.flag.PlotFlag;
030import com.plotsquared.core.plot.flag.implementations.PriceFlag;
031import com.plotsquared.core.util.EconHandler;
032import com.plotsquared.core.util.EventDispatcher;
033import com.plotsquared.core.util.task.RunnableVal2;
034import com.plotsquared.core.util.task.RunnableVal3;
035import net.kyori.adventure.text.Component;
036import net.kyori.adventure.text.minimessage.tag.Tag;
037import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
038import org.checkerframework.checker.nullness.qual.NonNull;
039
040import java.util.Set;
041import java.util.concurrent.CompletableFuture;
042
043@CommandDeclaration(command = "buy",
044        usage = "/plot buy",
045        permission = "plots.buy",
046        category = CommandCategory.CLAIMING,
047        requiredType = RequiredType.NONE)
048public class Buy extends Command {
049
050    private final EventDispatcher eventDispatcher;
051    private final EconHandler econHandler;
052
053    @Inject
054    public Buy(
055            final @NonNull EventDispatcher eventDispatcher,
056            final @NonNull EconHandler econHandler
057    ) {
058        super(MainCommand.getInstance(), true);
059        this.eventDispatcher = eventDispatcher;
060        this.econHandler = econHandler;
061    }
062
063    @Override
064    public CompletableFuture<Boolean> execute(
065            final PlotPlayer<?> player, String[] args,
066            RunnableVal3<Command, Runnable, Runnable> confirm,
067            final RunnableVal2<Command, CommandResult> whenDone
068    ) {
069
070        PlotArea area = player.getPlotAreaAbs();
071        check(area, TranslatableCaption.of("errors.not_in_plot_world"));
072        check(this.econHandler.isEnabled(area), TranslatableCaption.of("economy.econ_disabled"));
073        final Plot plot;
074        if (args.length != 0) {
075            if (args.length != 1) {
076                sendUsage(player);
077                return CompletableFuture.completedFuture(false);
078            }
079            plot = check(Plot.getPlotFromString(player, args[0], true), null);
080        } else {
081            plot = check(player.getCurrentPlot(), TranslatableCaption.of("errors.not_in_plot"));
082        }
083        checkTrue(plot.hasOwner(), TranslatableCaption.of("info.plot_unowned"));
084        checkTrue(!plot.isOwner(player.getUUID()), TranslatableCaption.of("economy.cannot_buy_own"));
085        Set<Plot> plots = plot.getConnectedPlots();
086        checkTrue(
087                player.getPlotCount() + plots.size() <= player.getAllowedPlots(),
088                TranslatableCaption.of("permission.cant_claim_more_plots"),
089                TagResolver.resolver("amount", Tag.inserting(Component.text(player.getAllowedPlots())))
090        );
091        double price = plot.getFlag(PriceFlag.class);
092        if (price <= 0) {
093            throw new CommandException(TranslatableCaption.of("economy.not_for_sale"));
094        }
095        checkTrue(
096                this.econHandler.isSupported(),
097                TranslatableCaption.of("economy.vault_or_consumer_null")
098        );
099        checkTrue(
100                this.econHandler.getMoney(player) >= price,
101                TranslatableCaption.of("economy.cannot_afford_plot"),
102                TagResolver.builder()
103                        .tag("money", Tag.inserting(Component.text(this.econHandler.format(price))))
104                        .tag("balance", Tag.inserting(Component.text(this.econHandler.format(this.econHandler.getMoney(player)))))
105                        .build()
106        );
107        this.econHandler.withdrawMoney(player, price);
108        // Failure
109        // Success
110        confirm.run(this, () -> {
111            player.sendMessage(
112                    TranslatableCaption.of("economy.removed_balance"),
113                    TagResolver.resolver("money", Tag.inserting(Component.text(this.econHandler.format(price))))
114            );
115
116            this.econHandler.depositMoney(PlotSquared.platform().playerManager().getOfflinePlayer(plot.getOwnerAbs()), price);
117
118            PlotPlayer<?> owner = PlotSquared.platform().playerManager().getPlayerIfExists(plot.getOwnerAbs());
119            if (owner != null) {
120                owner.sendMessage(
121                        TranslatableCaption.of("economy.plot_sold"),
122                        TagResolver.builder()
123                                .tag("plot", Tag.inserting(Component.text(plot.getId().toString())))
124                                .tag("player", Tag.inserting(Component.text(player.getName())))
125                                .tag("price", Tag.inserting(Component.text(this.econHandler.format(price))))
126                                .build()
127                );
128            }
129            PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(PriceFlag.class);
130            PlotFlagRemoveEvent event = this.eventDispatcher.callFlagRemove(plotFlag, plot);
131            if (event.getEventResult() != Result.DENY) {
132                plot.removeFlag(event.getFlag());
133            }
134            plot.setOwner(player.getUUID());
135            plot.getPlotModificationManager().setSign(player.getName());
136            player.sendMessage(
137                    TranslatableCaption.of("working.claimed"),
138                    TagResolver.resolver("plot", Tag.inserting(Component.text(plot.getId().toString())))
139            );
140            whenDone.run(Buy.this, CommandResult.SUCCESS);
141        }, () -> {
142            this.econHandler.depositMoney(player, price);
143            whenDone.run(Buy.this, CommandResult.FAILURE);
144        });
145        return CompletableFuture.completedFuture(true);
146    }
147
148}