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;
020
021import com.plotsquared.core.PlotSquared;
022import com.plotsquared.core.location.Location;
023import com.plotsquared.core.queue.QueueCoordinator;
024import com.plotsquared.core.queue.ScopedQueueCoordinator;
025import com.plotsquared.core.util.task.RunnableVal;
026import com.sk89q.worldedit.math.BlockVector2;
027import com.sk89q.worldedit.world.World;
028
029import java.util.Map;
030import java.util.concurrent.CompletableFuture;
031import java.util.concurrent.ConcurrentHashMap;
032
033public abstract class ChunkManager {
034
035    private static final Map<BlockVector2, RunnableVal<ScopedQueueCoordinator>> forceChunks = new ConcurrentHashMap<>();
036    private static final Map<BlockVector2, RunnableVal<ScopedQueueCoordinator>> addChunks = new ConcurrentHashMap<>();
037
038    /**
039     * @deprecated {@link ScopedQueueCoordinator} will be renamed in v7.
040     */
041    @Deprecated(forRemoval = true, since = "6.9.0")
042    public static void setChunkInPlotArea(
043            RunnableVal<ScopedQueueCoordinator> force,
044            RunnableVal<ScopedQueueCoordinator> add,
045            String world,
046            BlockVector2 loc
047    ) {
048        World weWorld = PlotSquared.platform().worldUtil().getWeWorld(world);
049        QueueCoordinator queue = PlotSquared.platform().globalBlockQueue().getNewQueue(weWorld);
050        if (PlotSquared.get().getPlotAreaManager().isAugmented(world) && PlotSquared.get().isNonStandardGeneration(world, loc)) {
051            int blockX = loc.getX() << 4;
052            int blockZ = loc.getZ() << 4;
053            ScopedQueueCoordinator scoped =
054                    new ScopedQueueCoordinator(
055                            queue,
056                            Location.at(world, blockX, weWorld.getMinY(), blockZ),
057                            Location.at(world, blockX + 15, weWorld.getMaxY(), blockZ + 15)
058                    );
059            if (force != null) {
060                force.run(scoped);
061            } else {
062                scoped.regenChunk(loc.getX(), loc.getZ());
063                if (add != null) {
064                    add.run(scoped);
065                }
066            }
067            queue.enqueue();
068        } else {
069            if (force != null) {
070                forceChunks.put(loc, force);
071            }
072            addChunks.put(loc, add);
073            queue.regenChunk(loc.getX(), loc.getZ());
074            forceChunks.remove(loc);
075            addChunks.remove(loc);
076        }
077    }
078
079    /**
080     * @deprecated {@link ScopedQueueCoordinator} will be renamed in v7.
081     */
082    @Deprecated(forRemoval = true, since = "6.9.0")
083    public static boolean preProcessChunk(BlockVector2 loc, ScopedQueueCoordinator queue) {
084        final RunnableVal<ScopedQueueCoordinator> forceChunk = forceChunks.get(loc);
085        if (forceChunk != null) {
086            forceChunk.run(queue);
087            forceChunks.remove(loc);
088            return true;
089        }
090        return false;
091    }
092
093    /**
094     * @deprecated {@link ScopedQueueCoordinator} will be renamed in v7.
095     */
096    @Deprecated(forRemoval = true, since = "6.9.0")
097    public static boolean postProcessChunk(BlockVector2 loc, ScopedQueueCoordinator queue) {
098        final RunnableVal<ScopedQueueCoordinator> addChunk = forceChunks.get(loc);
099        if (addChunk != null) {
100            addChunk.run(queue);
101            addChunks.remove(loc);
102            return true;
103        }
104        return false;
105    }
106
107    @Deprecated
108    public abstract CompletableFuture<?> loadChunk(String world, BlockVector2 loc, boolean force);
109
110}