Project 10: Block Detector

Step 2

Completing the program


Next, add the code as shown to complete the program!

Here, we first have an infinite loop as well as the “getPos” function so we can constantly get the player’s location.

We then have a for loop inside the infinite loop. Notice how this time we only use 1 number/parameter, for the “range”, when this happens we start at the number 0. In this case, “i” will go from 0-64, looping 63 times!

Every time we run the for loop we check what the next block down is. We do this using the “getBlock” function combined with the “i” variable. Remember, “i” will go up each loop, so saying y-i will always mean 1 more block down.

We then have an if statement to see if the block is a diamond ore block. If it is we post a message and place a torch to mark it. (The “str” code is a function that converts a number into text, allowing us to put it in our chat post.)

Lastly, outside the for loop but still inside the infinite loop, we have a small delay.


Testing the program


Now that we’re done with the code, let’s test it out!

Start the program and look for some diamond ore, if it works, it will show a message and place a torch! If not, you might have to go back and double check your code. If you want to test it faster (since it might be difficult to find diamond ore), feel free to go in creative mode and place some ore blocks yourself!

Once you have this working, see if you can add on more than one block to check for. Maybe you can have it find gold or even emerald!