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.listener;
020
021import com.plotsquared.core.util.WEManager;
022import com.sk89q.worldedit.WorldEditException;
023import com.sk89q.worldedit.entity.BaseEntity;
024import com.sk89q.worldedit.entity.Entity;
025import com.sk89q.worldedit.extent.AbstractDelegateExtent;
026import com.sk89q.worldedit.extent.Extent;
027import com.sk89q.worldedit.math.BlockVector2;
028import com.sk89q.worldedit.math.BlockVector3;
029import com.sk89q.worldedit.regions.CuboidRegion;
030import com.sk89q.worldedit.util.Location;
031import com.sk89q.worldedit.world.biome.BiomeType;
032import com.sk89q.worldedit.world.block.BaseBlock;
033import com.sk89q.worldedit.world.block.BlockState;
034import com.sk89q.worldedit.world.block.BlockStateHolder;
035import com.sk89q.worldedit.world.block.BlockTypes;
036
037import java.util.Set;
038
039public class WEExtent extends AbstractDelegateExtent {
040
041    public static BlockState AIRSTATE = BlockTypes.AIR.getDefaultState();
042    public static BaseBlock AIRBASE = BlockTypes.AIR.getDefaultState().toBaseBlock();
043    private final Set<CuboidRegion> mask;
044
045    public WEExtent(Set<CuboidRegion> mask, Extent extent) {
046        super(extent);
047        this.mask = mask;
048    }
049
050    @SuppressWarnings("unchecked")
051    @Override
052    public boolean setBlock(BlockVector3 location, BlockStateHolder block)
053            throws WorldEditException {
054        return WEManager.maskContains(this.mask, location.getX(), location.getY(), location.getZ())
055                && super.setBlock(location, block);
056    }
057
058    @Override
059    public Entity createEntity(Location location, BaseEntity entity) {
060        if (WEManager.maskContains(this.mask, location.getBlockX(), location.getBlockY(),
061                location.getBlockZ()
062        )) {
063            return super.createEntity(location, entity);
064        }
065        return null;
066    }
067
068    @Override
069    public boolean setBiome(BlockVector2 position, BiomeType biome) {
070        return WEManager.maskContains(this.mask, position.getX(), position.getZ()) && super
071                .setBiome(position, biome);
072    }
073
074    @Override
075    public BlockState getBlock(BlockVector3 location) {
076        if (WEManager.maskContains(this.mask, location.getX(), location.getY(), location.getZ())) {
077            return super.getBlock(location);
078        }
079        return AIRSTATE;
080    }
081
082    @Override
083    public BaseBlock getFullBlock(BlockVector3 location) {
084        if (WEManager.maskContains(this.mask, location.getX(), location.getY(), location.getZ())) {
085            return super.getFullBlock(location);
086        }
087        return AIRBASE;
088    }
089
090}