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.ZeroedDelegateScopedQueueCoordinator;
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<ZeroedDelegateScopedQueueCoordinator>> forceChunks = new ConcurrentHashMap<>();
036    private static final Map<BlockVector2, RunnableVal<ZeroedDelegateScopedQueueCoordinator>> addChunks = new ConcurrentHashMap<>();
037
038    /**
039     * @since 7.0.0
040     */
041    public static void setChunkInPlotArea(
042            RunnableVal<ZeroedDelegateScopedQueueCoordinator> force,
043            RunnableVal<ZeroedDelegateScopedQueueCoordinator> add,
044            String world,
045            BlockVector2 loc
046    ) {
047        World weWorld = PlotSquared.platform().worldUtil().getWeWorld(world);
048        QueueCoordinator queue = PlotSquared.platform().globalBlockQueue().getNewQueue(weWorld);
049        if (PlotSquared.get().getPlotAreaManager().isAugmented(world) && PlotSquared.get().isNonStandardGeneration(world, loc)) {
050            int blockX = loc.getX() << 4;
051            int blockZ = loc.getZ() << 4;
052            ZeroedDelegateScopedQueueCoordinator scoped =
053                    new ZeroedDelegateScopedQueueCoordinator(
054                            queue,
055                            Location.at(world, blockX, weWorld.getMinY(), blockZ),
056                            Location.at(world, blockX + 15, weWorld.getMaxY(), blockZ + 15)
057                    );
058            if (force != null) {
059                force.run(scoped);
060            } else {
061                scoped.regenChunk(loc.getX(), loc.getZ());
062                if (add != null) {
063                    add.run(scoped);
064                }
065            }
066            queue.enqueue();
067        } else {
068            if (force != null) {
069                forceChunks.put(loc, force);
070            }
071            addChunks.put(loc, add);
072            queue.regenChunk(loc.getX(), loc.getZ());
073            forceChunks.remove(loc);
074            addChunks.remove(loc);
075        }
076    }
077
078    /**
079     * @since 7.0.0
080     */
081    public static boolean preProcessChunk(BlockVector2 loc, ZeroedDelegateScopedQueueCoordinator queue) {
082        final RunnableVal<ZeroedDelegateScopedQueueCoordinator> forceChunk = forceChunks.get(loc);
083        if (forceChunk != null) {
084            forceChunk.run(queue);
085            forceChunks.remove(loc);
086            return true;
087        }
088        return false;
089    }
090
091    /**
092     * @since 7.0.0
093     */
094    public static boolean postProcessChunk(BlockVector2 loc, ZeroedDelegateScopedQueueCoordinator queue) {
095        final RunnableVal<ZeroedDelegateScopedQueueCoordinator> addChunk = forceChunks.get(loc);
096        if (addChunk != null) {
097            addChunk.run(queue);
098            addChunks.remove(loc);
099            return true;
100        }
101        return false;
102    }
103
104    @Deprecated
105    public abstract CompletableFuture<?> loadChunk(String world, BlockVector2 loc, boolean force);
106
107}