Added new Mask interface and deprecated old one.

This commit is contained in:
sk89q 2014-03-30 01:36:02 -07:00
parent 9ab1d0f150
commit 9113cd4bd3
35 changed files with 753 additions and 182 deletions

View File

@ -33,6 +33,7 @@ import com.sk89q.worldedit.function.block.BlockCount;
import com.sk89q.worldedit.function.block.BlockReplace; import com.sk89q.worldedit.function.block.BlockReplace;
import com.sk89q.worldedit.function.block.Naturalizer; import com.sk89q.worldedit.function.block.Naturalizer;
import com.sk89q.worldedit.function.generator.GardenPatchGenerator; import com.sk89q.worldedit.function.generator.GardenPatchGenerator;
import com.sk89q.worldedit.function.mask.*;
import com.sk89q.worldedit.function.operation.OperationHelper; import com.sk89q.worldedit.function.operation.OperationHelper;
import com.sk89q.worldedit.function.util.RegionOffset; import com.sk89q.worldedit.function.util.RegionOffset;
import com.sk89q.worldedit.function.visitor.DownwardVisitor; import com.sk89q.worldedit.function.visitor.DownwardVisitor;
@ -42,7 +43,8 @@ import com.sk89q.worldedit.function.visitor.RegionVisitor;
import com.sk89q.worldedit.interpolation.Interpolation; import com.sk89q.worldedit.interpolation.Interpolation;
import com.sk89q.worldedit.interpolation.KochanekBartelsInterpolation; import com.sk89q.worldedit.interpolation.KochanekBartelsInterpolation;
import com.sk89q.worldedit.interpolation.Node; import com.sk89q.worldedit.interpolation.Node;
import com.sk89q.worldedit.masks.*; import com.sk89q.worldedit.masks.Mask;
import com.sk89q.worldedit.math.noise.RandomNoise;
import com.sk89q.worldedit.patterns.Pattern; import com.sk89q.worldedit.patterns.Pattern;
import com.sk89q.worldedit.patterns.SingleBlockPattern; import com.sk89q.worldedit.patterns.SingleBlockPattern;
import com.sk89q.worldedit.regions.*; import com.sk89q.worldedit.regions.*;
@ -51,7 +53,6 @@ import com.sk89q.worldedit.shape.ArbitraryShape;
import com.sk89q.worldedit.shape.RegionShape; import com.sk89q.worldedit.shape.RegionShape;
import com.sk89q.worldedit.shape.WorldEditExpressionEnvironment; import com.sk89q.worldedit.shape.WorldEditExpressionEnvironment;
import com.sk89q.worldedit.util.TreeGenerator; import com.sk89q.worldedit.util.TreeGenerator;
import com.sk89q.worldedit.math.noise.RandomNoise;
import java.util.*; import java.util.*;
@ -559,9 +560,9 @@ public class EditSession implements Extent {
* @return the number of blocks that matched the pattern * @return the number of blocks that matched the pattern
*/ */
public int countBlocks(Region region, Set<BaseBlock> searchBlocks) { public int countBlocks(Region region, Set<BaseBlock> searchBlocks) {
FuzzyBlockMask mask = new FuzzyBlockMask(searchBlocks); FuzzyBlockMask mask = new FuzzyBlockMask(this, searchBlocks);
BlockCount count = new BlockCount(); BlockCount count = new BlockCount();
RegionMaskingFilter filter = new RegionMaskingFilter(this, mask, count); RegionMaskingFilter filter = new RegionMaskingFilter(mask, count);
RegionVisitor visitor = new RegionVisitor(region, filter); RegionVisitor visitor = new RegionVisitor(region, filter);
OperationHelper.completeBlindly(visitor); // We can't throw exceptions, nor do we expect any OperationHelper.completeBlindly(visitor); // We can't throw exceptions, nor do we expect any
return count.getCount(); return count.getCount();
@ -819,10 +820,10 @@ public class EditSession implements Extent {
checkArgument(radius >= 0, "radius >= 0"); checkArgument(radius >= 0, "radius >= 0");
checkArgument(depth >= 1, "depth >= 1"); checkArgument(depth >= 1, "depth >= 1");
CombinedMask mask = new CombinedMask( MaskIntersection mask = new MaskIntersection(
new RegionMask(new EllipsoidRegion(null, origin, new Vector(radius, radius, radius))), new RegionMask(new EllipsoidRegion(null, origin, new Vector(radius, radius, radius))),
new BoundedYMask(origin.getBlockY() - depth + 1, origin.getBlockY()), new BoundedHeightMask(origin.getBlockY() - depth + 1, origin.getBlockY()),
new InvertedMask(new ExistingBlockMask())); Masks.negate(new ExistingBlockMask(this)));
// Want to replace blocks // Want to replace blocks
BlockReplace replace = new BlockReplace(this, pattern); BlockReplace replace = new BlockReplace(this, pattern);
@ -901,7 +902,7 @@ public class EditSession implements Extent {
checkNotNull(position); checkNotNull(position);
checkArgument(apothem >= 1, "apothem >= 1"); checkArgument(apothem >= 1, "apothem >= 1");
Mask mask = new FuzzyBlockMask(new BaseBlock(blockType, -1)); Mask mask = new com.sk89q.worldedit.masks.FuzzyBlockMask(new BaseBlock(blockType, -1));
Vector adjustment = new Vector(1, 1, 1).multiply(apothem - 1); Vector adjustment = new Vector(1, 1, 1).multiply(apothem - 1);
Region region = new CuboidRegion( Region region = new CuboidRegion(
getWorld(), // Causes clamping of Y range getWorld(), // Causes clamping of Y range
@ -966,7 +967,7 @@ public class EditSession implements Extent {
* @throws MaxChangedBlocksException thrown if too many blocks are changed * @throws MaxChangedBlocksException thrown if too many blocks are changed
*/ */
public int replaceBlocks(Region region, Set<BaseBlock> filter, Pattern pattern) throws MaxChangedBlocksException { public int replaceBlocks(Region region, Set<BaseBlock> filter, Pattern pattern) throws MaxChangedBlocksException {
Mask mask = filter == null ? new ExistingBlockMask() : new FuzzyBlockMask(filter); Mask mask = filter == null ? new com.sk89q.worldedit.masks.ExistingBlockMask() : new com.sk89q.worldedit.masks.FuzzyBlockMask(filter);
return replaceBlocks(region, mask, pattern); return replaceBlocks(region, mask, pattern);
} }
@ -986,7 +987,7 @@ public class EditSession implements Extent {
checkNotNull(pattern); checkNotNull(pattern);
BlockReplace replace = new BlockReplace(this, pattern); BlockReplace replace = new BlockReplace(this, pattern);
RegionMaskingFilter filter = new RegionMaskingFilter(this, mask, replace); RegionMaskingFilter filter = new RegionMaskingFilter(Masks.wrap(this, mask), replace);
RegionVisitor visitor = new RegionVisitor(region, filter); RegionVisitor visitor = new RegionVisitor(region, filter);
OperationHelper.completeLegacy(visitor); OperationHelper.completeLegacy(visitor);
return visitor.getAffected(); return visitor.getAffected();

View File

@ -35,7 +35,7 @@ import com.sk89q.worldedit.function.generator.ForestGenerator;
import com.sk89q.worldedit.function.operation.OperationHelper; import com.sk89q.worldedit.function.operation.OperationHelper;
import com.sk89q.worldedit.function.visitor.LayerVisitor; import com.sk89q.worldedit.function.visitor.LayerVisitor;
import com.sk89q.worldedit.masks.Mask; import com.sk89q.worldedit.masks.Mask;
import com.sk89q.worldedit.masks.NoiseFilter2D; import com.sk89q.worldedit.function.mask.NoiseFilter2D;
import com.sk89q.worldedit.patterns.Pattern; import com.sk89q.worldedit.patterns.Pattern;
import com.sk89q.worldedit.patterns.SingleBlockPattern; import com.sk89q.worldedit.patterns.SingleBlockPattern;
import com.sk89q.worldedit.regions.ConvexPolyhedralRegion; import com.sk89q.worldedit.regions.ConvexPolyhedralRegion;

View File

@ -19,10 +19,9 @@
package com.sk89q.worldedit.function; package com.sk89q.worldedit.function;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.Vector2D; import com.sk89q.worldedit.Vector2D;
import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.masks.Mask2D; import com.sk89q.worldedit.function.mask.Mask2D;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
@ -33,30 +32,26 @@ import static com.google.common.base.Preconditions.checkNotNull;
*/ */
public class FlatRegionMaskingFilter implements FlatRegionFunction { public class FlatRegionMaskingFilter implements FlatRegionFunction {
private final EditSession editSession;
private final FlatRegionFunction function; private final FlatRegionFunction function;
private Mask2D mask; private Mask2D mask;
/** /**
* Create a new masking filter. * Create a new masking filter.
* *
* @param editSession the edit session
* @param mask the mask * @param mask the mask
* @param function the delegate function to call * @param function the delegate function to call
*/ */
public FlatRegionMaskingFilter(EditSession editSession, Mask2D mask, FlatRegionFunction function) { public FlatRegionMaskingFilter(Mask2D mask, FlatRegionFunction function) {
checkNotNull(function); checkNotNull(function);
checkNotNull(editSession);
checkNotNull(mask); checkNotNull(mask);
this.editSession = editSession;
this.mask = mask; this.mask = mask;
this.function = function; this.function = function;
} }
@Override @Override
public boolean apply(Vector2D position) throws WorldEditException { public boolean apply(Vector2D position) throws WorldEditException {
return mask.matches(editSession, position) && function.apply(position); return mask.test(position) && function.apply(position);
} }
} }

View File

@ -19,10 +19,9 @@
package com.sk89q.worldedit.function; package com.sk89q.worldedit.function;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.masks.Mask; import com.sk89q.worldedit.function.mask.Mask;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
@ -33,30 +32,25 @@ import static com.google.common.base.Preconditions.checkNotNull;
*/ */
public class RegionMaskingFilter implements RegionFunction { public class RegionMaskingFilter implements RegionFunction {
private final EditSession editSession;
private final RegionFunction function; private final RegionFunction function;
private Mask mask; private Mask mask;
/** /**
* Create a new masking filter. * Create a new masking filter.
* *
* @param editSession the edit session
* @param mask the mask * @param mask the mask
* @param function the function * @param function the function
*/ */
public RegionMaskingFilter(EditSession editSession, Mask mask, RegionFunction function) { public RegionMaskingFilter(Mask mask, RegionFunction function) {
checkNotNull(function); checkNotNull(function);
checkNotNull(editSession);
checkNotNull(mask); checkNotNull(mask);
this.editSession = editSession;
this.mask = mask; this.mask = mask;
this.function = function; this.function = function;
} }
@Override @Override
public boolean apply(Vector position) throws WorldEditException { public boolean apply(Vector position) throws WorldEditException {
return mask.matches(editSession, position) && function.apply(position); return mask.test(position) && function.apply(position);
} }
} }

View File

@ -0,0 +1,61 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* 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
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.function.mask;
import com.sk89q.worldedit.Extent;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* An abstract implementation of {@link Mask} that takes uses an {@link Extent}.
*/
public abstract class AbstractExtentMask extends AbstractMask {
private Extent extent;
/**
* Construct a new mask.
*
* @param extent the extent
*/
protected AbstractExtentMask(Extent extent) {
setExtent(extent);
}
/**
* Get the extent.
*
* @return the extent
*/
public Extent getExtent() {
return extent;
}
/**
* Set the extent.
*
* @param extent the extent
*/
public void setExtent(Extent extent) {
checkNotNull(extent);
this.extent = extent;
}
}

View File

@ -17,23 +17,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package com.sk89q.worldedit.masks; package com.sk89q.worldedit.function.mask;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.Vector2D;
/** /**
* A 2-dimensional mask, similar to {@link com.sk89q.worldedit.masks.Mask}. * A base class of {@link Mask} that all masks should inherit from.
*/ */
public interface Mask2D { public abstract class AbstractMask implements Mask {
/**
* Return whether the given position is matched by this mask.
*
* @param editSession an edit session
* @param position a mask
* @return true if there is a match
*/
boolean matches(EditSession editSession, Vector2D position);
} }

View File

@ -17,10 +17,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package com.sk89q.worldedit.masks; package com.sk89q.worldedit.function.mask;
/** /**
* An abstract implementaiton of {@link com.sk89q.worldedit.masks.Mask2D}. * A base class of {@link Mask2D} that all masks should inherit from.
*/ */
public abstract class AbstractMask2D implements Mask2D { public abstract class AbstractMask2D implements Mask2D {
} }

View File

@ -0,0 +1,99 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* 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
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.function.mask;
import com.sk89q.worldedit.Extent;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BaseBlock;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* A mask that checks whether blocks at the given positions are listed
* in a list of block types.
* </p>
* This mask checks for both an exact block ID and data value match, as well
* for a block with the same ID but a data value of -1.
*/
public class BlockMask extends AbstractExtentMask {
private final Set<BaseBlock> blocks = new HashSet<BaseBlock>();
/**
* Create a new block mask.
*
* @param extent the extent
* @param blocks a list of blocks to match
*/
public BlockMask(Extent extent, Collection<BaseBlock> blocks) {
super(extent);
checkNotNull(blocks);
blocks.addAll(blocks);
}
/**
* Create a new block mask.
*
* @param extent the extent
* @param block an array of blocks to match
*/
public BlockMask(Extent extent, BaseBlock... block) {
this(extent, Arrays.asList(checkNotNull(block)));
}
/**
* Add the given blocks to the list of criteria.
*
* @param blocks a list of blocks
*/
public void add(Collection<BaseBlock> blocks) {
checkNotNull(blocks);
blocks.addAll(blocks);
}
/**
* Add the given blocks to the list of criteria.
*
* @param block an array of blocks
*/
public void add(BaseBlock... block) {
add(Arrays.asList(checkNotNull(block)));
}
/**
* Get the list of blocks that are tested with.
*
* @return a list of blocks
*/
public Collection<BaseBlock> getBlocks() {
return blocks;
}
@Override
public boolean test(Vector vector) {
BaseBlock block = getExtent().getBlock(vector);
return blocks.contains(block) || blocks.contains(new BaseBlock(block.getType(), -1));
}
}

View File

@ -17,31 +17,36 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package com.sk89q.worldedit.masks; package com.sk89q.worldedit.function.mask;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import static com.google.common.base.Preconditions.checkArgument;
/** /**
* Fails all tests against locations outside a range of Y values. * Has the criteria where the Y value of passed positions must be within
* a certain range of Y values (inclusive).
*/ */
public class BoundedYMask extends AbstractMask { public class BoundedHeightMask extends AbstractMask {
private final int minY; private final int minY;
private final int maxY; private final int maxY;
public BoundedYMask(int minY, int maxY) { /**
if (minY > maxY) { * Create a new bounded height mask.
throw new IllegalArgumentException("minY must be less than or equal to maxY"); *
} * @param minY the minimum Y
* @param maxY the maximum Y (must be equal to or greater than minY)
*/
public BoundedHeightMask(int minY, int maxY) {
checkArgument(minY <= maxY, "minY <= maxY required");
this.minY = minY; this.minY = minY;
this.maxY = maxY; this.maxY = maxY;
} }
@Override @Override
public boolean matches(EditSession editSession, Vector pos) { public boolean test(Vector vector) {
int y = pos.getBlockY(); return vector.getY() >= minY && vector.getY() <= maxY;
return y >= minY && y <= maxY;
} }
} }

View File

@ -0,0 +1,46 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* 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
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.function.mask;
import com.sk89q.worldedit.Extent;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BlockID;
/**
* A mask that returns true whenever the block at the location is not
* an air block (it contains some other block).
*/
public class ExistingBlockMask extends AbstractExtentMask {
/**
* Create a new existing block map.
*
* @param extent the extent to check
*/
public ExistingBlockMask(Extent extent) {
super(extent);
}
@Override
public boolean test(Vector vector) {
return getExtent().getBlockType(vector) != BlockID.AIR;
}
}

View File

@ -0,0 +1,45 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* 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
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.function.mask;
import com.sk89q.worldedit.Extent;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BaseBlock;
import java.util.Collection;
public class FuzzyBlockMask extends BlockMask {
public FuzzyBlockMask(Extent extent, Collection<BaseBlock> blocks) {
super(extent, blocks);
}
public FuzzyBlockMask(Extent extent, BaseBlock... block) {
super(extent, block);
}
@Override
public boolean test(Vector vector) {
Extent extent = getExtent();
Collection<BaseBlock> blocks = getBlocks();
BaseBlock compare = new BaseBlock(extent.getBlockType(vector), extent.getBlockData(vector));
return BaseBlock.containsFuzzy(blocks, compare);
}
}

View File

@ -17,19 +17,21 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package com.sk89q.worldedit.masks; package com.sk89q.worldedit.function.mask;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
/** /**
* A mask that only returns <code>true</code>. * Tests whether a given vector meets a criteria.
*/ */
public class DummyMask extends AbstractMask { public interface Mask {
@Override /**
public boolean matches(EditSession editSession, Vector pos) { * Returns true if the criteria is met.
return true; *
} * @param vector the vector to test
* @return true if the criteria is met
*/
boolean test(Vector vector);
} }

View File

@ -17,19 +17,21 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package com.sk89q.worldedit.masks; package com.sk89q.worldedit.function.mask;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.Vector2D; import com.sk89q.worldedit.Vector2D;
/** /**
* A mask that only returns <code>true</code>. * Tests whether a given vector meets a criteria.
*/ */
public class DummyMask2D extends AbstractMask2D { public interface Mask2D {
@Override /**
public boolean matches(EditSession editSession, Vector2D position) { * Returns true if the criteria is met.
return true; *
} * @param vector the vector to test
* @return true if the criteria is met
*/
boolean test(Vector2D vector);
} }

View File

@ -0,0 +1,102 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* 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
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.function.mask;
import com.sk89q.worldedit.Vector;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Combines several masks and requires that all masks return true
* when a certain position is tested. It serves as a logical AND operation
* on a list of masks.
*/
public class MaskIntersection extends AbstractMask {
private final Set<Mask> masks = new HashSet<Mask>();
/**
* Create a new intersection.
*
* @param masks a list of masks
*/
public MaskIntersection(Collection<Mask> masks) {
checkNotNull(masks);
masks.addAll(masks);
}
/**
* Create a new intersection.
*
* @param mask a list of masks
*/
public MaskIntersection(Mask... mask) {
this(Arrays.asList(checkNotNull(mask)));
}
/**
* Add some masks to the list.
*
* @param masks the masks
*/
public void add(Collection<Mask> masks) {
checkNotNull(masks);
masks.addAll(masks);
}
/**
* Add some masks to the list.
*
* @param mask the masks
*/
public void add(Mask... mask) {
add(Arrays.asList(checkNotNull(mask)));
}
/**
* Get the masks that are tested with.
*
* @return the masks
*/
public Collection<Mask> getMasks() {
return masks;
}
@Override
public boolean test(Vector vector) {
if (masks.size() == 0) {
return false;
}
for (Mask mask : masks) {
if (!mask.test(vector)) {
return false;
}
}
return true;
}
}

View File

@ -0,0 +1,64 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* 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
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.function.mask;
import com.sk89q.worldedit.Vector;
import java.util.Collection;
/**
* Combines several masks and requires that one or more masks return true
* when a certain position is tested. It serves as a logical OR operation
* on a list of masks.
*/
public class MaskUnion extends MaskIntersection {
/**
* Create a new union.
*
* @param masks a list of masks
*/
public MaskUnion(Collection<Mask> masks) {
super(masks);
}
/**
* Create a new union.
*
* @param mask a list of masks
*/
public MaskUnion(Mask... mask) {
super(mask);
}
@Override
public boolean test(Vector vector) {
Collection<Mask> masks = getMasks();
for (Mask mask : masks) {
if (mask.test(vector)) {
return true;
}
}
return false;
}
}

View File

@ -0,0 +1,113 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* 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
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.function.mask;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.Vector2D;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Various utility functions related to {@link Mask} and {@link Mask2D}.
*/
public final class Masks {
private Masks() {
}
/**
* Return a 3D mask that always returns true;
*
* @return a mask
*/
public static Mask alwaysTrue() {
return new AbstractMask() {
@Override
public boolean test(Vector vector) {
return true;
}
};
}
/**
* Return a 2D mask that always returns true;
*
* @return a mask
*/
public static Mask2D alwaysTrue2D() {
return new AbstractMask2D() {
@Override
public boolean test(Vector2D vector) {
return true;
}
};
}
/**
* Negate the given mask.
*
* @param mask the mask
* @return a new mask
*/
public static Mask negate(final Mask mask) {
checkNotNull(mask);
return new AbstractMask() {
@Override
public boolean test(Vector vector) {
return !mask.test(vector);
}
};
}
/**
* Negate the given mask.
*
* @param mask the mask
* @return a new mask
*/
public static Mask2D negate(final Mask2D mask) {
checkNotNull(mask);
return new AbstractMask2D() {
@Override
public boolean test(Vector2D vector) {
return !mask.test(vector);
}
};
}
/**
* Wrap an old-style mask and convert it to a new mask.
*
* @param editSession the edit session to bind to
* @param mask the old-style mask
* @return a new-style mask
*/
public static Mask wrap(final EditSession editSession, final com.sk89q.worldedit.masks.Mask mask) {
checkNotNull(mask);
return new AbstractMask() {
@Override
public boolean test(Vector vector) {
return mask.matches(editSession, vector);
}
};
}
}

View File

@ -17,9 +17,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package com.sk89q.worldedit.masks; package com.sk89q.worldedit.function.mask;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.math.noise.NoiseGenerator; import com.sk89q.worldedit.math.noise.NoiseGenerator;
@ -86,8 +85,8 @@ public class NoiseFilter extends AbstractMask {
} }
@Override @Override
public boolean matches(EditSession editSession, Vector pos) { public boolean test(Vector vector) {
return noiseGenerator.noise(pos) <= density; return noiseGenerator.noise(vector) <= density;
} }
} }

View File

@ -17,9 +17,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package com.sk89q.worldedit.masks; package com.sk89q.worldedit.function.mask;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.Vector2D; import com.sk89q.worldedit.Vector2D;
import com.sk89q.worldedit.math.noise.NoiseGenerator; import com.sk89q.worldedit.math.noise.NoiseGenerator;
@ -86,7 +85,7 @@ public class NoiseFilter2D extends AbstractMask2D {
} }
@Override @Override
public boolean matches(EditSession editSession, Vector2D pos) { public boolean test(Vector2D pos) {
return noiseGenerator.noise(pos) <= density; return noiseGenerator.noise(pos) <= density;
} }

View File

@ -0,0 +1,67 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* 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
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.function.mask;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.regions.Region;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* A mask that tests whether given positions are contained within a region.
*/
public class RegionMask extends AbstractMask {
private Region region;
/**
* Create a new region mask.
*
* @param region the region
*/
public RegionMask(Region region) {
setRegion(region);
}
/**
* Get the region.
*
* @return the region
*/
public Region getRegion() {
return region;
}
/**
* Set the region that positions must be contained within.
*
* @param region the region
*/
public void setRegion(Region region) {
checkNotNull(region);
this.region = region;
}
@Override
public boolean test(Vector vector) {
return region.contains(vector);
}
}

View File

@ -0,0 +1,38 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* 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
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.function.mask;
import com.sk89q.worldedit.Extent;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BlockType;
public class SolidBlockMask extends AbstractExtentMask {
public SolidBlockMask(Extent extent) {
super(extent);
}
@Override
public boolean test(Vector vector) {
Extent extent = getExtent();
return !BlockType.canPassThrough(extent.getBlockType(vector), extent.getBlockData(vector));
}
}

View File

@ -21,8 +21,8 @@ package com.sk89q.worldedit.function.visitor;
import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.masks.Mask;
import com.sk89q.worldedit.function.RegionFunction; import com.sk89q.worldedit.function.RegionFunction;
import com.sk89q.worldedit.function.mask.Mask;
import java.util.List; import java.util.List;

View File

@ -24,9 +24,9 @@ import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.Vector2D; import com.sk89q.worldedit.Vector2D;
import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.function.LayerFunction; import com.sk89q.worldedit.function.LayerFunction;
import com.sk89q.worldedit.function.mask.Mask2D;
import com.sk89q.worldedit.function.mask.Masks;
import com.sk89q.worldedit.function.operation.Operation; import com.sk89q.worldedit.function.operation.Operation;
import com.sk89q.worldedit.masks.DummyMask2D;
import com.sk89q.worldedit.masks.Mask2D;
import com.sk89q.worldedit.regions.FlatRegion; import com.sk89q.worldedit.regions.FlatRegion;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
@ -45,7 +45,7 @@ public class LayerVisitor implements Operation {
private final EditSession editSession; private final EditSession editSession;
private final FlatRegion flatRegion; private final FlatRegion flatRegion;
private final LayerFunction function; private final LayerFunction function;
private Mask2D mask = new DummyMask2D(); private Mask2D mask = Masks.alwaysTrue2D();
private int minY; private int minY;
private int maxY; private int maxY;
@ -94,7 +94,7 @@ public class LayerVisitor implements Operation {
@Override @Override
public Operation resume() throws WorldEditException { public Operation resume() throws WorldEditException {
for (Vector2D column : flatRegion.asFlatRegion()) { for (Vector2D column : flatRegion.asFlatRegion()) {
if (!mask.matches(editSession, column)) { if (!mask.test(column)) {
continue; continue;
} }

View File

@ -21,7 +21,7 @@ package com.sk89q.worldedit.function.visitor;
import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.masks.Mask; import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.function.RegionFunction; import com.sk89q.worldedit.function.RegionFunction;
/** /**
@ -45,6 +45,6 @@ public class RecursiveVisitor extends BreadthFirstSearch {
if (y < 0 || y > editSession.getWorld().getMaxY()) { if (y < 0 || y > editSession.getWorld().getMaxY()) {
return false; return false;
} }
return mask.matches(editSession, to); return mask.test(to);
} }
} }

View File

@ -4,8 +4,13 @@ import com.sk89q.worldedit.LocalPlayer;
import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
/**
* @deprecated Switch to {@link com.sk89q.worldedit.function.mask.AbstractMask}
*/
@Deprecated
public abstract class AbstractMask implements Mask { public abstract class AbstractMask implements Mask {
@Override @Override
public void prepare(LocalSession session, LocalPlayer player, Vector target) { public void prepare(LocalSession session, LocalPlayer player, Vector target) {
} }
} }

View File

@ -1,7 +1,6 @@
package com.sk89q.worldedit.masks; package com.sk89q.worldedit.masks;
import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.Extent;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BaseBlock;
@ -9,7 +8,11 @@ import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
public class BlockMask extends ExtentAwareMask { /**
* @deprecated Use {@link com.sk89q.worldedit.function.mask.BlockMask}
*/
@Deprecated
public class BlockMask extends AbstractMask {
private final Set<BaseBlock> blocks; private final Set<BaseBlock> blocks;
@ -43,8 +46,7 @@ public class BlockMask extends ExtentAwareMask {
@Override @Override
public boolean matches(EditSession editSession, Vector pos) { public boolean matches(EditSession editSession, Vector pos) {
Extent extent = getExtent(editSession); BaseBlock block = editSession.getBlock(pos);
BaseBlock block = extent.getBlock(pos);
return blocks.contains(block) return blocks.contains(block)
|| blocks.contains(new BaseBlock(block.getType(), -1)); || blocks.contains(new BaseBlock(block.getType(), -1));
} }

View File

@ -23,10 +23,15 @@ import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.LocalPlayer; import com.sk89q.worldedit.LocalPlayer;
import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.function.mask.MaskIntersection;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
/**
* @deprecated See {@link MaskIntersection}
*/
@Deprecated
public class CombinedMask extends AbstractMask { public class CombinedMask extends AbstractMask {
private final List<Mask> masks = new ArrayList<Mask>(); private final List<Mask> masks = new ArrayList<Mask>();

View File

@ -7,7 +7,7 @@ import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.regions.Region;
public class DynamicRegionMask implements Mask { public class DynamicRegionMask extends AbstractMask {
private Region region; private Region region;
@Override @Override

View File

@ -23,9 +23,13 @@ import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BlockID; import com.sk89q.worldedit.blocks.BlockID;
public class ExistingBlockMask extends ExtentAwareMask { /**
* @deprecated See {@link com.sk89q.worldedit.function.mask.ExistingBlockMask}
*/
@Deprecated
public class ExistingBlockMask extends AbstractMask {
@Override @Override
public boolean matches(EditSession editSession, Vector pos) { public boolean matches(EditSession editSession, Vector pos) {
return getExtent(editSession).getBlockType(pos) != BlockID.AIR; return editSession.getBlockType(pos) != BlockID.AIR;
} }
} }

View File

@ -1,71 +0,0 @@
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* 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
* (at your option) any later version.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.masks;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.Extent;
/**
* Extended by masks to make them potentially use an {@link Extent} rather than
* the {@link EditSession}.
* </p>
* At the moment, masks are coupled to {@link EditSession} when they should
* not be. However, because a change to {@link Mask} would cause massive breakage in
* the API, that change is deferred until further notice and this class exists as
* an opt-in mixin for adding support for {@link Extent}s.
*/
public abstract class ExtentAwareMask extends AbstractMask {
private Extent extent;
/**
* Get the extent that will be used for lookups.
*
* @return the extent, or null if the {@link EditSession} is to be used
*/
public Extent getExtent() {
return extent;
}
/**
* Set the extent that will be used for lookups.
*
* @param extent the extent, or null if the {@link EditSession} is to be used
*/
public void setExtent(Extent extent) {
this.extent = extent;
}
/**
* Get the extent to use for operations. Subclasses should call this method
* rather than access the passed {@link EditSession} directly.
*
* @param editSession the passed in {@link EditSession}
* @return an extent
*/
protected Extent getExtent(EditSession editSession) {
if (extent != null) {
return extent;
} else {
return editSession;
}
}
}

View File

@ -20,7 +20,6 @@
package com.sk89q.worldedit.masks; package com.sk89q.worldedit.masks;
import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.Extent;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BaseBlock;
@ -28,10 +27,10 @@ import java.util.HashSet;
import java.util.Set; import java.util.Set;
/** /**
* Uses {@link BaseBlock#containsFuzzy(java.util.Collection, BaseBlock)} to * @deprecated See {@link com.sk89q.worldedit.function.mask.FuzzyBlockMask}
* match blocks.
*/ */
public class FuzzyBlockMask extends ExtentAwareMask { @Deprecated
public class FuzzyBlockMask extends AbstractMask {
private final Set<BaseBlock> filter; private final Set<BaseBlock> filter;
@ -59,8 +58,7 @@ public class FuzzyBlockMask extends ExtentAwareMask {
@Override @Override
public boolean matches(EditSession editSession, Vector pos) { public boolean matches(EditSession editSession, Vector pos) {
Extent extent = getExtent(editSession); BaseBlock compare = new BaseBlock(editSession.getBlockType(pos), editSession.getBlockData(pos));
BaseBlock compare = new BaseBlock(extent.getBlockType(pos), extent.getBlockData(pos));
return BaseBlock.containsFuzzy(filter, compare); return BaseBlock.containsFuzzy(filter, compare);
} }
} }

View File

@ -4,7 +4,12 @@ import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.LocalPlayer; import com.sk89q.worldedit.LocalPlayer;
import com.sk89q.worldedit.LocalSession; import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.function.mask.Masks;
/**
* @deprecated See {@link Masks#negate(com.sk89q.worldedit.function.mask.Mask)}
*/
@Deprecated
public class InvertedMask extends AbstractMask { public class InvertedMask extends AbstractMask {
private final Mask mask; private final Mask mask;

View File

@ -25,13 +25,9 @@ import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
/** /**
* Base matcher for the block filtering framework. Implementing classes * @deprecated Use {@link com.sk89q.worldedit.function.mask.Mask}
* can be used to filter blocks to set or replace.
* <p>
* <u>Do NOT</u> implement this interface. Extend {@link AbstractMask} instead.
*
* @author sk89q
*/ */
@Deprecated
public interface Mask { public interface Mask {
/** /**

View File

@ -2,7 +2,12 @@ package com.sk89q.worldedit.masks;
import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.function.mask.NoiseFilter;
/**
* @deprecated See {@link NoiseFilter}
*/
@Deprecated
public class RandomMask extends AbstractMask { public class RandomMask extends AbstractMask {
private final double ratio; private final double ratio;

View File

@ -23,6 +23,10 @@ import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.regions.Region; import com.sk89q.worldedit.regions.Region;
/**
* @deprecated See {@link com.sk89q.worldedit.function.mask.RegionMask}
*/
@Deprecated
public class RegionMask extends AbstractMask { public class RegionMask extends AbstractMask {
private final Region region; private final Region region;

View File

@ -1,17 +1,16 @@
package com.sk89q.worldedit.masks; package com.sk89q.worldedit.masks;
import com.sk89q.worldedit.EditSession; import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.Extent;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BlockType; import com.sk89q.worldedit.blocks.BlockType;
/** /**
* Works like {@link ExistingBlockMask}, except also dealing with non-solid non-air blocks the same way as with air. * @deprecated See {@link com.sk89q.worldedit.function.mask.SolidBlockMask}
*/ */
public class SolidBlockMask extends ExtentAwareMask { @Deprecated
public class SolidBlockMask extends AbstractMask {
@Override @Override
public boolean matches(EditSession editSession, Vector pos) { public boolean matches(EditSession editSession, Vector pos) {
Extent extent = getExtent(editSession); return !BlockType.canPassThrough(editSession.getBlockType(pos), editSession.getBlockData(pos));
return !BlockType.canPassThrough(extent.getBlockType(pos), extent.getBlockData(pos));
} }
} }