General code cleanup.

This commit is contained in:
sk89q 2014-03-31 20:29:05 -07:00
parent 8bec48dc9e
commit 761904e496
16 changed files with 97 additions and 43 deletions

View File

@ -27,10 +27,10 @@ import com.sk89q.worldedit.expression.Expression;
import com.sk89q.worldedit.expression.ExpressionException; import com.sk89q.worldedit.expression.ExpressionException;
import com.sk89q.worldedit.expression.runtime.RValue; import com.sk89q.worldedit.expression.runtime.RValue;
import com.sk89q.worldedit.extent.*; import com.sk89q.worldedit.extent.*;
import com.sk89q.worldedit.extent.reorder.SimpleBlockReorder; import com.sk89q.worldedit.extent.reorder.MultiStageReorder;
import com.sk89q.worldedit.function.GroundFunction; import com.sk89q.worldedit.function.GroundFunction;
import com.sk89q.worldedit.function.RegionMaskingFilter; import com.sk89q.worldedit.function.RegionMaskingFilter;
import com.sk89q.worldedit.function.block.BlockCount; import com.sk89q.worldedit.function.block.Counter;
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;
@ -93,7 +93,7 @@ public class EditSession implements Extent {
private final FastModeExtent fastModeExtent; private final FastModeExtent fastModeExtent;
private final BlockBagExtent blockBagExtent; private final BlockBagExtent blockBagExtent;
private final SimpleBlockReorder reorderExtent; private final MultiStageReorder reorderExtent;
private final MaskingExtent maskingExtent; private final MaskingExtent maskingExtent;
private final BlockChangeLimiter changeLimiter; private final BlockChangeLimiter changeLimiter;
@ -136,7 +136,7 @@ public class EditSession implements Extent {
blockBagExtent = new BlockBagExtent(validator, world, blockBag); blockBagExtent = new BlockBagExtent(validator, world, blockBag);
// This extent can be skipped by calling rawSetBlock() // This extent can be skipped by calling rawSetBlock()
reorderExtent = new SimpleBlockReorder(blockBagExtent, false); reorderExtent = new MultiStageReorder(blockBagExtent, false);
// These extents can be skipped by calling smartSetBlock() // These extents can be skipped by calling smartSetBlock()
ChangeSetExtent changeSetExtent = new ChangeSetExtent(reorderExtent, changeSet); ChangeSetExtent changeSetExtent = new ChangeSetExtent(reorderExtent, changeSet);
@ -581,7 +581,7 @@ public class EditSession implements Extent {
*/ */
public int countBlocks(Region region, Set<BaseBlock> searchBlocks) { public int countBlocks(Region region, Set<BaseBlock> searchBlocks) {
FuzzyBlockMask mask = new FuzzyBlockMask(this, searchBlocks); FuzzyBlockMask mask = new FuzzyBlockMask(this, searchBlocks);
BlockCount count = new BlockCount(); Counter count = new Counter();
RegionMaskingFilter filter = new RegionMaskingFilter(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
@ -983,7 +983,7 @@ public class EditSession implements Extent {
BlockReplace replace = new BlockReplace(this, Patterns.wrap(pattern)); BlockReplace replace = new BlockReplace(this, Patterns.wrap(pattern));
RegionOffset offset = new RegionOffset(new Vector(0, 1, 0), replace); RegionOffset offset = new RegionOffset(new Vector(0, 1, 0), replace);
GroundFunction ground = new GroundFunction(this, offset); GroundFunction ground = new GroundFunction(new ExistingBlockMask(this), offset);
LayerVisitor visitor = new LayerVisitor(asFlatRegion(region), minimumBlockY(region), maximumBlockY(region), ground); LayerVisitor visitor = new LayerVisitor(asFlatRegion(region), minimumBlockY(region), maximumBlockY(region), ground);
OperationHelper.completeLegacy(visitor); OperationHelper.completeLegacy(visitor);
return ground.getAffected(); return ground.getAffected();
@ -1634,7 +1634,7 @@ public class EditSession implements Extent {
position.add(apothem, 10, apothem)); position.add(apothem, 10, apothem));
double density = 0.02; double density = 0.02;
GroundFunction ground = new GroundFunction(this, generator); GroundFunction ground = new GroundFunction(new ExistingBlockMask(this), generator);
LayerVisitor visitor = new LayerVisitor(region, minimumBlockY(region), maximumBlockY(region), ground); LayerVisitor visitor = new LayerVisitor(region, minimumBlockY(region), maximumBlockY(region), ground);
visitor.setMask(new NoiseFilter2D(new RandomNoise(), density)); visitor.setMask(new NoiseFilter2D(new RandomNoise(), density));
OperationHelper.completeLegacy(visitor); OperationHelper.completeLegacy(visitor);

View File

@ -27,6 +27,7 @@ import com.sk89q.worldedit.*;
import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.blocks.BlockID; import com.sk89q.worldedit.blocks.BlockID;
import com.sk89q.worldedit.expression.ExpressionException; import com.sk89q.worldedit.expression.ExpressionException;
import com.sk89q.worldedit.function.mask.ExistingBlockMask;
import com.sk89q.worldedit.math.convolution.GaussianKernel; import com.sk89q.worldedit.math.convolution.GaussianKernel;
import com.sk89q.worldedit.math.convolution.HeightMap; import com.sk89q.worldedit.math.convolution.HeightMap;
import com.sk89q.worldedit.math.convolution.HeightMapFilter; import com.sk89q.worldedit.math.convolution.HeightMapFilter;
@ -551,7 +552,7 @@ public class RegionCommands {
Region region = session.getSelection(player.getWorld()); Region region = session.getSelection(player.getWorld());
ForestGenerator generator = new ForestGenerator(editSession, new TreeGenerator(type)); ForestGenerator generator = new ForestGenerator(editSession, new TreeGenerator(type));
GroundFunction ground = new GroundFunction(editSession, generator); GroundFunction ground = new GroundFunction(new ExistingBlockMask(editSession), generator);
LayerVisitor visitor = new LayerVisitor(asFlatRegion(region), minimumBlockY(region), maximumBlockY(region), ground); LayerVisitor visitor = new LayerVisitor(asFlatRegion(region), minimumBlockY(region), maximumBlockY(region), ground);
visitor.setMask(new NoiseFilter2D(new RandomNoise(), density)); visitor.setMask(new NoiseFilter2D(new RandomNoise(), density));
OperationHelper.completeLegacy(visitor); OperationHelper.completeLegacy(visitor);
@ -573,7 +574,7 @@ public class RegionCommands {
Region region = session.getSelection(player.getWorld()); Region region = session.getSelection(player.getWorld());
FloraGenerator generator = new FloraGenerator(editSession); FloraGenerator generator = new FloraGenerator(editSession);
GroundFunction ground = new GroundFunction(editSession, generator); GroundFunction ground = new GroundFunction(new ExistingBlockMask(editSession), generator);
LayerVisitor visitor = new LayerVisitor(asFlatRegion(region), minimumBlockY(region), maximumBlockY(region), ground); LayerVisitor visitor = new LayerVisitor(asFlatRegion(region), minimumBlockY(region), maximumBlockY(region), ground);
visitor.setMask(new NoiseFilter2D(new RandomNoise(), density)); visitor.setMask(new NoiseFilter2D(new RandomNoise(), density));
OperationHelper.completeLegacy(visitor); OperationHelper.completeLegacy(visitor);

View File

@ -26,6 +26,9 @@ import com.sk89q.worldedit.function.mask.Mask;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
/**
* Requires that all mutating methods pass a given {@link Mask}.
*/
public class MaskingExtent extends ExtentDelegate { public class MaskingExtent extends ExtentDelegate {
private Mask mask; private Mask mask;
@ -63,11 +66,7 @@ public class MaskingExtent extends ExtentDelegate {
@Override @Override
public boolean setBlock(Vector location, BaseBlock block) throws WorldEditException { public boolean setBlock(Vector location, BaseBlock block) throws WorldEditException {
if (mask.test(location)) { return mask.test(location) && super.setBlock(location, block);
return super.setBlock(location, block);
} else {
return false;
}
} }
} }

View File

@ -39,7 +39,7 @@ import java.util.*;
/** /**
* Re-orders blocks into several stages. * Re-orders blocks into several stages.
*/ */
public class SimpleBlockReorder extends ExtentDelegate { public class MultiStageReorder extends ExtentDelegate implements ReorderingExtent {
private TupleArrayList<BlockVector, BaseBlock> stage1 = new TupleArrayList<BlockVector, BaseBlock>(); private TupleArrayList<BlockVector, BaseBlock> stage1 = new TupleArrayList<BlockVector, BaseBlock>();
private TupleArrayList<BlockVector, BaseBlock> stage2 = new TupleArrayList<BlockVector, BaseBlock>(); private TupleArrayList<BlockVector, BaseBlock> stage2 = new TupleArrayList<BlockVector, BaseBlock>();
@ -52,7 +52,7 @@ public class SimpleBlockReorder extends ExtentDelegate {
* @param extent the extent * @param extent the extent
* @param enabled true to enable * @param enabled true to enable
*/ */
public SimpleBlockReorder(Extent extent, boolean enabled) { public MultiStageReorder(Extent extent, boolean enabled) {
super(extent); super(extent);
this.enabled = enabled; this.enabled = enabled;
} }
@ -62,7 +62,7 @@ public class SimpleBlockReorder extends ExtentDelegate {
* *
* @param extent the extent * @param extent the extent
*/ */
public SimpleBlockReorder(Extent extent) { public MultiStageReorder(Extent extent) {
this(extent, true); this(extent, true);
} }

View File

@ -0,0 +1,34 @@
/*
* 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.extent.reorder;
import com.sk89q.worldedit.extent.Extent;
/**
* An interface for {@link Extent}s that are meant to reorder changes so
* that they are more successful.
* </p>
* For example, torches in Minecraft need to be placed on a block. A smart
* reordering implementation might place the torch after the block has
* been placed.
*/
public interface ReorderingExtent extends Extent {
}

View File

@ -21,19 +21,21 @@ package com.sk89q.worldedit.function;
import com.sk89q.worldedit.Vector2D; import com.sk89q.worldedit.Vector2D;
import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.regions.FlatRegion;
/** /**
* Performs a function on points in a flat region. * Performs a function on the columns in a {@link FlatRegion}, or also
* known as vectors with only X and Z components (where Y is height).
*/ */
public interface FlatRegionFunction { public interface FlatRegionFunction {
/** /**
* Apply the function to the given point. * Apply the function to the given position.
* *
* @param pt the point * @param position the position
* @return true if something was changed * @return true if something was changed
* @throws WorldEditException thrown on an error * @throws WorldEditException thrown on an error
*/ */
public boolean apply(Vector2D pt) throws WorldEditException; public boolean apply(Vector2D position) throws WorldEditException;
} }

View File

@ -19,11 +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.ExistingBlockMask; import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.masks.Mask;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
@ -32,21 +30,20 @@ import static com.google.common.base.Preconditions.checkNotNull;
*/ */
public class GroundFunction implements LayerFunction { public class GroundFunction implements LayerFunction {
private final EditSession editSession; private Mask mask;
private final RegionFunction function; private final RegionFunction function;
private Mask mask = new ExistingBlockMask();
private int affected; private int affected;
/** /**
* Create a new ground function. * Create a new ground function.
* *
* @param editSession an edit session * @param mask a mask
* @param function the function to apply * @param function the function to apply
*/ */
public GroundFunction(EditSession editSession, RegionFunction function) { public GroundFunction(Mask mask, RegionFunction function) {
checkNotNull(editSession); checkNotNull(mask);
checkNotNull(function); checkNotNull(function);
this.editSession = editSession; this.mask = mask;
this.function = function; this.function = function;
} }
@ -80,7 +77,7 @@ public class GroundFunction implements LayerFunction {
@Override @Override
public boolean isGround(Vector position) { public boolean isGround(Vector position) {
return mask.matches(editSession, position); return mask.test(position);
} }
@Override @Override

View File

@ -21,9 +21,10 @@ package com.sk89q.worldedit.function;
import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.WorldEditException; import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.function.visitor.LayerVisitor;
/** /**
* A function that accepts layers of blocks. * A function that takes a position and a depth.
*/ */
public interface LayerFunction { public interface LayerFunction {
@ -38,11 +39,14 @@ public interface LayerFunction {
/** /**
* Apply the function to the given position. * Apply the function to the given position.
* </p>
* The depth would be the number of blocks from the surface if
* a {@link LayerVisitor} was used.
* *
* @param position the position * @param position the position
* @param depth the depth as a number starting from 0 * @param depth the depth as a number starting from 0
* @return true whether this method should be called for further layers * @return true whether this method should be called for further layers
* @throws com.sk89q.worldedit.WorldEditException thrown on an error * @throws WorldEditException thrown on an error
*/ */
boolean apply(Vector position, int depth) throws WorldEditException; boolean apply(Vector position, int depth) throws WorldEditException;
} }

View File

@ -28,7 +28,7 @@ import com.sk89q.worldedit.function.pattern.Pattern;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
/** /**
* Replaces blocks with the given pattern. * Replaces blocks with a given pattern.
*/ */
public class BlockReplace implements RegionFunction { public class BlockReplace implements RegionFunction {

View File

@ -24,9 +24,9 @@ import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.function.RegionFunction; import com.sk89q.worldedit.function.RegionFunction;
/** /**
* Counts the number of blocks. * Keeps a count of the number of times that {@link #apply(Vector)} is called.
*/ */
public class BlockCount implements RegionFunction { public class Counter implements RegionFunction {
private int count; private int count;

View File

@ -32,6 +32,9 @@ import com.sk89q.worldedit.function.pattern.RandomPattern;
import java.util.Random; import java.util.Random;
/**
* Generates patches of fruit (i.e. pumpkin patches).
*/
public class GardenPatchGenerator implements RegionFunction { public class GardenPatchGenerator implements RegionFunction {
private final Random random = new Random(); private final Random random = new Random();

View File

@ -31,8 +31,8 @@ import java.util.Set;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
/** /**
* A mask that checks whether blocks at the given positions are listed * A mask that checks whether blocks at the given positions are matched by
* in a list of block types. * a block in a list.
* </p> * </p>
* This mask checks for both an exact block ID and data value match, as well * 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. * for a block with the same ID but a data value of -1.

View File

@ -29,11 +29,21 @@ import java.util.Map;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
/**
* Sets block from an iterator of {@link Map.Entry} containing a
* {@link BlockVector} as the key and a {@link BaseBlock} as the value.
*/
public class BlockMapEntryPlacer implements Operation { public class BlockMapEntryPlacer implements Operation {
private final Extent extent; private final Extent extent;
private final Iterator<Map.Entry<BlockVector, BaseBlock>> iterator; private final Iterator<Map.Entry<BlockVector, BaseBlock>> iterator;
/**
* Create a new instance.
*
* @param extent the extent to set the blocks on
* @param iterator the iterator
*/
public BlockMapEntryPlacer(Extent extent, Iterator<Map.Entry<BlockVector, BaseBlock>> iterator) { public BlockMapEntryPlacer(Extent extent, Iterator<Map.Entry<BlockVector, BaseBlock>> iterator) {
checkNotNull(extent); checkNotNull(extent);
checkNotNull(iterator); checkNotNull(iterator);

View File

@ -28,7 +28,8 @@ import com.sk89q.worldedit.Vector2D;
public interface NoiseGenerator { public interface NoiseGenerator {
/** /**
* Get the noise for the given position. * Get the noise value for the given position. The returned value may
* change on every future call for the same position.
* *
* @param position the position * @param position the position
* @return a noise value between 0 (inclusive) and 1 (inclusive) * @return a noise value between 0 (inclusive) and 1 (inclusive)
@ -36,7 +37,8 @@ public interface NoiseGenerator {
float noise(Vector2D position); float noise(Vector2D position);
/** /**
* Get the noise for the given position. * Get the noise value for the given position. The returned value may
* change on every future call for the same position.
* *
* @param position the position * @param position the position
* @return a noise value between 0 (inclusive) and 1 (inclusive) * @return a noise value between 0 (inclusive) and 1 (inclusive)

View File

@ -25,7 +25,8 @@ import com.sk89q.worldedit.Vector2D;
import java.util.Random; import java.util.Random;
/** /**
* Generates noise non-deterministically using {@link java.util.Random}. * Generates noise using {@link java.util.Random}. Every time a noise
* generating function is called, a new value will be returned.
*/ */
public class RandomNoise implements NoiseGenerator { public class RandomNoise implements NoiseGenerator {

View File

@ -24,8 +24,9 @@ import com.sk89q.worldedit.Vector;
/** /**
* An affine transform. * An affine transform.
* </p> * </p>
* This class is from the <a href="http://geom-java.sourceforge.net/index.html>JavaGeom * This class is from the
* project</a>, which is licensed under LGPL v2.1. * <a href="http://geom-java.sourceforge.net/index.html>JavaGeom project</a>,
* which is licensed under LGPL v2.1.
*/ */
public class AffineTransform implements Transform { public class AffineTransform implements Transform {