Minecraft ========= Use Python to communicate and interact with the Minecraft world. Recipes ~~~~~~~~ .. code:: Python ## # Minecraft quick start # # API reference: https://www.stuffaboutcode.com/p/minecraft-api-reference.html # Block type examples # WOOD, has types # WOOL, has colours # WATER_STATIONARY # WATER # GOLD_ORE # GOLD_BLOCK # DIMOND_BLOCK # NETHER_REACTOR_CORE # TNT (1 to set active) # LAVA # WOOL colours for reference: wool_colour = { 'white' : 0, 'orange' : 1, 'magenta' : 2, 'light_blue' : 3, 'yellow' : 4, 'lime' : 5, 'pink' : 6, 'grey' : 7, 'light_grey' : 8, 'cyan' : 9, 'purple' : 10, 'blue' : 11, 'brwon' : 12, 'green' : 13, 'red' : 14, 'black' : 15, } # Import packages from mcpi.minecraft import Minecraft from mcpi import block from time import sleep # Create a connection between Python and Minecraft mc = Minecraft.create() .. code:: Python # Post to chat mc.postToChat("Hello world") .. code:: Python # Get position of Steve # x -> forward/backward, z -> left/right, y -> up/down # Using a Vec3 of floats (vector with float values) position = mc.player.getPos() print(f'position.x is:{position.x}, position.y is: {position.y}, position.z is: {position.z}') # Using individual variables x, y, z = mc.player.getPos() print(f'x is:{x}, y is: {y}, z is: {z}') .. code:: Python # Move Steve to a location in the world (up in the air) x, y, z = mc.player.getPos() mc.player.setPos(x, y+100, z) .. code:: Python # Set block below Steve (y-1) x, y, z = mc.player.getPos() mc.setBlock(x, y-1, z, block.STONE.id) # Set block below Steve to wool with a colour (5, Lime) x, y, z = mc.player.getPos() mc.setBlock(x, y-1, z, block.WOOL.id, 5) # Create a flower trail as Steve walks while True: x, y, z = mc.player.getPos() mc.setBlock(x, y, z, block.FLOWER_YELLOW.id) sleep(0.1) # Create a TNT trail (5th parameter (1) indicates TNT can be ignited) while True: pos = mc.player.getTilePos() mc.setBlock(pos.x, pos.y-1, pos.z, block.TNT.id, 1) sleep(0.1) .. code:: Python # Set multiple blocks (create a wall) x, y, z = mc.player.getPos() mc.setBlocks(x+1, y+1, z+1, x+11, y+11, z+11, block.STONE.id) .. code:: Python # Get the type of block Steve is standing on while True: x, y, z = mc.player.getPos() block_beneath = mc.getBlock(x, y-1, z) print(block_beneath) # Get the block (and additional data (colour/type)) that Steve is standing on while True: pos = mc.player.getTilePos() block = mv.getBlockWithData(pos) block_type = block.id block_state = block.data print(f'id: {block.id}, data: {block.data}') # If standing on grass, place a flower, otherwise turn block to grass while True: x, y, z = mc.player.getPos() block_beneath = mc.getBlock(x, y-1, z) if block_beneath == block.GRASS.id: mc.setBlock(x, y, z, block.FLOWER_CYAN.id) else: mc.setBlock(x, y-1, z, block.GRASS.id) .. code:: Python # Flatten entire world, from sea level, with sandstone mc.setBlocks(-128, 0, -128, 128, 64, 128, block.AIR.id) mc.setBlocks(-128, 0, -128, 128, -64, 128, block.SANDSTONE.id) .. code:: Python # Block hits (with sword) hitList = [] while True: for hit in mc.events.pollBlockHits(): hit_block_position = hit.pos sleep(0.1) .. code:: Python # Create four coloured areas (wool) and detect which Steve is walking over pad_position = 5 pad_size = 2 colour1 = wool_colour['orange'] colour2 = wool_colour['magenta'] colour3 = wool_colour['blue'] colour4 = wool_colour['lime'] # Create the pads mc.setBlocks(-pad_position, 0, -pad_position, -pad_position + pad_size, 0, -pad_position + pad_size, block.WOOL.id, colour1) mc.setBlocks(pad_position, 0, -pad_position, pad_position + pad_size, 0, -pad_position + pad_size, block.WOOL.id, colour2) mc.setBlocks(-pad_position, 0, pad_position, -pad_position + pad_size, 0, pad_position + pad_size, block.WOOL.id, colour3) mc.setBlocks(pad_position, 0, pad_position, pad_position + pad_size, 0, pad_position + pad_size, block.WOOL.id, colour4) # Place Steve in the centre of the pads mc.player.setTilePos(0, 0, 0) # Detect which coloured pad Steve is walking over while True: pos = mc.player.getPos() on_pad = mc.getBlockWithData(pos.x, pos.y-1, pos.z) # print(tile) if on_pad.id == block.WOOL.id: # print('Steve is standing on a pad') if on_pad.data == colour1: print('standing on orange pad') elif on_pad.data == colour2: print('standing on magenta pad') elif on_pad.data == colour3: print('standing on blue pad') elif on_pad.data == colour4: print('standing on lime pad') else: print('not standing on a pad') sleep(0.1)