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
|
|
|
|
*
|
2020-08-25 01:31:47 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU 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.
|
|
|
|
*
|
2020-08-25 01:31:47 +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 General Public License for more details.
|
2014-03-31 23:55:58 +00:00
|
|
|
*
|
2020-08-25 01:31:47 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2014-03-31 23:55:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
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-09-28 10:13:02 +00:00
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
import java.util.concurrent.Future;
|
|
|
|
|
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-09-25 14:00:42 +00:00
|
|
|
private final LoadingCache<Long, ChunkFilterBlock> threadIdToFilter;
|
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;
|
2020-09-25 14:00:42 +00:00
|
|
|
this.threadIdToFilter = FaweCache.IMP.createCache(() -> new CharFilterBlock(getExtent()));
|
|
|
|
}
|
|
|
|
|
|
|
|
private MaskingExtent(Extent extent, Mask mask, LoadingCache<Long, ChunkFilterBlock> threadIdToFilter) {
|
|
|
|
super(extent);
|
|
|
|
checkNotNull(mask);
|
|
|
|
this.mask = mask;
|
|
|
|
this.threadIdToFilter = threadIdToFilter;
|
2014-03-31 23:55:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the mask.
|
|
|
|
*
|
|
|
|
* @return the mask
|
|
|
|
*/
|
|
|
|
public Mask getMask() {
|
2020-09-25 14:00:42 +00:00
|
|
|
return this.mask;
|
2014-03-31 23:55:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-09-25 14:00:42 +00:00
|
|
|
return this.mask.test(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-09-25 14:00:42 +00:00
|
|
|
return this.mask.test(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-09-25 14:00:42 +00:00
|
|
|
return this.mask.test(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
|
2020-09-25 14:00:42 +00:00
|
|
|
public IChunkSet processSet(final IChunk chunk, final IChunkGet get, final IChunkSet set) {
|
|
|
|
final ChunkFilterBlock filter = threadIdToFilter.getUnchecked(Thread.currentThread().getId());
|
|
|
|
return filter.filter(chunk, get, set, MaskingExtent.this);
|
2019-12-16 09:07:51 +00:00
|
|
|
}
|
2018-08-12 16:36:39 +00:00
|
|
|
|
2020-09-28 10:13:02 +00:00
|
|
|
@Override
|
|
|
|
public Future<IChunkSet> postProcessSet(IChunk chunk, IChunkGet get, IChunkSet set) {
|
|
|
|
// This should not do anything otherwise dangerous...
|
|
|
|
return CompletableFuture.completedFuture(set);
|
|
|
|
}
|
|
|
|
|
2019-12-16 09:07:51 +00:00
|
|
|
@Override
|
2020-09-25 14:00:42 +00:00
|
|
|
public void applyBlock(final FilterBlock block) {
|
|
|
|
if (!this.mask.test(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) {
|
2020-10-05 17:41:41 +00:00
|
|
|
if (child == getExtent()) {
|
|
|
|
return this;
|
|
|
|
}
|
2020-09-25 14:00:42 +00:00
|
|
|
return new MaskingExtent(child, this.mask.copy(), this.threadIdToFilter);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Filter fork() {
|
|
|
|
return new MaskingExtent(getExtent(), this.mask.copy(), this.threadIdToFilter);
|
2019-12-16 09:07:51 +00:00
|
|
|
}
|
2014-03-31 23:55:58 +00:00
|
|
|
}
|