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.util.task;
020
021import com.plotsquared.core.configuration.caption.Templates;
022import com.plotsquared.core.configuration.caption.TranslatableCaption;
023import com.plotsquared.core.events.PlotMergeEvent;
024import com.plotsquared.core.events.Result;
025import com.plotsquared.core.location.Direction;
026import com.plotsquared.core.player.MetaDataAccess;
027import com.plotsquared.core.player.PlayerMetaDataKeys;
028import com.plotsquared.core.player.PlotPlayer;
029import com.plotsquared.core.plot.Plot;
030import com.plotsquared.core.plot.PlotArea;
031import com.plotsquared.core.util.EventDispatcher;
032
033import java.util.concurrent.Callable;
034
035public final class AutoClaimFinishTask implements Callable<Boolean> {
036
037    private final PlotPlayer<?> player;
038    private final Plot plot;
039    private final PlotArea area;
040    private final String schematic;
041    private final EventDispatcher eventDispatcher;
042
043    public AutoClaimFinishTask(
044            final PlotPlayer<?> player, final Plot plot, final PlotArea area,
045            final String schematic, final EventDispatcher eventDispatcher
046    ) {
047        this.player = player;
048        this.plot = plot;
049        this.area = area;
050        this.schematic = schematic;
051        this.eventDispatcher = eventDispatcher;
052    }
053
054    @Override
055    public Boolean call() {
056        try (final MetaDataAccess<Boolean> autoAccess
057                     = player.accessTemporaryMetaData(PlayerMetaDataKeys.TEMPORARY_AUTO)) {
058            autoAccess.remove();
059        }
060        if (plot == null) {
061            player.sendMessage(TranslatableCaption.of("errors.no_free_plots"));
062            return false;
063        }
064        plot.claim(player, true, schematic, false, true);
065        eventDispatcher.callPostAuto(player, plot);
066        if (area.isAutoMerge()) {
067            PlotMergeEvent event = this.eventDispatcher.callMerge(plot, Direction.ALL, Integer.MAX_VALUE, player);
068            if (event.getEventResult() == Result.DENY) {
069                player.sendMessage(
070                        TranslatableCaption.of("events.event_denied"),
071                        Templates.of("value", "Auto Merge")
072                );
073            } else {
074                if (plot.getPlotModificationManager().autoMerge(event.getDir(), event.getMax(), player.getUUID(), player, true)) {
075                    eventDispatcher.callPostMerge(player, plot);
076                }
077            }
078        }
079        return true;
080    }
081
082}