In this lesson we will use the agent to build small farm modules and plant several different crops. You will need to create your own world to complete this lesson. I recommend using the flat grass template, but you might find that way too boring. You should also set it to creative mode.

You can read about the details of crop farming in Minecraft on this website:

We will utilise similar code from the rectangles lesson, but I have added some extra functions to help create our farms which will look like this:

A completed farming module.
If you examine the picture above you will see there are 4 different block types (wood plank, dirt, water, glass) and 3 different crops (carrot, beetroot, wheat). The wood, dirt and glass are all rectangles of various dimensions:
  • Wood planks: 10 x 7
  • Dirt: 6 x 5
  • Water: 1 x 1
  • Glass: 1 x 5

To grow crops you must have a water source nearby which is why we have 2 troughs of water on either side. I cover it with glass so that things don’t fall into the water – I like it to stay nice and clean. There is another reason, but I’ll get to that in part 2. You also need to till the soil before you plant. The order this lesson will suggest to do things in is:

  1. Wood planks (rectangle outline)
  2. Dirt (rectangle filled)
  3. Water right side – the agent only needs to place water in 1 block to fill a trough
  4. Water left side
  5. Glass – left side
  6. Glass – right side
  7. Till soil
  8. Plant crops

The code I’m providing to you has 4 main functions and a few commands to get you started:

  • rectangleOutline – similar to the one from a previous lesson, but I’ve added the inventory slot as an extra parameter
  • rectangleFilled – similar to the one from a previous lesson, but I’ve added the inventory slot as an extra parameter
  • tillDirt – this works similar to rectangleFilled but instead of placing blocks it tills dirt
  • plantCrops – this uses inventory slots 1 – 5 to plant crops in a filled rectangular area
  • Command – tpa – teleports agent to user location
  • Command – L – turns agent to left
  • Command – R – turns agent to right
This image has an empty alt attribute; its file name is rectangleOutline.png
This function call will build a 10 x 7 rectangle outline using blocks in inventory slot 27 (the last one)

To import the code I’ve prepared you must right-click this link and select ‘Copy Link Address’. If you’ve forgotten how to import code expand the section below.

Import code into code builder

Might also look like this:

Step 2: Click the Import button

This time we will import some code that I’ve already prepared for you. You will still need to some coding, but it will be to use and experiment with the code I’ve written to see what you can do with it.


Step 3: Click Import URL…

Right-click the ‘Code’ button below and select ‘Copy link address’ in the pop-up menu. Then click the ‘Import URL…’ option in Minecraft (as pictured).

Code

Step 4: Paste the address in the box

Click in the white text box and then press Ctrl-V (paste) to paste in the address from the code button in the previous step. You can also right click the text box and choose ‘Paste’.


Step 5: Go ahead!

Click the green ‘Go ahead!’ button to complete the import process. It should look similar to the picture here (not identical). If it doesn’t – did you forget to copy or paste in the address?

You must then complete the ‘make’ command to build your farm. It currently only has 1 block of code in it. You should end up with 16 before you do the challenge at the end:

You need to add the blocks to create the rectangles in the correct positions. Do not remove the first block.

Step 1 – Build border (1 code block)

This image has an empty alt attribute; its file name is farm-step1-1024x380.png
Hmmm, this looks like a rectangle that is 10 x 7 blocks

Step 1 is to build the wooden plank border around the farm. The code automatically gives your agent some planks in slot 27 so all you need to do is to add the function call to build the correct type and size rectangle. Notice how the agent ends up at the starting position of the rectangle. This step requires only 1 block and your make command should have 2 blocks by the end of this step.

Remember, to add function calls you click the ‘advanced’ button in the code builder and then click ‘Functions’. Expand the box below for pictorial steps.

Adding function calls to your code

To add function calls you first click the ‘Advanced’ menu and then ‘Functions’. You should see the list of 5 functions as pictured above.

Step 2 – Add dirt (3 code blocks)

This looks like 6 x 5 blocks of dirt

Step 2 is to build the rectangular area of dirt. Make sure you leave a gap on either side for the water source. After the agent builds a filled rectangle it also returns to the starting position. Can you see the difference in starting positions between step 1 and 2? You will need to move the agent forward (x2) and right before you build the dirt rectangle. It’s simpler if you don’t turn the agent. This step requires 3 blocks in total and your make command should end up with 5 in total.

Before you attempt step 3, you must give your agent a water bucket in slot 24. You can then proceed.

Step 3 – Add water on both sides (4 code blocks)

Step 3 actually only requires a 1 x 1 block of water on each side of the soil, but you still need to move the agent just a little bit so he’s over the gap between the dirt and the wood on the right. Then you move forward (how many blocks?) to the gap on the other side before placing another water block down. The flowing water fills the 1 x 5 rectangular space and provides much needed moisture to the soil for crops to grow. Yes, we could have done the water then the soil and then the other bit of water – this is just the way I did it. This step requires 4 extra code blocks to complete and your make command should now have 9 in total.

Step 4 – Add glass on both sides (2 code blocks)

Step 4 and 5 are pretty much the same. Build a 1 x 5 section of glass blocks. The agent has to move up before you do this. This might seem a bit unnecessary, but it will make more sense later when we automate the collection of crops with redstone mechanisms. This step requires 2 code blocks to complete and your make command should now have… 11!

Step 5 – The other side (2 code blocks)

Once you are done with this your make command should now have 13 blocks in total. 
Notice how the agent is always facing the same way? Ok, we are nearly done. Now to prepare the soil for planting…

Step 6 – Tilling (2 code blocks)

You should use the tillSoil function for this step. The dimensions should be 6 x 5. You could make the farm bigger, but this is a good size and you can always build lots of them. The agent must be 1 block above the soil to till it. The darker areas of soil are those that have already absorbed moisture from the water source and are ready for planting. This step requires 2 code blocks and your make command should have a total of 15 once you are done.

Step 7 – Planting (1 code block)

Before you plant you should give your agent crops to plant in slots 1 – 5. Your agents’ inventory should look something like the picture above. The order of the crops will determine what is planted in each row of the farm (there are 5 rows in total). If you manage to complete it you should have a total of 16 code blocks in the make command. You could have more or even less.

Challenge 1 – Multiple farms (4 code blocks)

Build 3 farm modules next to each other. Make sure you bring your agent back down to the level of the glass blocks otherwise the water may flow out as the code assumes there’s solid ground under the farm.