https://s3-us-west-2.amazonaws.com/secure.notion-static.com/6fb0230e-8a67-4121-b4f6-24421f40ad26/09-shields-are-up.png

Create a wall of glass around your Minecraft creations by simply running your Python program.

What you’ll learn:

Nothing’s worse than when you’ve spent hours building something amazing in Minecraft, only for someone to come along and grief your creation. With this program, you’ll be able to throw up a ‘force field’ made of glass all around your base at the touch of a button. For this exercise, we’ll need the exact coordinates for the perimeter of our house, so we’ll start with a program that can determine our location in Minecraft.

# Force Field!
# Create a glass wall around your creation to protect it from griefers.
# Jim Christian
# CodinginMinecraft.com

# Import the Minecraft API
import mcpi.minecraft as minecraft
import mcpi.block as block
import time

def liveCompass():
    #create a connection to the Minecraft game
    mc = minecraft.Minecraft.create()
    # set a variable for the player's position
    pos = mc.player.getPos()
    pos = minecraft.Vec3(int(pos.x), int(pos.y), int(pos.z))
    time.sleep(5)
    print(pos)
    mc.postToChat("I'm currently at x: " + str(pos.x) + " y: " + str(pos.y) + " z: " + str(pos.z))

def shieldsUp():
    # create a connection to the Minecraft game
    mc = minecraft.Minecraft.create()
    forcex1 = 7
    forcex2 = 83
    forcez1 = -141
    forcez2 = -230
    forcey = -1
    height = 20
    field = block.GLASS.id

    # Set surrounding blocks to same value, with a 1m gap made out of glass
    mc.setBlocks(forcex1, forcey, forcez1, forcex2, forcey + height, forcez1, field)
    mc.setBlocks(forcex2, forcey, forcez1, forcex2, forcey + height, forcez2, field)
    mc.setBlocks(forcex2, forcey, forcez2, forcex1, forcey + height, forcez2, field)
    mc.setBlocks(forcex1, forcey, forcez1, forcex1, forcey + height, forcez2, field)

def shieldsDown():
    # create a connection to the Minecraft game
    mc = minecraft.Minecraft.create()
    forcex1 = 7
    forcex2 = 83
    forcez1 = -141
    forcez2 = -230
    forcey = -1
    height = 20
    field = block.AIR.id

    # Set surrounding blocks to same value, with a 1m gap made out of glass
    mc.setBlocks(forcex1, forcey, forcez1, forcex2, forcey + height, forcez1, field)
    mc.setBlocks(forcex2, forcey, forcez1, forcex2, forcey + height, forcez2, field)
    mc.setBlocks(forcex2, forcey, forcez2, forcex1, forcey + height, forcez2, field)
    mc.setBlocks(forcex1, forcey, forcez1, forcex1, forcey + height, forcez2, field)    

# True is *always* True. So therefore the following code will run until it is terminated.
while True:
    if raw_input() == "up":
        print("Shields up!")
        shieldsUp()
    if raw_input() == "down":
        print("Shields down!")
        shieldsDown()