Python in Minecraft 3 – Placing many blocks at once

The previous example showed that python scripts could quickly become very long if each block had to be set individually. Fortunately there is the setBlocks command which can set many blocks at once. It also has two forms.

mc.setBlocks(x1,y1,z1,x2,y2,z2,block)
mc.setBlocks(x1,y1,z1,x2,y2,z2,block.id,variation)

The minecraft coordinates (x1,y1,z1) represent one corner of the rectangular prism about to be set. The minecraft coordinates (x2,y2,z2) represent the diagonally opposite corner. The previous example can be rewritten to use the setBlocks command. The glass is set first otherwise it would overwrite the setting of the middle block. In this example the middle block is air which is equivalent to there being no block.

nano placeglassblocks.py

import mcpi.minecraft as minecraft
import mcpi.block as block
mc=minecraft.Minecraft.create()
mc.setBlocks(-220,72,-223,-218,74,-221,block.GLASS)
mc.setBlock(-219,73,-222, block.AIR)

python3 placeglassblocks.py

Categories: CoderDojo