Tree shapes
These types plan a mod’s trees as plain math a unit test can run, before any block is written,
and draw a shape’s seeds for the mod’s owner to approve. Use them for trees vanilla’s tree
configurations cannot shape; a world feature then writes the planned blocks with
WorldGenBlocks.
At a glance
Section titled “At a glance”| Type | Summary | Status | Since | Side | Thread |
|---|---|---|---|---|---|
Blueprint |
A tree’s logs and leaves, relative to its origin, the sapling’s block. | Experimental | 2.9.0 | both | any |
ShapeKit |
Layers, balls, branches, and fraying that write logs and leaves into a Blueprint. |
Experimental | 2.9.0 | both | any |
ShapeRenderer |
Draws a tree shape’s front and top views over several seeds into one PNG. | Experimental | 2.9.0 | both | any |
Quick start
Section titled “Quick start”With MAPLE_HEIGHT, the tallest maple’s height:
static Blueprint planMaple(RandomSource random) { Blueprint tree = new Blueprint(MAPLE_HEIGHT, 0); int height = 5 + random.nextInt(3); for (int y = 0; y < height; y++) tree.log(0, y, 0); ShapeKit.leafBall(tree, 0, height, 0); ShapeKit.fray(tree, random, 0.2f); return tree;}Blueprint
Section titled “Blueprint”Experimental · Since 2.9.0 · Side: both · Thread: any
Blueprint holds one tree’s logs and leaves before any block is written, so a shape is plain
math a unit test can run without a game. A mod’s world feature plans the tree, then writes it.
In the feature’s place method, with planMaple and MAPLE_HEIGHT as in the
quick start, MAPLE_LOG and MAPLE_LEAVES, the wood’s Registered handles,
and level, origin, and random from the feature’s context:
if (origin.getY() + MAPLE_HEIGHT >= level.getMaxBuildHeight()) return false;Blueprint tree = planMaple(random);Long2IntMap distance = tree.settleLeaves();BlockPos.MutableBlockPos at = new BlockPos.MutableBlockPos();for (Long2ObjectMap.Entry<Direction.Axis> log : tree.logs().long2ObjectEntrySet()) { long k = log.getLongKey(); at.setWithOffset(origin, BlockPos.getX(k), BlockPos.getY(k), BlockPos.getZ(k)); WorldGenBlocks.placeTreeBlock(level, at, MAPLE_LOG.get().defaultBlockState() .setValue(RotatedPillarBlock.AXIS, log.getValue()));}for (long leaf : tree.leaves()) { at.setWithOffset(origin, BlockPos.getX(leaf), BlockPos.getY(leaf), BlockPos.getZ(leaf)); WorldGenBlocks.placeTreeBlock(level, at, MAPLE_LEAVES.get().defaultBlockState() .setValue(LeavesBlock.DISTANCE, distance.get(leaf)));}- A shape’s unit test runs it over thousands of seeds, so a rare variant that leaves the
bounds shows in
Blueprint#outside.
ShapeKit
Section titled “ShapeKit”Experimental · Since 2.9.0 · Side: both · Thread: any
ShapeKit writes the layers, balls, branches, and fraying shapes are built from into a
Blueprint. With tree, random, and height as in the quick start:
for (ShapeKit.Dir direction : ShapeKit.DIRECTIONS.subList(0, 4)) { long end = ShapeKit.walk(tree, random, 0, height - 2, 0, direction, 4, null); ShapeKit.endBall(tree, end);}- A call added to a shape changes every tree it plans from that call on, since each later call draws different numbers from the random source.
ShapeRenderer
Section titled “ShapeRenderer”Experimental · Since 2.9.0 · Side: both · Thread: any
ShapeRenderer draws front and top views of several seeds of a shape into one PNG, so the
mod’s owner can approve a shape before checking it in game. In a JUnit test, with planMaple
from the quick start:
@Testvoid renderMaple() throws IOException { String out = System.getenv("MYMOD_RENDER"); assumeTrue(out != null && !out.isBlank(), "set MYMOD_RENDER to draw shapes"); ShapeRenderer.render(Path.of(out), "S-maple", seed -> planMaple(RandomSource.create(seed)), 6);}- A test that skips without its folder keeps the drawing out of every build.
See also
Section titled “See also”- Features and loot: writing a planned tree’s blocks with tree flags, and adding the tree’s placed feature to biomes.
- Block behavior: saplings that grow a mod’s trees.