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.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
043@CommandDeclaration(command = "done",
044        aliases = {"submit"},
045        permission = "plots.done",
046        category = CommandCategory.SETTINGS,
047        requiredType = RequiredType.NONE)
048public class Done extends SubCommand {
049
050    private final EventDispatcher eventDispatcher;
051    private final HybridUtils hybridUtils;
052
053    @Inject
054    public Done(
055            final @NonNull EventDispatcher eventDispatcher,
056            final @NonNull HybridUtils hybridUtils
057    ) {
058        this.eventDispatcher = eventDispatcher;
059        this.hybridUtils = hybridUtils;
060    }
061
062    @Override
063    public boolean onCommand(final PlotPlayer<?> player, String[] args) {
064        Location location = player.getLocation();
065        final Plot plot = location.getPlotAbs();
066        if ((plot == null) || !plot.hasOwner()) {
067            player.sendMessage(TranslatableCaption.of("errors.not_in_plot"));
068            return false;
069        }
070        PlotDoneEvent event = this.eventDispatcher.callDone(plot);
071        if (event.getEventResult() == Result.DENY) {
072            player.sendMessage(
073                    TranslatableCaption.of("events.event_denied"),
074                    TagResolver.resolver("value", Tag.inserting(Component.text("Done")))
075            );
076            return true;
077        }
078        boolean force = event.getEventResult() == Result.FORCE;
079        if (!force && !plot.isOwner(player.getUUID()) && !player.hasPermission(Permission.PERMISSION_ADMIN_COMMAND_DONE)) {
080            player.sendMessage(TranslatableCaption.of("permission.no_plot_perms"));
081            return false;
082        }
083        if (DoneFlag.isDone(plot)) {
084            player.sendMessage(TranslatableCaption.of("done.done_already_done"));
085            return false;
086        }
087        if (plot.getRunning() > 0) {
088            player.sendMessage(TranslatableCaption.of("errors.wait_for_timer"));
089            return false;
090        }
091        plot.addRunning();
092        player.sendMessage(
093                TranslatableCaption.of("web.generating_link"),
094                TagResolver.resolver("plot", Tag.inserting(Component.text(plot.getId().toString())))
095        );
096        final Settings.Auto_Clear doneRequirements = Settings.AUTO_CLEAR.get("done");
097        if (PlotSquared.platform().expireManager() == null || doneRequirements == null) {
098            finish(plot, player, true);
099            plot.removeRunning();
100        } else {
101            this.hybridUtils.analyzePlot(plot, new RunnableVal<>() {
102                @Override
103                public void run(PlotAnalysis value) {
104                    plot.removeRunning();
105                    boolean result =
106                            value.getComplexity(doneRequirements) >= doneRequirements.THRESHOLD;
107                    finish(plot, player, result);
108                }
109            });
110        }
111        return true;
112    }
113
114    private void finish(Plot plot, PlotPlayer<?> player, boolean success) {
115        if (!success) {
116            player.sendMessage(TranslatableCaption.of("done.done_insufficient_complexity"));
117            return;
118        }
119        long flagValue = System.currentTimeMillis() / 1000;
120        PlotFlag<?, ?> plotFlag = plot.getFlagContainer().getFlag(DoneFlag.class)
121                .createFlagInstance(Long.toString(flagValue));
122        PlotFlagAddEvent event = eventDispatcher.callFlagAdd(plotFlag, plot);
123        if (event.getEventResult() == Result.DENY) {
124            player.sendMessage(TranslatableCaption.of("events.event_denied"));
125            return;
126        }
127        plot.setFlag(plotFlag);
128        player.sendMessage(TranslatableCaption.of("done.done_success"));
129    }
130
131}