Minecraft Education – Lesson 3

Events and Conditionals with Chat Commands

Objectives

Vocabulary


Part A – Simple Chat Command

We can attach code to a word we type in the chat.

Sample Code:

def on_chat():
    player.say("You used the 'start' command!")

player.on_chat("start", on_chat)

Try: Type start in the game chat and see the message appear.


Part B – Conditionals (If-Statements)

We can make choices in our code using if and else.

Sample Code:

health = 10  # pretend health value for this example

if health > 5:
    player.say("You are healthy!")
else:
    player.say("You need healing!")

Discussion:


Part C – Chat Command That Builds Different Structures

Here we use a chat command and a conditional to choose between different builds.

Sample Code:

def build_structure(struct_type):
    if struct_type == 1:
        player.say("Building a stone tower!")
        for y in range(5):
            blocks.place(STONE, pos(0, y, 0))
    else:
        player.say("Building a glass pillar!")
        for y in range(5):
            blocks.place(GLASS, pos(0, y, 0))

def on_chat_type(num):
    build_structure(num)

player.on_chat_number("build", on_chat_type)

Usage:

(If your Python template does not support numeric chat parameters in this way, you can simplify to one structure per command such as "tower" and "pillar".)


Exit Ticket / Reflection