Minecraft Forestry

We can build very simple trees programmatically in Minecraft by stacking blocks together using
wood-y and leaf-y materials.

importmcpi.minecraftasminecraftimportmcpi.blockasblockimportrandomimporttimedefbuild_trunk(world,x,y,z,trunk_height,trunk_radius,trunk_material):iftrunk_radius==0:world.setBlocks(x,y,z,x+1,y+trunk_height,z+1,trunk_material)else:world.setBlocks(x-trunk_radius,y,z-trunk_radius,x+trunk_radius,y+trunk_height,z+trunk_radius,trunk_material)defgenerate_leaves(world,x,y,z,leaf_height,leaf_radius,leaf_material):world.setBlocks(x-leaf_radius,y,z-leaf_radius,x+leaf_radius,y+leaf_height,z+leaf_radius,leaf_material)# function to build a simple tree using the given materialsdefbuild_tree(world,x,y,z,trunk_height=5,trunk_radius=1,leaf_height=3,leaf_radius=4,trunk_material=block.WOOD,leaf_material=block.LEAVES):build_trunk(world,x,y,z,trunk_height,trunk_radius,trunk_material)generate_leaves(world,x,y+trunk_height,z,leaf_height,leaf_radius,leaf_material)world=minecraft.Minecraft.create()# clear out the spaceworld.setBlocks(-128,0,-128,128,40,128,block.AIR)world.setBlocks(-128,1,-128,128,2,128,block.GRASS)playerTile=world.player.getTilePos()x=playerTile.x+5z=playerTile.z+5y=world.getHeight(x,z)build_tree(world,x,y,z)

Square blocks of leaves aren’t very realistic so we can trim off the square edges like this.

defsand_off_corners(world,x1,y1,z1,x2,y2,z2):world.setBlock(x1,y1,z1,block.AIR)world.setBlock(x2,y1,z1,block.AIR)world.setBlock(x1,y1,z2,block.AIR)world.setBlock(x2,y1,z2,block.AIR)world.setBlock(x1,y2,z1,block.AIR)world.setBlock(x2,y2,z1,block.AIR)world.setBlock(x1,y2,z2,block.AIR)world.setBlock(x2,y2,z2,block.AIR)defgenerate_leaves(world,x,y,z,leaf_height,leaf_radius,leaf_material):world.setBlocks(x-leaf_radius,y,z-leaf_radius,x+leaf_radius,y+leaf_height,z+leaf_radius,leaf_material)# knock out corners to make it slightly more realistic.sand_off_corners(world,x-leaf_radius,y,z-leaf_radius,x+leaf_radius,y+leaf_height,z+leaf_radius)

Finally, we can make ourselves a nice orchard or small forest using some random values for each tree:

Note by using the ground value specific to each tree location, we can get a nice effect “planting” trees on a sloping
hillside.

We can go further by changing the materials for each tree, the trunk and leaf materials, on a random basis. The setBlock
function can take an extra argument for some block types. For wood and leaves, you are able to pick the exact kind of
material you would like.

value

Wood

0

Oak

1

Spruce

2

Birch

value

Leaves

1

Oak

2

Spruce

3

Birch

In the final version, the foliage still looks a bit too false as a single monolithic block of leaves. We can
change the leaf generation to work on a probability of a leaf block being at any one position in the canopy.