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.backup.BackupManager;
023import com.plotsquared.core.configuration.Settings;
024import com.plotsquared.core.configuration.caption.TranslatableCaption;
025import com.plotsquared.core.events.PlotFlagRemoveEvent;
026import com.plotsquared.core.events.Result;
027import com.plotsquared.core.events.TeleportCause;
028import com.plotsquared.core.player.PlotPlayer;
029import com.plotsquared.core.plot.Plot;
030import com.plotsquared.core.plot.flag.PlotFlag;
031import com.plotsquared.core.plot.flag.implementations.AnalysisFlag;
032import com.plotsquared.core.plot.flag.implementations.DoneFlag;
033import com.plotsquared.core.queue.GlobalBlockQueue;
034import com.plotsquared.core.util.EventDispatcher;
035import com.plotsquared.core.util.task.RunnableVal2;
036import com.plotsquared.core.util.task.RunnableVal3;
037import com.plotsquared.core.util.task.TaskManager;
038import net.kyori.adventure.text.Component;
039import net.kyori.adventure.text.minimessage.tag.Tag;
040import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
041import org.checkerframework.checker.nullness.qual.NonNull;
042
043import java.util.concurrent.CompletableFuture;
044
045@CommandDeclaration(command = "clear",
046        requiredType = RequiredType.NONE,
047        permission = "plots.clear",
048        category = CommandCategory.APPEARANCE,
049        usage = "/plot clear",
050        aliases = "reset",
051        confirmation = true)
052public class Clear extends Command {
053
054    private final EventDispatcher eventDispatcher;
055    @SuppressWarnings({"unused", "FieldCanBeLocal"})
056    private final GlobalBlockQueue blockQueue;
057
058    @Inject
059    public Clear(
060            final @NonNull EventDispatcher eventDispatcher,
061            final @NonNull GlobalBlockQueue blockQueue
062    ) {
063        super(MainCommand.getInstance(), true);
064        this.eventDispatcher = eventDispatcher;
065        this.blockQueue = blockQueue;
066    }
067
068    @Override
069    public CompletableFuture<Boolean> execute(
070            final PlotPlayer<?> player, String[] args,
071            RunnableVal3<Command, Runnable, Runnable> confirm,
072            RunnableVal2<Command, CommandResult> whenDone
073    ) throws CommandException {
074        if (args.length != 0) {
075            sendUsage(player);
076            return CompletableFuture.completedFuture(false);
077        }
078        final Plot plot = check(player.getCurrentPlot(), TranslatableCaption.of("errors.not_in_plot"));
079        Result eventResult = this.eventDispatcher.callClear(plot).getEventResult();
080        if (eventResult == Result.DENY) {
081            player.sendMessage(
082                    TranslatableCaption.of("events.event_denied"),
083                    TagResolver.resolver("value", Tag.inserting(Component.text("Clear")))
084            );
085            return CompletableFuture.completedFuture(true);
086        }
087        if (plot.getVolume() > Integer.MAX_VALUE) {
088            player.sendMessage(TranslatableCaption.of("schematics.schematic_too_large"));
089            return CompletableFuture.completedFuture(true);
090        }
091        boolean force = eventResult == Result.FORCE;
092        checkTrue(
093                force || plot.isOwner(player.getUUID()) || player.hasPermission("plots.admin.command.clear"),
094                TranslatableCaption.of("permission.no_plot_perms")
095        );
096        checkTrue(plot.getRunning() == 0, TranslatableCaption.of("errors.wait_for_timer"));
097        checkTrue(
098                force || !Settings.Done.RESTRICT_BUILDING || !DoneFlag.isDone(plot) || player.hasPermission("plots.continue"),
099                TranslatableCaption.of("done.done_already_done")
100        );
101        confirm.run(this, () -> {
102            if (Settings.Teleport.ON_CLEAR) {
103                plot.getPlayersInPlot().forEach(playerInPlot -> plot.teleportPlayer(playerInPlot, TeleportCause.COMMAND_CLEAR,
104                        result -> {
105                        }
106                ));
107            }
108            BackupManager.backup(player, plot, () -> {
109                final long start = System.currentTimeMillis();
110                boolean result = plot.getPlotModificationManager().clear(true, false, player, () -> TaskManager.runTask(() -> {
111                    plot.removeRunning();
112                    // If the state changes, then mark it as no longer done
113                    if (DoneFlag.isDone(plot)) {
114                        PlotFlag<?, ?> plotFlag =
115                                plot.getFlagContainer().getFlag(DoneFlag.class);
116                        PlotFlagRemoveEvent event = this.eventDispatcher
117                                .callFlagRemove(plotFlag, plot);
118                        if (event.getEventResult() != Result.DENY) {
119                            plot.removeFlag(event.getFlag());
120                        }
121                    }
122                    if (!plot.getFlag(AnalysisFlag.class).isEmpty()) {
123                        PlotFlag<?, ?> plotFlag =
124                                plot.getFlagContainer().getFlag(AnalysisFlag.class);
125                        PlotFlagRemoveEvent event = this.eventDispatcher
126                                .callFlagRemove(plotFlag, plot);
127                        if (event.getEventResult() != Result.DENY) {
128                            plot.removeFlag(event.getFlag());
129                        }
130                    }
131                    player.sendMessage(
132                            TranslatableCaption.of("working.clearing_done"),
133                            TagResolver.builder()
134                                    .tag("amount", Tag.inserting(Component.text(System.currentTimeMillis() - start)))
135                                    .tag("plot", Tag.inserting(Component.text(plot.getId().toString())))
136                                    .build()
137                    );
138                }));
139                if (!result) {
140                    player.sendMessage(TranslatableCaption.of("errors.wait_for_timer"));
141                } else {
142                    plot.addRunning();
143                }
144            });
145        }, null);
146        return CompletableFuture.completedFuture(true);
147    }
148
149}