Skip to content

Latest commit

 

History

History
1215 lines (740 loc) · 22.7 KB

README.md

File metadata and controls

1215 lines (740 loc) · 22.7 KB

TeachCraft-Examples

Examples

Checkout the /examples directory in this repo!

MCPI library (TeachCraft version)

Click a function name to see an example.

mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

Connect to a minecraft world

from mcpi import minecraft

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

#Get current player's position
pos = mc.player.getPos()
print pos.x, pos.y, pos.z
mc.setBlock(x, y, z, block_id, [block_data])

Set the block at coordinates X/Y/Z to block_id

from mcpi import minecraft

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

#Get current player's position
pos = mc.player.getPos()

#This is the minecraft block ID of the glass block.
#To see what other block IDs are available, go here in your browser: http://minecraft-ids.grahamedgecombe.com/
glass_block_id = 20

#Set the block underneath the player to be glass
mc.setBlock(pos.x, pos.y-1, pos.z, glass_block_id)

#Set the block to the side of player to be wood of a specific subtype
wood_block_id = 5
wood_data = 1 #subtype
mc.setBlock(pos.x+1, pos.y, pos.z, wood_block_id, wood_data)
mc.getBlock(x, y, z)

Get the block at coordinates X/Y/Z, returning its block ID

from mcpi import minecraft

# Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

# Get current player's position
pos = mc.player.getPos()

# Get the block underneath the player
block_id_under_player = mc.getBlock(pos.x, pos.y-1, pos.z)
grass_block_id = 2

if block_id_under_player == grass_block_id:
    print "Player is standing on grass"
mc.getBlockWithData(x, y, z)

Get the block at coordinates X/Y/Z, returning its block ID & data field (e.g. for wool color)

from mcpi import minecraft

# Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

# Get current player's position
pos = mc.player.getPos()

# Get the block underneath the player
block_under_player = mc.getBlockWithData(pos.x, pos.y-1, pos.z)
print "block id", block_under_player.id
print "block data", block_under_player.data
mc.setBlocks(x1, y1, z1, x2, y2, z2, block_id, [block_data])

Set a cuboid of blocks between two opposite corners (x1/y1/z1 and x2/y2/z2)

from mcpi import minecraft

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

#Get current player's position
pos = mc.player.getPos()

#This is the minecraft block ID of the glass block.
#To see what other block IDs are available, go here in your browser: http://minecraft-ids.grahamedgecombe.com/
glass_block_id = 20

#Build a glass cube next to the player
mc.setBlocks(pos.x+3, pos.y, pos.z, pos.x+8, pos.y+5, pos.z+5, glass_block_id)

#Build a wood cube of a specific subtype next to the player, then make it hollow by building a smaller cube of air inside
wood_block_id = 5
wood_data = 1 #subtype
mc.setBlocks(pos.x-3, pos.y, pos.z, pos.x-8, pos.y+5, pos.z-5, wood_block_id, wood_data)

air_block_id = 0
mc.setBlocks(pos.x-2, pos.y+1, pos.z-1, pos.x-7, pos.y+4, pos.z-4, air_block_id)
mc.getBlocks(x1, y1, z1, x2, y2, z2)

Get a cuboid of blocks between two opposite corners (x1/y1/z1 and x2/y2/z2)

from mcpi import minecraft

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

#Get current player's position
pos = mc.player.getPos()

blocks = mc.getBlocks(pos.x+3, pos.y, pos.z, pos.x+8, pos.y+5, pos.z+5)
for block_id in blocks:
    print block_id
mc.player.getPos()

Get current player's position exactly (decimals)

from mcpi import minecraft

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="bob")

#Get current player's position
pos = mc.player.getPos()

# Returns Vec3(18.3814903971,6.0,25.6063951368)
# Can be accessed as pos.x, pos.y, and pos.z
print pos.x, pos.y, pos.z
mc.player.setPos(x, y, z)

Set current player's position exactly (supports decimals)

from mcpi import minecraft

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="bob")

#Get current player's position
pos = mc.player.getPos()

#Set current player's position 100 blocks in the air
mc.player.setPos(pos.x, pos.y+100, pos.z)
mc.player.getTilePos()

Get current player's position rounded to the block (integer)

from mcpi import minecraft

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="bob")

#Get current player's position
pos = mc.player.getTilePos()

# Returns Vec3(52, 4, -10)
# Can be accessed as pos.x, pos.y, and pos.z
print pos.x, pos.y, pos.z
mc.player.setTilePos(x, y, z)

Set current player's position rounded to the block (supports integers)

from mcpi import minecraft

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="bob")

#Get current player's position
pos = mc.player.getTilePos()

#Set current player's position 100 blocks in the air
mc.player.setTilePos(pos.x, pos.y+100, pos.z)
mc.getHeight(x, z)

Given an x/z coordinate, find the highest non-air block (y coordinate)

from mcpi import minecraft

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="bob")

#Get current player's position
pos = mc.player.getTilePos()

highest_block_y_coordinate = mc.getHeight(pos.x, pos.y)
print highest_block_y_coordinate
mc.postToChat("Hello World!")

Post any text string to chat in-game

from mcpi import minecraft

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="bob")

mc.postToChat("Hello World!")
mc.player.pollBlockHits()

Perform an action wherever the player right clicks with a sword

from mcpi import minecraft

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="bob")

while True:

    for blockhit in mc.player.pollBlockHits():

        #Get coordinates for block that player right clicked with sword
        pos = blockhit.pos

        #Clear out a cube of blocks at that location
        air_block_id = 0
        mc.setBlocks(pos.x+2, pos.y+2, pos.z+2, pos.x-2, pos.y-2, pos.z-2, air_block_id)
mc.player.pollProjectileHits()

Perform an action wherever the player shoots with an arrow

from mcpi import minecraft

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="bob")

while True:

    for blockhit in mc.player.pollProjectileHits():

        #Get coordinates for block that player shot with an arrow
        pos = blockhit.pos

        #Teleport player to where arrow landed
        mc.player.setPos(pos.x, pos.y, pos.z)
mc.player.pollChatPosts()

Perform an action whenever the player types something in chat

from mcpi import minecraft

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="bob")

while True:

    for chatpost in mc.player.pollChatPosts():

        #If I type explode into chat...
        if chatpost.message.lower() == "explode":

            #Get my position
            pos = mc.player.getPos()

            #Put TNT at my position
            mc.setBlock(pos.x, pos.y, pos.z, 46)

            #And put a redstone block under the TNT to activate it
            mc.setBlock(pos.x, pos.y-1, pos.z, 152)
mc.events.clearAll()

Clear all events that have happened since the events where last retrieved

from mcpi import minecraft

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="bob")

mc.events.clearAll()
mc.player.getDirection()

Get unit vector of x,y,z for the player's direction

from mcpi import minecraft

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="bob")

#Get current player's direction
direction = mc.player.getDirection()

# Returns Vec3(-0.935271308082,-0.271442436324,-0.227126801679)
# Can be accessed as direction.x, direction.y, and direction.z
print direction.x, direction.y, direction.z
mc.player.getPitch()

Get the pitch angle (-90 to 90) for the player

from mcpi import minecraft

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="bob")

#Get current player's pitch
pitch = mc.player.getPitch()

# Returns 15.750118 (or something like that)
print pitch
mc.player.getRotation()

Get the rotational angle (0 to 360) for the player

from mcpi import minecraft

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="bob")

#Get current player's rotation
rotation = mc.player.getRotation()

# Returns -256.3502 (or something like that)
print rotation

minecraftstuff library [From Martin O'Hanlon, repo, website, book]

minecraftstuff.MinecraftTurtle

turtle = MinecraftTurtle(mc, pos)

Create a Minecraft Turtle

from mcpi import minecraft
from minecraftstuff import MinecraftTurtle

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

#Get current player's position
pos = mc.player.getPos()


# create minecraft turtle
turtle = MinecraftTurtle(mc, pos)
turtle.forward(distance)

Move turtle forward [distance] number of blocks

from mcpi import minecraft
from minecraftstuff import MinecraftTurtle

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

#Get current player's position
pos = mc.player.getPos()


# create minecraft turtle
turtle = MinecraftTurtle(mc, pos)

# Move turtle forward 5 blocks
turtle.forward(5)
turtle.backward(distance)

Move turtle backward [distance] number of blocks

from mcpi import minecraft
from minecraftstuff import MinecraftTurtle

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

#Get current player's position
pos = mc.player.getPos()


# create minecraft turtle
turtle = MinecraftTurtle(mc, pos)

turtle.backward(10)
turtle.right(distance)

Move turtle right [distance] number of blocks

from mcpi import minecraft
from minecraftstuff import MinecraftTurtle

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

#Get current player's position
pos = mc.player.getPos()


# create minecraft turtle
turtle = MinecraftTurtle(mc, pos)

turtle.right(10)
turtle.left(distance)

Move turtle left [distance] number of blocks

from mcpi import minecraft
from minecraftstuff import MinecraftTurtle

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

#Get current player's position
pos = mc.player.getPos()


# create minecraft turtle
turtle = MinecraftTurtle(mc, pos)

turtle.left(10)
turtle.up(distance)

Move turtle up [distance] number of blocks

from mcpi import minecraft
from minecraftstuff import MinecraftTurtle

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

#Get current player's position
pos = mc.player.getPos()


# create minecraft turtle
turtle = MinecraftTurtle(mc, pos)

turtle.up(10)
turtle.down(distance)

Move turtle down [distance] number of blocks

from mcpi import minecraft
from minecraftstuff import MinecraftTurtle

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

#Get current player's position
pos = mc.player.getPos()


# create minecraft turtle
turtle = MinecraftTurtle(mc, pos)

turtle.down(10)
turtle.home()

Move turtle back to the position it started in

from mcpi import minecraft
from minecraftstuff import MinecraftTurtle

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

#Get current player's position
pos = mc.player.getPos()


# create minecraft turtle
turtle = MinecraftTurtle(mc, pos)

turtle.down(10)
turtle.right(10)
turtle.home()
turtle.speed(integer)

Change the turtles speed (1 - slowest, 10 - fastest, 0 - no animation, it just draws the lines)

from mcpi import minecraft
from minecraftstuff import MinecraftTurtle

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

#Get current player's position
pos = mc.player.getPos()


# create minecraft turtle
turtle = MinecraftTurtle(mc, pos)

turtle.speed(5)
turtle.down(10)
turtle.speed(10)
turtle.right(10)
turtle.home()
turtle.penblock(block_id, [block_data])

Change the turtles speed (1 - slowest, 10 - fastest, 0 - no animation, it just draws the lines)

from mcpi import minecraft
from minecraftstuff import MinecraftTurtle

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

#Get current player's position
pos = mc.player.getPos()


# create minecraft turtle
turtle = MinecraftTurtle(mc, pos)

grass_block_id = 2
turtle.penblock(grass_block_id)
turtle.down(10)

wool_block_id = 35
wool_block_data = 1 #orange

turtle.penblock(wool_block_id, wool_block_data)
turtle.right(10)
turtle.penup()

Put the pen up (stop drawing when the turtle moves)

from mcpi import minecraft
from minecraftstuff import MinecraftTurtle

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

#Get current player's position
pos = mc.player.getPos()


# create minecraft turtle
turtle = MinecraftTurtle(mc, pos)

turtle.down(10)

turtle.penup()

turtle.right(10)
turtle.pendown()

Put the pen down (start drawing again when the turtle moves after you called turtle.penup())

from mcpi import minecraft
from minecraftstuff import MinecraftTurtle

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

#Get current player's position
pos = mc.player.getPos()


# create minecraft turtle
turtle = MinecraftTurtle(mc, pos)

turtle.down(10)

turtle.pendown()

turtle.right(10)

turtle.penup()

turtle.up(10)
turtle.isdown()

Check if the pen is down, returning a boolean

from mcpi import minecraft
from minecraftstuff import MinecraftTurtle

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

#Get current player's position
pos = mc.player.getPos()


# create minecraft turtle
turtle = MinecraftTurtle(mc, pos)

turtle.down(10)

turtle.pendown()

turtle.right(10)

if turtle.isdown():
    print "Pen is down!"
turtle.setposition(x, y, z)

Reset turtle's position to a given x/y/z coordinate

from mcpi import minecraft
from minecraftstuff import MinecraftTurtle

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

#Get current player's position
pos = mc.player.getPos()


# create minecraft turtle at player's current position
turtle = MinecraftTurtle(mc, pos)

turtle.down(10)

turtle.right(10)

# Have turtle reset back to player's position
turtle.setposition(pos.x, pos.y, pos.z)
turtle.setx(x)

Reset turtle's position to a given x coordinate

from mcpi import minecraft
from minecraftstuff import MinecraftTurtle

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

#Get current player's position
pos = mc.player.getPos()


# create minecraft turtle at player's current position
turtle = MinecraftTurtle(mc, pos)

turtle.down(10)

turtle.right(10)

# Have turtle reset back to player's x position
turtle.setx(pos.x)
turtle.sety(y)

Reset turtle's position to a given y coordinate

from mcpi import minecraft
from minecraftstuff import MinecraftTurtle

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

#Get current player's position
pos = mc.player.getPos()


# create minecraft turtle at player's current position
turtle = MinecraftTurtle(mc, pos)

turtle.down(10)

turtle.right(10)

# Have turtle reset back to player's y position
turtle.setx(pos.y)
turtle.setz(z)

Reset turtle's position to a given z coordinate

from mcpi import minecraft
from minecraftstuff import MinecraftTurtle

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

#Get current player's position
pos = mc.player.getPos()


# create minecraft turtle at player's current position
turtle = MinecraftTurtle(mc, pos)

turtle.down(10)

turtle.right(10)

# Have turtle reset back to player's z position
turtle.setx(pos.z)
turtle.position

Retrieve turtle's current x/y/z position

from mcpi import minecraft
from minecraftstuff import MinecraftTurtle

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

#Get current player's position
pos = mc.player.getPos()


# create minecraft turtle at player's current position
turtle = MinecraftTurtle(mc, pos)

turtlePos = turtle.position
print turtlePos.x
print turtlePos.y
print turtlePos.z
turtle.setheading(angle)

Set the turtles headings

from mcpi import minecraft
from minecraftstuff import MinecraftTurtle

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

#Get current player's position
pos = mc.player.getPos()


# create minecraft turtle at player's current position
turtle = MinecraftTurtle(mc, pos)

turtle.setheading(90)
turtle.setverticalheading(angle)

Set the turtles vertical headings

from mcpi import minecraft
from minecraftstuff import MinecraftTurtle

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

#Get current player's position
pos = mc.player.getPos()


# create minecraft turtle at player's current position
turtle = MinecraftTurtle(mc, pos)

turtle.setverticalheading(90)
turtle.walk()

Force the turtle to walk along the ground

from mcpi import minecraft
from minecraftstuff import MinecraftTurtle

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

#Get current player's position
pos = mc.player.getPos()


# create minecraft turtle at player's current position
turtle = MinecraftTurtle(mc, pos)

turtle.walk()
turtle.fly()

Allow the turtle to fly (e.g. not be forced to move along the ground)

from mcpi import minecraft
from minecraftstuff import MinecraftTurtle

#Connect to minecraft server 127.0.0.1 as player 'steve'
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

#Get current player's position
pos = mc.player.getPos()


# create minecraft turtle at player's current position
turtle = MinecraftTurtle(mc, pos)

turtle.fly()

minecraftstuff.MinecraftShape

minecraftstuff.MinecraftDrawing

Block IDs