Hardware Reference
In-Depth Information
2. Import the necessary modules. You will need the normal Minecraft module
and, because you are interacting with blocks, also the block module:
import mcpi.minecraft as minecraft
import mcpi.block as block
import time
3. Connect to the Minecraft game:
mc = minecraft.Minecraft.create()
4. Because you plan to use parts of this program in a bigger program later, you are
going to write the bulk of the code inside a function called safeFeet() . This
will make it easier for you to reuse this code later. The first thing this function
does is to get the position of your player, Steve:
def safeFeet():
pos = mc.player.getTilePos()
5. getBlock() will get the block id of the block at the coordinates you provide.
Because pos.x, pos.y and pos.z are the coordinates of your player, you must
use pos.y-1 to get the block directly below Steve's feet:
b = mc.getBlock(pos.x, pos.y-1, pos.z)
# note: pos.y-1 is important!
6. You now use a simple method to work out whether your player is safe or not. If
he is standing on air or water, then he is not safe. Otherwise, he is safe. It is sim-
pler to check the block types for the “not safe” condition, as there are a few hun-
dred blocks you would need to check for the “safe” condition. This is quite a long
piece of code, so make sure you type it in all on one line:
if b == block.AIR.id or b == block.WATER_STATIONARY.id
or b == block.WATER_FLOWING.id:
7. If the block is one of the unsafe blocks, post a message to the Minecraft chat say-
ing that your player is not safe. Otherwise, he is safe.
Make sure you get the indents correct here, otherwise the program will not
work. Remember that all of the code inside the function is indented one level,
and any code inside an if or an else is indented by another level. The if and
the else statements should line up with each other as they are related and
logically at the same level.
mc.postToChat("not safe")
else:
mc.postToChat("safe")
 
Search WWH ::




Custom Search