Coding Ireland STEM Report 2024 Have Your Say
Minecraft
Advanced
60 mins
175 points
What you need:
  • Computer/laptop

Agent Activities

Learn what your agent is and how to program it to assist you in Minecraft.

1 - Open Minecraft

In this project we are going to learn what a Minecraft agent is and how you can use it to help you do things in Minecraft.

First, open Minecraft Education Edition, login, create a new world and open the Code Builder.

2 - What is an agent?

The agent is your assistant who helps you do things in Minecraft. You command the agent with the agent blocks.

Your agent appears when you teleport it to you. When you have your agent, it can do work for you. Give things from your inventory to your agent and have it build stuff for you.

You make your agent go in different directions by moving, turning, and detecting blocks in the way.


3 - Make the agent come to you

First let's make the agent to come the position in the Minecraft world that we are in.

Create a command called summon and give it this code.

player.onChat("summon", function () {
    player.say("Hey Agent!")
    agent.teleportToPlayer()
})

Go to your world and run the summon command. The agent should come to you.


4 - Make the agent dig

Next let's create a command to make the agent dig a certain number of levels that we tell it to.

For each level the agent will repeat the following 4 times:

  • move forward 1
  • if there is a block underneath, destroy it
  • collect the material
  • turn left
  • move down to the next level

Add the following code to create the command.

player.onChat("dig", function (levels) {
    for (let i = 0; i < levels; i++) {
        for (let i = 0; i < 4; i++) {
            agent.move(FORWARD, 1)
            if (agent.detect(AgentDetection.Block, DOWN)) {
                agent.destroy(DOWN)
                agent.collectAll()
            }
            agent.turn(LEFT_TURN)
        }
        agent.move(DOWN, 1)
    }
})

Now run the command in your world (e.g. "dig 3" to dig 3 levels down) and watch the agent dig down the number of levels you specified. Try it a few times with different levels.

The for block is a loop block that starts the variable i at 0 and then adds 1 to i each time that it runs through it's code until i equals the number after "to" in the block.

Because we start at 0, we need to subtract 1 from the levels variable that we pass in.

For example if we pass in the number 4 for the levels ("dig 4") that will make the for loop do the following:

  1. i = 0 and is not equal to (4 - 1)
  2. i = 1 and is not equal to (4 - 1)
  3. i = 2 and is not equal to (4 - 1)
  4. i = 3 and is equal to (4 - 1)


5 - Make the agent build a tower

Now let's make the agent build something!

Create a command called tower and add the following code to it to make the agent build a tower 10 blocks high.

player.onChat("tower", function () {
    agent.move(FORWARD, 5)
    agent.setSlot(1)
    agent.setAssist(PLACE_ON_MOVE, true)
    agent.setAssist(DESTROY_OBSTACLES, true)
    for (let i = 0; i < 11; i++) {
        for (let i = 0; i < 4; i++) {
            agent.setItem(SANDSTONE, 16, 1)
            agent.move(FORWARD, 4)
            agent.turn(LEFT_TURN)
        }
        agent.move(UP, 1)
    }
})

Join our club 😃

To view the remaining 2 steps and access hundreds of other coding projects please login or create an account.

Copyright Notice
This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.
🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more