What's programming without an occasional Harry Potter/Minecraft mashup? In the Harry Potter universe, a Portkey is a magical teleportation device which is disguised as an ordinary, everyday object. Touch the object and it automatically teleports you to a matching location. How would we do this in Minecraft?


import mcpi.minecraft as minecraft
import mcpi.block as block
import random
import time

world = minecraft.Minecraft.create()

# place a portkey in a "random" place.
portkey_x = 0
portkey_z = 0
portkey_y = world.getHeight(portkey_x, portkey_z)

# clear some space around the port key
world.setBlocks(portkey_x - 10, portkey_y, portkey_z - 10, 
                portkey_x + 10, portkey_y + 10, portkey_z + 10, 
                block.AIR)

portkey_block = block.CHEST
world.setBlock(portkey_x, portkey_y, portkey_z, portkey_block)

destination_x = random.randint(-50, 50)
destination_z = random.randint(-50, 50)
destination_y = world.getHeight(destination_x, destination_z)

while True:
  playerTile = world.player.getTilePos()
  blockType = world.getBlock(playerTile.x, playerTile.y, playerTile.z)

  if blockType == portkey_block:
    world.postToChat("port key activated...")
    world.postToChat("hold on!")
    time.sleep(0.5)
    world.player.setPos(destination_x, destination_y + 5, destination_z)

  time.sleep(0.3)

The portkey is triggered if you stand on the object. The other way we could trigger it is by hitting the object and poll for the hits using the world.events.pollBlockHits() API.