2014-03-31 23:55:58 +00:00
|
|
|
/*
|
|
|
|
* WorldEdit, a Minecraft world manipulation toolkit
|
|
|
|
* Copyright (C) sk89q <http://www.sk89q.com>
|
|
|
|
* Copyright (C) WorldEdit team and contributors
|
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
2014-03-31 23:55:58 +00:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
|
|
|
* for more details.
|
2014-03-31 23:55:58 +00:00
|
|
|
*
|
2014-04-04 22:03:18 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2014-03-31 23:55:58 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.sk89q.worldedit.extent;
|
|
|
|
|
2019-12-16 09:07:51 +00:00
|
|
|
import com.boydti.fawe.FaweCache;
|
|
|
|
import com.boydti.fawe.beta.Filter;
|
|
|
|
import com.boydti.fawe.beta.IBatchProcessor;
|
|
|
|
import com.boydti.fawe.beta.IChunk;
|
|
|
|
import com.boydti.fawe.beta.IChunkGet;
|
|
|
|
import com.boydti.fawe.beta.IChunkSet;
|
|
|
|
import com.boydti.fawe.beta.implementation.filter.block.CharFilterBlock;
|
|
|
|
import com.boydti.fawe.beta.implementation.filter.block.ChunkFilterBlock;
|
|
|
|
import com.boydti.fawe.beta.implementation.filter.block.FilterBlock;
|
|
|
|
import com.google.common.cache.LoadingCache;
|
2014-03-31 23:55:58 +00:00
|
|
|
import com.sk89q.worldedit.WorldEditException;
|
2019-06-12 13:45:41 +00:00
|
|
|
import com.sk89q.worldedit.function.mask.Mask;
|
2019-06-25 17:07:47 +00:00
|
|
|
import com.sk89q.worldedit.math.BlockVector2;
|
2019-06-12 13:45:41 +00:00
|
|
|
import com.sk89q.worldedit.math.BlockVector3;
|
2019-06-25 17:07:47 +00:00
|
|
|
import com.sk89q.worldedit.world.biome.BiomeType;
|
2018-07-30 13:26:06 +00:00
|
|
|
import com.sk89q.worldedit.world.block.BlockStateHolder;
|
2014-03-31 23:55:58 +00:00
|
|
|
|
2020-05-14 21:32:32 +00:00
|
|
|
import static com.google.common.base.Preconditions.checkNotNull;
|
2019-12-16 09:07:51 +00:00
|
|
|
|
2014-04-01 03:29:05 +00:00
|
|
|
/**
|
|
|
|
* Requires that all mutating methods pass a given {@link Mask}.
|
|
|
|
*/
|
2019-12-16 09:07:51 +00:00
|
|
|
public class MaskingExtent extends AbstractDelegateExtent implements IBatchProcessor, Filter {
|
2014-03-31 23:55:58 +00:00
|
|
|
|
|
|
|
private Mask mask;
|
2020-01-29 20:01:38 +00:00
|
|
|
private LoadingCache<Long, ChunkFilterBlock> threadIdToFilter = FaweCache.IMP.createCache(() -> new CharFilterBlock(getExtent()));
|
2014-03-31 23:55:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new instance.
|
|
|
|
*
|
|
|
|
* @param extent the extent
|
2019-06-04 15:48:30 +00:00
|
|
|
* @param mask the mask
|
2014-03-31 23:55:58 +00:00
|
|
|
*/
|
|
|
|
public MaskingExtent(Extent extent, Mask mask) {
|
|
|
|
super(extent);
|
|
|
|
checkNotNull(mask);
|
|
|
|
this.mask = mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the mask.
|
|
|
|
*
|
|
|
|
* @return the mask
|
|
|
|
*/
|
|
|
|
public Mask getMask() {
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set a mask.
|
|
|
|
*
|
|
|
|
* @param mask a mask
|
|
|
|
*/
|
|
|
|
public void setMask(Mask mask) {
|
|
|
|
checkNotNull(mask);
|
|
|
|
this.mask = mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-12-27 00:39:10 +00:00
|
|
|
public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 location, B block) throws WorldEditException {
|
2020-01-04 10:11:13 +00:00
|
|
|
return mask.test(getExtent(), location) && super.setBlock(location, block);
|
2014-03-31 23:55:58 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
@Override
|
2019-04-03 05:53:34 +00:00
|
|
|
public boolean setBiome(BlockVector2 position, BiomeType biome) {
|
2020-01-04 10:11:13 +00:00
|
|
|
return mask.test(getExtent(), position.toBlockVector3()) && super.setBiome(position, biome);
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-04-03 05:53:34 +00:00
|
|
|
public boolean setBiome(int x, int y, int z, BiomeType biome) {
|
2020-01-04 10:11:13 +00:00
|
|
|
return mask.test(getExtent(), BlockVector3.at(x, y, z)) && super.setBiome(x, y, z, biome);
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 09:07:51 +00:00
|
|
|
@Override
|
|
|
|
public IChunkSet processSet(IChunk chunk, IChunkGet get, IChunkSet set) {
|
|
|
|
ChunkFilterBlock filter = threadIdToFilter.getUnchecked(Thread.currentThread().getId());
|
|
|
|
return filter.filter(chunk, get, set, this);
|
|
|
|
}
|
2018-08-12 16:36:39 +00:00
|
|
|
|
2019-12-16 09:07:51 +00:00
|
|
|
@Override
|
|
|
|
public void applyBlock(FilterBlock block) {
|
2020-05-14 21:32:32 +00:00
|
|
|
//TODO: Find a way to make masking thread safe without having to synchonise the whole extent
|
|
|
|
synchronized (this) {
|
|
|
|
if (!mask.test(getExtent(), block)) {
|
|
|
|
block.setOrdinal(0);
|
|
|
|
}
|
2019-12-16 09:07:51 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-12 14:03:07 +00:00
|
|
|
|
2019-12-16 09:07:51 +00:00
|
|
|
@Override
|
|
|
|
public Extent construct(Extent child) {
|
|
|
|
if (child == getExtent()) return this;
|
|
|
|
return new MaskingExtent(child, mask);
|
|
|
|
}
|
2014-03-31 23:55:58 +00:00
|
|
|
}
|