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.sk89q.jnbt.CompoundTag;
022import com.sk89q.worldedit.function.pattern.Pattern;
023import com.sk89q.worldedit.math.BlockVector3;
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 * Offsets input coordinates and delegates to a parent queue
032 */
033public class LocationOffsetDelegateQueueCoordinator extends DelegateQueueCoordinator {
034
035    private final boolean[][] canPlace;
036    private final int blockX;
037    private final int blockZ;
038
039    public LocationOffsetDelegateQueueCoordinator(
040            final boolean[][] canPlace,
041            final int blockX,
042            final int blockZ,
043            @Nullable QueueCoordinator parent
044    ) {
045        super(parent);
046        this.canPlace = canPlace;
047        this.blockX = blockX;
048        this.blockZ = blockZ;
049    }
050
051    @Override
052    public boolean setBlock(int x, int y, int z, @NonNull BlockState id) {
053        try {
054            if (canPlace[x - blockX][z - blockZ]) {
055                return super.setBlock(x, y, z, id);
056            }
057        } catch (final Exception e) {
058            throw e;
059        }
060        return false;
061    }
062
063    @Override
064    public boolean setBlock(int x, int y, int z, @NonNull BaseBlock id) {
065        try {
066            if (canPlace[x - blockX][z - blockZ]) {
067                return super.setBlock(x, y, z, id);
068            }
069        } catch (final Exception e) {
070            throw e;
071        }
072        return false;
073    }
074
075    @Override
076    public boolean setBlock(int x, int y, int z, @NonNull Pattern pattern) {
077        final BlockVector3 blockVector3 = BlockVector3.at(x + blockX, y, z + blockZ);
078        return this.setBlock(x, y, z, pattern.applyBlock(blockVector3));
079    }
080
081    @Override
082    public boolean setBiome(int x, int z, @NonNull BiomeType biome) {
083        try {
084            if (canPlace[x - blockX][z - blockZ]) {
085                return super.setBiome(x, z, biome);
086            }
087        } catch (final Exception e) {
088            throw e;
089        }
090        return false;
091    }
092
093    @Override
094    public boolean setBiome(int x, int y, int z, @NonNull BiomeType biome) {
095        try {
096            if (canPlace[x - blockX][z - blockZ]) {
097                return super.setBiome(x, y, z, biome);
098            }
099        } catch (final Exception e) {
100            throw e;
101        }
102        return false;
103    }
104
105    @Override
106    public boolean setTile(int x, int y, int z, @NonNull CompoundTag tag) {
107        try {
108            if (canPlace[x - blockX][z - blockZ]) {
109                return super.setTile(x, y, z, tag);
110            }
111        } catch (final Exception e) {
112            throw e;
113        }
114        return false;
115    }
116
117}