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.Settings;
024import com.plotsquared.core.configuration.caption.TranslatableCaption;
025import com.plotsquared.core.events.PlotDoneEvent;
026import com.plotsquared.core.events.PlotFlagAddEvent;
027import com.plotsquared.core.events.Result;
028import com.plotsquared.core.generator.HybridUtils;
029import com.plotsquared.core.location.Location;
030import com.plotsquared.core.permissions.Permission;
031import com.plotsquared.core.player.PlotPlayer;
032import com.plotsquared.core.plot.Plot;
033import com.plotsquared.core.plot.expiration.PlotAnalysis;
034import com.plotsquared.core.plot.flag.PlotFlag;
035import com.plotsquared.core.plot.flag.implementations.DoneFlag;
036import com.plotsquared.core.util.EventDispatcher;
037import com.plotsquared.core.util.task.RunnableVal;
038import net.kyori.adventure.text.minimessage.Template;
039import org.checkerframework.checker.nullness.qual.NonNull;
040
041@CommandDeclaration(command = "done",
042        aliases = {"submit"},
043        permission = "plots.done",
044        category = CommandCategory.SETTINGS,
045        requiredType = RequiredType.NONE)
046public class Done extends SubCommand {
047
048    private final EventDispatcher eventDispatcher;
049    private final HybridUtils hybridUtils;
050
051    @Inject
052    public Done(
053            final @NonNull EventDispatcher eventDispatcher,
054            final @NonNull HybridUtils hybridUtils
055    ) {
056        this.eventDispatcher = eventDispatcher;
057        this.hybridUtils = hybridUtils;
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) || !plot.hasOwner()) {
065            player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
066            return false;
067        }
068        PlotDoneEvent event = this.eventDispatcher.callDone(plot);
069        if (event.getEventResult() == Result.DENY) {
070            player.sendMessage(
071                    TranslatableCaption.of("events.event_denied"),
072                    Template.of("value", "Done")
073            );
074            return true;
075        }
076        boolean force = event.getEventResult() == Result.FORCE;
077        if (!force && !plot.isOwner(player.getUUID()) && !player.hasPermission(Permission.PERMISSION_ADMIN_COMMAND_DONE)) {
078            player.sendMessage(TranslatableCaption.of("permission.no_plot_perms"));
079            return false;
080        }
081        if (DoneFlag.isDone(plot)) {
082            player.sendMessage(TranslatableCaption.of("done.done_already_done"));
083            return false;
084        }
085        if (plot.getRunning() > 0) {
086            player.sendMessage(TranslatableCaption.of("errors.wait_for_timer"));
087            return false;
088        }
089        plot.addRunning();
090        player.sendMessage(
091                TranslatableCaption.of("web.generating_link"),
092                Template.of("plot", plot.getId().toString())
093        );
094        final Settings.Auto_Clear doneRequirements = Settings.AUTO_CLEAR.get("done");
095        if (PlotSquared.platform().expireManager() == null || doneRequirements == null) {
096            finish(plot, player, true);
097            plot.removeRunning();
098        } else {
099            this.hybridUtils.analyzePlot(plot, new RunnableVal<>() {
100                @Override
101                public void run(PlotAnalysis value) {
102                    plot.removeRunning();
103                    boolean result =
104                            value.getComplexity(doneRequirements) <= doneRequirements.THRESHOLD;
105                    finish(plot, player, result);
106                }
107            });
108        }
109        return true;
110    }
111
112    private void finish(Plot plot, PlotPlayer<?> player, boolean success) {
113        if (!success) {
114            player.sendMessage(TranslatableCaption.of("done.done_insufficient_complexity"));
115            return;
116        }
117        long flagValue = System.currentTimeMillis() / 1000;
118        PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(DoneFlag.class)
119                .createFlagInstance(Long.toString(flagValue));
120        PlotFlagAddEvent event = eventDispatcher.callFlagAdd(plotFlag, plot);
121        if (event.getEventResult() == Result.DENY) {
122            player.sendMessage(TranslatableCaption.of("events.event_denied"));
123            return;
124        }
125        plot.setFlag(plotFlag);
126        player.sendMessage(TranslatableCaption.of("done.done_success"));
127    }
128
129}