TeachCraft Lesson 7

Words as Code-Magic

Now for a new part of the minecraft api - polling chatposts!


With this new feature, we can perform a specific action when you type something in minecraft chat.


It's similar to the last two lessons, but using a different (and more dynamic) trigger!


Whereas in lesson 5's example with a sword, we polled the function mc.player.pollBlockHits(), while in this example we will poll the function mc.player.pollChatPosts().


In our for loop, each loop's chatpost variable contains information on the message you typed into chat, and we can access the message you typed in via chatpost.message


In the script on the right, everytime we type "water" into chat, water will spawn over the top of our heads. We can use this as a defensive spell against fire and lava.

Sample Code

import time
from mcpi import minecraft
mc = minecraft.Minecraft.create(address="127.0.0.1", name="steve")

while True:

    for chatpost in mc.player.pollChatPosts():
        if chatpost.message == "water":

            mc.postToChat("Pouring water over you!")

            pos = mc.player.getPos()
            water_block_id = 8
            mc.setBlock(pos.x, pos.y+3, pos.z, water_block_id)

    time.sleep(1)


Your Challenge: Add a new magic spell invoked via chat

Your Challenge: Write a script invoked via you typing "shield" in Minecraft's chat, which builds a hollow glass house around you.