Skip to content

Examples

Ivaynn edited this page Jul 6, 2024 · 3 revisions

To help you understand how to use the instructions, here are some examples. It's important to include the line numbers because of the line instruction.

Bridge Builder

This program builds an infinite bridge using stone blocks from the turtle's inventory:

1 |  move forward
2 |  use minecraft:stone down
3 |  line set 1

With this program, the turtle will place blocks and move forward until it runs out of stone blocks, which will generate an error. To prevent this, you can add a simple check using if item to see if the specified item is in the turtle's inventory:

1 |  move forward
2 |  use minecraft:stone down
3 |  if item minecraft:stone
4 |  line set 1
5 |  move back

In this program, if the condition in line 3 is true (there are still stone blocks in the turtle's inventory), the turtle will execute line 4 and then loop back to line 1 and continue building the bridge. However, if the condition is false (meaning that there are no more stone blocks in the turtle's inventory), line 4 will be skipped, and the turtle will execute line 5 to move one block back, completing its program.


Cobblestone Generator

This program demonstrates how a counter can be used. This instruction allows you to perform operations with values from different sources, such as fixed values, other counters, coordinates, fuel level, or random numbers.

1 |  counter blocks_mined = value 0
2 |  mine forward
3 |  counter blocks_mined += value 1
4 |  if counter blocks_mined < value 10
5 |  line set 2
6 |  insert minecraft:cobblestone up
7 |  line set 1

This program mines the block in front of the turtle 10 times, inserts the mined cobblestone in a chest above it and then restarts.

  • On line 1, the counter blocks_mined is set to 0.
  • On lines 2 and 3, the block in front of the turtle is mined and the counter blocks_mined is incremented by 1.
  • On line 4, the program checks if the counter's value is less than 10. If it is, the program continues to line 5, looping back to line 2. If the counter's value is not less than 10, line 5 is skipped, exiting this loop and continuing the program from line 6.
  • On line 6, the turtle inserts all the cobblestone in its inventory into a chest above it.
  • On line 7, the program loops back to line 1, restarting the program.

It's worth noting that the program can be improved by adding a check to ensure the block in front of the turtle is cobblestone, using if block minecraft:cobblestone forward, or by verifying if the item transfer was successful, using if item minecraft:cobblestone after using insert.


Tree Cutter

Compared to the previous programs, this one is more complex. A similar program is already included in the imports list of the data pack with an ID of 2. This program can be used to easily automate wood farming when coupled with a dispenser filled with bone meal. The turtle needs oak saplings in its inventory.

1  |  if block air forward
2  |  use minecraft:oak_sapling forward
3  |  wait
4  |  if block minecraft:oak_log forward
5  |  line set 8
6  |  line set 1
7  |  :
8  |  move forward
9  |  counter height = value 0
10 |  unless block minecraft:oak_log up
11 |  line set 16
12 |  move up
13 |  counter height += value 1
14 |  line remove 4
15 |  :
16 |  move down
17 |  counter height -= value 1
18 |  if counter height >= value 1
19 |  line remove 3
20 |  move back
21 |  grab minecraft:oak_sapling
22 |  line set 1

This program can be divided into 3 sections, separated by empty lines (7 and 15).

  • Plant the sapling and wait for the tree to grow:
    • On line 1, the program checks if the block in front of the turtle is air. If so, it plants an oak sapling there (line 2) and then continues normally to line 3. If not, it skips line 2 and goes directly to line 3.
    • On line 4, the program checks if the block in front of the turtle is an oak log. If so, it goes to the next section and if not, it restarts this section.
  • Cut the tree:
    • On line 8, the turtle moves forward. For this program, the turtle has the "Auto Mine" option enabled, so this mines the block too.
    • On line 9, a counter height is created to track the turtle's position relative to the ground, making it possible to return to the starting position in the next section.
    • On line 10, the program checks the block above the turtle. If it's oak log, it skips line 11 and continues from 12 (unless has the opposite result of if).
    • On line 11, the program skips to the next section. Thanks to the condition of line 10, this only runs if the block above the turtle isn't an oak log.
    • On line 12, 13 and 14, the turtle moves up, mining the block above, increments the counter height by 1 and restarts this loop.
    • This loop continues until the block above the turtle isn't oak log, meaning there's no more tree to cut.
  • Return to the starting position:
    • On line 16 and 17, the turtle moves down and the counter height is reduced by 1.
    • On line 18, the program checks if the counter is greater than or equal to 1, meaning that the turtle isn't at the starting height yet. If that's the case, it runs line 19, looping back to the start of this section. If not (meaning the turtle is at the starting height), line 19 is skipped, exiting this loop.
    • On line 20, the turtle moves back, returning to its starting position.
    • On line 21, it picks up any oak saplings that may have dropped from the leaves.
    • On line 22, the program restarts.
Clone this wiki locally