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.configuration.Settings;
023import com.plotsquared.core.configuration.caption.TranslatableCaption;
024import com.plotsquared.core.events.Result;
025import com.plotsquared.core.events.TeleportCause;
026import com.plotsquared.core.location.Location;
027import com.plotsquared.core.permissions.Permission;
028import com.plotsquared.core.player.PlotPlayer;
029import com.plotsquared.core.plot.Plot;
030import com.plotsquared.core.plot.PlotArea;
031import com.plotsquared.core.util.EconHandler;
032import com.plotsquared.core.util.EventDispatcher;
033import com.plotsquared.core.util.PlotExpression;
034import com.plotsquared.core.util.task.TaskManager;
035import net.kyori.adventure.text.minimessage.Template;
036import org.checkerframework.checker.nullness.qual.NonNull;
037
038
039@CommandDeclaration(command = "delete",
040        permission = "plots.delete",
041        usage = "/plot delete",
042        aliases = {"dispose", "del"},
043        category = CommandCategory.CLAIMING,
044        requiredType = RequiredType.NONE,
045        confirmation = true)
046public class Delete extends SubCommand {
047
048    private final EventDispatcher eventDispatcher;
049    private final EconHandler econHandler;
050
051    @Inject
052    public Delete(
053            final @NonNull EventDispatcher eventDispatcher,
054            final @NonNull EconHandler econHandler
055    ) {
056        this.eventDispatcher = eventDispatcher;
057        this.econHandler = econHandler;
058    }
059
060    @Override
061    public boolean onCommand(final PlotPlayer<?> player, String[] args) {
062        Location location = player.getLocation();
063        final Plot plot = location.getPlotAbs();
064        if (plot == null) {
065            player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
066            return false;
067        }
068        if (!plot.hasOwner()) {
069            player.sendMessage(TranslatableCaption.of("info.plot_unowned"));
070            return false;
071        }
072        if (plot.getVolume() > Integer.MAX_VALUE) {
073            player.sendMessage(TranslatableCaption.of("schematics.schematic_too_large"));
074            return false;
075        }
076        Result eventResult = this.eventDispatcher.callDelete(plot).getEventResult();
077        if (eventResult == Result.DENY) {
078            player.sendMessage(
079                    TranslatableCaption.of("events.event_denied"),
080                    Template.of("value", "Delete")
081            );
082            return true;
083        }
084        boolean force = eventResult == Result.FORCE;
085        if (!force && !plot.isOwner(player.getUUID()) && !player.hasPermission(Permission.PERMISSION_ADMIN_COMMAND_DELETE)) {
086            player.sendMessage(TranslatableCaption.of("permission.no_plot_perms"));
087            return false;
088        }
089        final PlotArea plotArea = plot.getArea();
090        final java.util.Set<Plot> plots = plot.getConnectedPlots();
091        final int currentPlots = Settings.Limit.GLOBAL ?
092                player.getPlotCount() :
093                player.getPlotCount(location.getWorldName());
094        Runnable run = () -> {
095            if (plot.getRunning() > 0) {
096                player.sendMessage(TranslatableCaption.of("errors.wait_for_timer"));
097                return;
098            }
099            final long start = System.currentTimeMillis();
100            if (Settings.Teleport.ON_DELETE) {
101                plot.getPlayersInPlot().forEach(playerInPlot -> plot.teleportPlayer(playerInPlot, TeleportCause.COMMAND_DELETE,
102                        result -> {
103                        }
104                ));
105            }
106            boolean result = plot.getPlotModificationManager().deletePlot(player, () -> {
107                plot.removeRunning();
108                if (this.econHandler.isEnabled(plotArea)) {
109                    PlotExpression valueExr = plotArea.getPrices().get("sell");
110                    double value = plots.size() * valueExr.evaluate(currentPlots);
111                    if (value > 0d) {
112                        this.econHandler.depositMoney(player, value);
113                        player.sendMessage(
114                                TranslatableCaption.of("economy.added_balance"),
115                                Template.of("money", this.econHandler.format(value))
116                        );
117                    }
118                }
119                player.sendMessage(
120                        TranslatableCaption.of("working.deleting_done"),
121                        Template.of("amount", String.valueOf(System.currentTimeMillis() - start)),
122                        Template.of("plot", plot.getId().toString())
123                );
124                eventDispatcher.callPostDelete(plot);
125            });
126            if (result) {
127                plot.addRunning();
128            } else {
129                player.sendMessage(TranslatableCaption.of("errors.wait_for_timer"));
130            }
131        };
132        if (hasConfirmation(player)) {
133            CmdConfirm.addPending(player, getCommandString() + ' ' + plot.getId(), run);
134        } else {
135            TaskManager.runTask(run);
136        }
137        return true;
138    }
139
140}