Fixes and changes to forest/forestgen.

* Sync up implementations of the two commands.
* Fix generating trees in spots with replaceable blocks.
* Make message when you mistype tree-type arg more correct.

Fixes WORLDEDIT-3869.
This commit is contained in:
wizjany
2019-03-20 21:05:11 -04:00
parent 1934006d14
commit 9b473cecbd
5 changed files with 38 additions and 49 deletions

View File

@ -44,6 +44,7 @@ import com.sk89q.worldedit.function.block.BlockDistributionCounter;
import com.sk89q.worldedit.function.block.BlockReplace;
import com.sk89q.worldedit.function.block.Counter;
import com.sk89q.worldedit.function.block.Naturalizer;
import com.sk89q.worldedit.function.generator.ForestGenerator;
import com.sk89q.worldedit.function.generator.GardenPatchGenerator;
import com.sk89q.worldedit.function.mask.BlockMask;
import com.sk89q.worldedit.function.mask.BlockStateMask;
@ -1832,38 +1833,25 @@ public class EditSession implements Extent, AutoCloseable {
* @throws MaxChangedBlocksException thrown if too many blocks are changed
*/
public int makeForest(BlockVector3 basePosition, int size, double density, TreeGenerator.TreeType treeType) throws MaxChangedBlocksException {
int affected = 0;
return makeForest(CuboidRegion.fromCenter(basePosition, size), density, treeType);
}
for (int x = basePosition.getBlockX() - size; x <= basePosition.getBlockX()
+ size; ++x) {
for (int z = basePosition.getBlockZ() - size; z <= basePosition.getBlockZ()
+ size; ++z) {
// Don't want to be in the ground
if (!getBlock(BlockVector3.at(x, basePosition.getBlockY(), z)).getBlockType().getMaterial().isAir()) {
continue;
}
// The gods don't want a tree here
if (Math.random() >= density) {
continue;
} // def 0.05
for (int y = basePosition.getBlockY(); y >= basePosition.getBlockY() - 10; --y) {
// Check if we hit the ground
BlockType t = getBlock(BlockVector3.at(x, y, z)).getBlockType();
if (t == BlockTypes.GRASS_BLOCK || t == BlockTypes.DIRT) {
treeType.generate(this, BlockVector3.at(x, y + 1, z));
++affected;
break;
} else if (t == BlockTypes.SNOW) {
setBlock(BlockVector3.at(x, y, z), BlockTypes.AIR.getDefaultState());
} else if (!t.getMaterial().isAir()) { // Trees won't grow on this!
break;
}
}
}
}
return affected;
/**
* Makes a forest.
*
* @param region the region to generate trees in
* @param density between 0 and 1, inclusive
* @param treeType the tree type
* @return number of trees created
* @throws MaxChangedBlocksException thrown if too many blocks are changed
*/
public int makeForest(Region region, double density, TreeGenerator.TreeType treeType) throws MaxChangedBlocksException {
ForestGenerator generator = new ForestGenerator(this, treeType);
GroundFunction ground = new GroundFunction(new ExistingBlockMask(this), generator);
LayerVisitor visitor = new LayerVisitor(asFlatRegion(region), minimumBlockY(region), maximumBlockY(region), ground);
visitor.setMask(new NoiseFilter2D(new RandomNoise(), density));
Operations.completeLegacy(visitor);
return ground.getAffected();
}
/**