Diamonds
In [1]:
# Some commands for better formatting in the website, no need to look at this
from IPython.display import Image
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))

Turn blocks into diamods

After completing the Hello world! example which introduces the very basics, we're going to look at a more advanced example which will turn existing blocks in the world into Diamond blocks.

To connect and interact with Minecraft we will use the mcpi library, so make use the mcpi folder you downloaded from RaspberryJam Mod's website is present in the same directory as this notebook. As an example, let's say you have the following folders:

  • Desktop
    • My Scripts
      • mcpi
      • Diamonds.ipynb <---- this notebook!

Let's start coding!

1. Get Minecraft ready

It is a good idea to tell Minecraft not to go into Settings Menu when you switch to another window, for example this browser, using maybe alt+tab. To do so, go to your Minecraft installation folder and edit the options.txt file. Find the pauseOnLostFocus value and replace true by false. Now your Minecraft game won't go into Settings Menu when you are writing your code here, and you can see everything in real time!

Now open your Minecraft game with RaspberryJam and PythonTool mods loaded, and load your favourite world, both creative or survival modes work great!

2. Load the required libraries

We have to tell Python which libraries we're going to use, so that it can import them.

In [2]:
import mcpi.minecraft as minecraft
import mcpi.block as block

If executing the previous cell gives you errors, it means that the mcpi folder is not in the same directory as this notebook, so Python cannot find it! Make sure you've placed it there.

3. Establish a connection to your loaded Minecraft world

Now that your world is up and running, and you have the mcpi library loaded in Python, we can make a connection to your game!

In [3]:
mc = minecraft.Minecraft.create()

When you execute the previous cell, it should connect without any complains or output. If an error is thrown, check that your Minecraft game has RaspberryJam and PythonTool mods loaded, and that you have opened a world.

4. Find your player's position in the world

We will obtain the player's coordinates:

In [4]:
position = mc.player.getPos()
print(position)
Vec3(-96.39792077711516,7.0,-63.126090997433664)

So my player is standing at position

  • x = -96.39792077711516
  • y = 7.0
  • z = -63.126090997433664

Your coordinates will vary, as your player is standing in a different place in a completely different world!

The coordinates of the block just underneath the player will have the same coordinates, but the height coordinate will be one unit smaller. The coordinate in charge of height is the number in the middle, coordinate y.

In [5]:
position.y = position.y - 1
print(position)
Vec3(-96.39792077711516,6.0,-63.126090997433664)

5. Turn the block you're stading on into a Diamond block

Remember that we have that block's coordinates stored in position. So we tell Python to turn the block placed at position into a nice Diamond block! We can also tell it to write something to the chat window to confirm the changes:

In [6]:
mc.setBlock(position.x, position.y, position.z, block.DIAMOND_BLOCK)
mc.postToChat("Look down! A shiny new Diamond appeared")

A Diamond block should have appeared under your player. If you have moved the player after getting its position, the Diamond block has been placed at those old coordinates, so have a look around!

Empty ground Diamond ground

6. Surround your player with diamonds!

We can place as many Diamond blocks as we desire, not only underneath the player. Let's build a Diamond flooring around the player.

First, let's obtain the player's coordinates again in case we have moved:

In [7]:
position = mc.player.getPos()

We will use 9 Diamond blocks in total, creating a 3x3 square in the ground just below us. In this case, we will subtract 1 from the height coordinate directly when placing the blocks, and we will also move the other two coordinates to complete the square:

In [8]:
mc.setBlock(position.x - 1, position.y - 1, position.z - 1, block.DIAMOND_BLOCK)
mc.setBlock(position.x - 1, position.y - 1, position.z, block.DIAMOND_BLOCK)
mc.setBlock(position.x - 1, position.y - 1, position.z + 1, block.DIAMOND_BLOCK)
mc.setBlock(position.x, position.y - 1, position.z - 1, block.DIAMOND_BLOCK)
mc.setBlock(position.x, position.y - 1, position.z, block.DIAMOND_BLOCK)
mc.setBlock(position.x, position.y - 1, position.z + 1, block.DIAMOND_BLOCK)
mc.setBlock(position.x + 1, position.y - 1, position.z - 1, block.DIAMOND_BLOCK)
mc.setBlock(position.x + 1, position.y - 1, position.z, block.DIAMOND_BLOCK)
mc.setBlock(position.x + 1, position.y - 1, position.z + 1, block.DIAMOND_BLOCK)

Diamond floor

Exporting all this code as a Python script to use with PythonTool-Mod

The Jupyter Notebook is a great way to test and explore the possibilities Python offers to make Minecraft even cooler, but once you have your code ready and working, you'll want to put it together as a standard Python script so that you can use it in-game with PythonTool-Mod.

Luckily, this is very easy! Just click on File, then Download as and select Python (.py).

It will download your Python script to your Donwloads folder, as with any other file downloaded form the Internet. Just move this file to your script folder and you will find it inside the Computer Block in Minecraft, ready to be executed with one click!