This is a reference of Minecraft Python API Library, which is supported on Minecraft: Pi edition and the PC version using the RaspberryJuice plugin.
似乎缺少生物相关的api?难道生物也是block?
pip install mcpi
一、代码结构
minecraft.py
Class Minecraft - main class for connecting and interacting with the game
Class camera - changing camera angle and postion
Class player - getting and changing the players position and setting
Class entity - getting and changing entities position and setting
Class events - retreiving events which have occured in the game
block.py
Class Block - definition of a block, specifically its type
event.py
Class BlockEvent - definition of a block event, specifically what event, what block and what player
vec3.py
Class Vec3 - generic class for managing a 3 dimension vector (i.e. x,y,z)
connection.py - internal module used by the api
util.py - internal module used by the api
Compatability
Not all functions and block types are available on all version of the api, by each function you will see a logo which shows whether that function is available:
二、API 明细
import mcpi.minecraft as minecraft
import mcpi.block as block
1.Minecraft
"Main class for interacting with the Minecraft world, includes functions for creating a connection, modifying players and blocks and capturing events"
.create(address = "localhost", port = 4711)
"Create connection to Minecraft (address, port) => Minecraft object"
mc = minecraft.Minecraft.create() #use default address and port
mc = minecraft.Minecraft.create("192.168.1.1", 4711) #specify ip address and port
.getBlock(x,y,z)
"Get block (x,y,z) => id:int"
blockType = mc.getBlock(0,0,0) #retrieves the block type for the block at 0,0,0
.getBlocks(x0,y0,z0,x1,y1,z1)
"Get a cuboid of blocks (x0,y0,z0,x1,y1,z1) => [id:int]"
blocks = mc.getBlocks(-1,-1,-1,1,1,1) #get the block id's in a cuboid
for block in blocks:
print block
.getBlockWithData(x,y,z)
"Get block with data (x,y,z) => Block"
blockObj = mc.getBlockWithData(0,0,0) #retrieves a block object for the block at 0,0,0
.setBlock(x,y,z)
"Set block (x,y,z,id,[data])"
mc.setBlock(0,0,0,block.DIRT.id) #sets a block at an x, y, z co-ordinate to a particular type
mc.setblock(0,0,0,block.WOOD.id, 1) #sets a block to a particular type and 'subtype'
.setBlocks(x0,y0,z0,x1,y1,z1,blockType, blockData)
"Set a cuboid of blocks (x0,y0,z0,x1,y1,z1,id,[data])"
mc.setBlocks(-1, -1, -1, 1, 1, 1, block.STONE.id) #sets many blocks at a time, filling the gap between 2 sets of x, y, z co-ordinates
.getHeight(x,z)
"Get the height of the world (x,z) => int"
y = mc.getHeight(0,0) #find the y (vertical) of an x, z co-ordinate which represents the 'highest' (non-air) block
.getPlayerEntityIds()
"Get the entity ids of the connected players => [id:int]"
entityIds = mc.getPlayerEntityIds() #get the entity id's of the players connected to the game
for entityId in entityIds:
print entityId
.getPlayerEntityId(playerName)
"Get the entity id for a named player => [id:int]