Agent Build

Introduction

The agent is very useful if it can build things for you. One thing you will learn in this tutorial is how to use the ||agent:agent place on move||. This tells your agent to place a block every time it moves. You will make some chat commands to tell the agent to place blocks and to move forward. You will also add some commands for turning your agent. With these commands you can make your agent build a shape. In this tutorial, the shape is a square.

The agent can build things for you

Chat command

Go to your Agent in Minecraft and equip him with blocks. Can’t find your Agent? Add a chat command with ||agent:agent teleport to player||.

player.onChat("tp", function () {
    agent.teleportToPlayer()
})

Place blocks

Add a new chat command pd (for “pen down”) and place ||agent:agent place on move|| to tell the Agent to place a block as it moves.

player.onChat("pd", function () {
    agent.setAssist(PLACE_ON_MOVE, true)
})

Load some blocks

Your Agent needs blocks in the inventory to build with. Add a chat command rl (for “reload”) that adds blocks to the inventory. Put some sandstone blocks in slot 1.

player.onChat("rl", function () {
    agent.setItem(SANDSTONE, 16, 1)
    agent.setSlot(1)
})

Moving forward

Add a chat command fd (for “forward“) that moves the Agent by 3 blocks.

player.onChat("fd", function () {
    agent.move(FORWARD, 3);
})

Try the commands

Go to Minecraft, press t to open the chat. First, enter rl to load some blocks. Then, enter pd and finally fd to get the Agent to build a wall.

Make sure to press Enter in the chat after each command.

Build a square

Add a chat command to build a square by combining ||agent:agent move|| and ||agent:agent turn|| blocks.

You can create a sequence of commands that the Agent will execute in order. Try to decompose the different steps, move or turn needed to build a square.

player.onChat("square", function () {
    agent.move(FORWARD, 3)
    agent.turn(LEFT_TURN)
    agent.move(FORWARD, 3)
    agent.turn(LEFT_TURN)
    agent.move(FORWARD, 3)
    agent.turn(LEFT_TURN)
    agent.move(FORWARD, 3)
    agent.turn(LEFT_TURN)
})

Iterate

That was an awful lot of blocks! Use the ||loops:for|| loop to repeat code.

The ||loops:for|| loop tells the computer to repeat the code under it.

player.onChat("square", function () {
    for (let index = 0; index <= 3; index++) {
        agent.move(FORWARD, 3)
        agent.turn(LEFT_TURN)
    }
})

Detect contact

Ouch the Agent keeps hitting the last block! Use ||agent:agent detect|| and ||logic:if then else|| to turn and move out if a block is in the way.

The ||logic:if|| block runs the code under ||logic:then|| if that ||agent:agent detect|| returns true. The agent turns left otherwise.

player.onChat("square", function () {
    for (let index = 0; index <= 3; index++) {
        agent.move(FORWARD, 3)
        if (agent.detect(AgentDetection.Block, FORWARD)) {
            agent.turn(RIGHT_TURN)
            agent.move(FORWARD, 1)
        } else {
            agent.turn(LEFT_TURN)
        }
    }
})