Introduction

One of the aims of MineCraft is to mine precious raw materials, but some materials are hidden deep underground.. Wouldn’t it be great if we could write a script that helped us find these raw materials?

The Challenge

Your challenge is to write a script that constantly checks down 50 blocks below your player and if there is a precious material below you post “We’ve found treasure!” in the MineCraft chat window.

Step1. Check the block below your player to see if it contains gold.

The first thing you need to do is to check if the block below you contains gold, and if it does then post ‘“We’ve found treasure!” to the chat window. To do this check we need to use an if statement. This pseudo-code should help you get started.

Set playerx, playery, playerz to player’s current position.

Get the blockID of the block below your player

If blockID equals 41

Post “We’ve found gold!”

Step 2. Check all the blocks up to 50 below you.

Okay so we can check the block below us, that’s great. But what if we want to check the 50 blocks below us? To do this we can use a for loop with the range function.

For each number in range 1 to 50:

Get the blockID of the block  playery — number

If blockID equals 41

Post “We’ve found gold!”

Step3. Put all the code in a loop to keep checking no matter where you walk.

Put it all in a loop and add a wait(sleep) in to each loop.

While True

# put your code here

Sleep for 0.1 seconds

Hint: If you don’t know how to add a wait, check out the glass bridge challenge.

Extensions

  • Post the name of the precious material to the chat window instead of treasure.
  • Tell the player how far below them the precious material is.
  • Only post to the chat window once instead of filling up the players window.
  • Use sounds instead or posting to the chat window.

 Challenge Solution

import minecraft as minecraft
import time
mc = minecraft.Minecraft.create()

treasureList = [14,15,16,41,42,49,73,89,56,57] #The block ID for Gold Ore, Diamond Ore, etc...

while True:	# A loop to keep checking
	playerx, playery, playerz = mc.player.getTilePos() # Find out where we are
	for y in range (0,50):  # build a loop to check the 50 blocks below us
		blocktype = mc.getBlock(playerx,playery -y, playerz)#find out what block is there
		if blocktype in treasureList: # check if the block ID is in the treasureList
			mc.postToChat("We've found treasure!")
	time.sleep(0.01) #Only check every 100th of a second - otherwise our pi will freeze...

 

 

Recommended reading