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.queue;
020
021import com.plotsquared.core.location.Location;
022import com.sk89q.jnbt.CompoundTag;
023import com.sk89q.worldedit.function.pattern.Pattern;
024import com.sk89q.worldedit.world.biome.BiomeType;
025import com.sk89q.worldedit.world.block.BaseBlock;
026import com.sk89q.worldedit.world.block.BlockState;
027import org.checkerframework.checker.nullness.qual.NonNull;
028import org.checkerframework.checker.nullness.qual.Nullable;
029
030/**
031 * Queue that only sets blocks with a designated X-Z area, will accept any Y values. Requires all blocks be set normalized to
032 * zero in the x and z directions, i.e. starting from 0,0. An offset of the minimum point of the region will then be applied to
033 * x and z.
034 *
035 * @since 7.0.0
036 */
037public class ZeroedDelegateScopedQueueCoordinator extends DelegateQueueCoordinator {
038
039    private final Location min;
040    private final Location max;
041    private final int minX;
042    private final int minZ;
043
044    private final int maxX;
045    private final int maxZ;
046
047    private final int dx;
048    private final int dz;
049
050    /**
051     * Create a new ScopedQueueCoordinator instance that delegates to a given QueueCoordinator. Locations are inclusive.
052     *
053     * @since 7.0.0
054     */
055    public ZeroedDelegateScopedQueueCoordinator(@Nullable QueueCoordinator parent, @NonNull Location min, @NonNull Location max) {
056        super(parent);
057        this.min = min;
058        this.max = max;
059        this.minX = min.getX();
060        this.minZ = min.getZ();
061
062        this.maxX = max.getX();
063        this.maxZ = max.getZ();
064
065        this.dx = maxX - minX;
066        this.dz = maxZ - minZ;
067    }
068
069    @Override
070    public boolean setBiome(int x, int z, @NonNull BiomeType biome) {
071        return x >= 0 && x <= dx && z >= 0 && z <= dz && super.setBiome(x + minX, z + minZ, biome);
072    }
073
074    @Override
075    public boolean setBiome(int x, int y, int z, @NonNull BiomeType biome) {
076        return x >= 0 && x <= dx && z >= 0 && z <= dz && super.setBiome(x + minX, y, z + minZ, biome);
077    }
078
079    public void fillBiome(BiomeType biome) {
080        for (int y = min.getY(); y <= max.getY(); y++) {
081            for (int x = 0; x <= dx; x++) {
082                for (int z = 0; z < dz; z++) {
083                    setBiome(x, y, z, biome);
084                }
085            }
086        }
087    }
088
089    @Override
090    public boolean setBlock(int x, int y, int z, @NonNull BaseBlock id) {
091        return x >= 0 && x <= dx && z >= 0 && z <= dz && super.setBlock(x + minX, y, z + minZ, id);
092    }
093
094    @Override
095    public boolean setBlock(int x, int y, int z, @NonNull BlockState id) {
096        return x >= 0 && x <= dx && z >= 0 && z <= dz && super.setBlock(x + minX, y, z + minZ, id);
097    }
098
099    @Override
100    public boolean setBlock(int x, int y, int z, @NonNull Pattern pattern) {
101        return x >= 0 && x <= dx && z >= 0 && z <= dz && super.setBlock(x + minX, y, z + minZ, pattern);
102    }
103
104    @Override
105    public boolean setTile(int x, int y, int z, @NonNull CompoundTag tag) {
106        return x >= 0 && x <= dx && z >= 0 && z <= dz && super.setTile(x + minX, y, z + minZ, tag);
107    }
108
109    public @NonNull Location getMin() {
110        return min;
111    }
112
113    public @NonNull Location getMax() {
114        return max;
115    }
116
117}