Minecraft NFC

Once the PN532 Python library is installed you're ready to make NFC tags that build blocks in Minecraft!

There are two Python scripts included in the PN532 examples.  One is mcpi_write.py and it's used to write a Minecraft block type to a MiFare classic card.  The other script is mcpi_listen.py which listens for Minecraft blocks swiped against the PN532 and creates them in a running Minecraft game.

Since you'll be running Minecraft on the Pi you'll want to make sure the Pi is connected to a monitor and has a mouse and keyboard connected to it.  If your Pi isn't connected to these devices yet, power it down and connect them, then power it back up so the monitor, keyboard, and mouse are working.  Instead of connecting to the Pi over a network connection with SSH, use the mouse, keyboard, and monitor to directly control the terminal on the Raspberry Pi.

If your Raspberry Pi doesn't automatically boot to the graphical X windows environment then login to the Pi and run the following command to start the graphical environment:

Download: file
startx

Now open a terminal in the X windows environment by clicking the Raspberry Pi Menu in the upper left and drilling in to the Accessories -> Terminal submenu.  This terminal will be used to run the python scripts that follow.

Writing Minecraft Blocks to NFC Cards

Start by running the following commands to navigate to the PN532 examples folder and start the mcpi_write.py script:

Download: file
cd ~/Adafruit_Python_PN532/examples
sudo python mcpi_write.py

Once the script starts running it will walk you through choosing a Minecraft block type and writing it to a MiFare classic card.  The first thing the script will display is a message telling you to place a card on the PN532:

Place a NFC tag / card on the PN532 and you should see the program detects it and moves to the next step:

Notice the warning to not remove the card until you have finished writing.  If the card is removed during the writing process it could be corrupted!

Now you need to pick a block type to write to the card.  At the menu you can type L and press enter to have a list of block types displayed.  The complete list looks like:

Download: file
Number	Block name
------	----------
     0	Air
     1	Bed
     2	Bedrock
     3	Bedrock, invisible
     4	Bookshelf
     5	Brick block
     6	Cactus
     7	Chest
     8	Clay
     9	Coal ore
    10	Cobweb
    11	Cobblestone
    12	Crafting table
    13	Diamond ore
    14	Diamond block
    15	Dirt
    16	Door, iron
    17	Door, wood
    18	Farmland
    19	Fence
    20	Fence gate
    21	Fire
    22	Flower, yellow
    23	Flower, cyan
    24	Furnace, inactive
    25	Furnace, active
    26	Glowstone block
    27	Gold block
    28	Gold ore
    29	Glass
    30	Grass
    31	Grass, tall
    32	Glass pane
    33	Glowing obsidian
    34	Gravel
    35	Ice
    36	Iron block
    37	Iron ore
    38	Ladder
    39	Lapis lazuli ore
    40	Lapis lazuli block
    41	Lava, flowing
    42	Lava, stationary
    43	Leaves
    44	Melon
    45	Moss stone
    46	Mushroom, brown
    47	Mushroom, red
    48	Nether reactor core
    49	Obsidian
    50	Redstone ore
    51	Sand
    52	Sandstone
    53	Sapling
    54	Snow
    55	Snow block
    56	Stairs, wood
    57	Stairs, cobblestone
    58	Stone
    59	Stone, brick
    60	Stone slab
    61	Stone slab, double
    62	Sugar cane
    63	Torch
    64	TNT
    65	Water, flowing
    66	Water, stationary
    67	Wood
    68	Wood planks
    69	Wool

For example to turn the card into a TNT block enter the number for TNT, 64, at the prompt and press enter.  You should see a new menu appear that lists a few sub-types for the TNT block:

Some blocks, like this TNT block, have a subtype that you can choose.  Other blocks have no subtype and will skip straight to the next step.

For the TNT block choose the 'Ready to explode' subtype with number 1 so that the block is active and can be exploded by swiping it with the sword in the game.  Type 1 and press enter to choose the ready to explode subtype.  Now you should see a confirmation screen like:

Now you need to confirm writing the TNT block to the card.  Type Y and press enter to confirm the write (or type N and enter to abort the write and quit).  

Don't worry if you accidentally write the wrong block type to a card.  You can rewrite a new block type to a card at any time by running the mcpi_write.py script again.

After confirming the write you should see a successful card write message like below:

That's all you need to do to write a block type to a card!  After the program quits you can safely remove the card from the PN532 breakout.

If you see an error message during the write, carefully check that the card is directly on top of the PN532's antenna and try running the mcpi_write.py script again.

You can run the mcpi_write.py script again to write other cards, or to change the block type written to a card.  Note that a card can only have one block type written to it at a time.  Also don't use NFC cards that have important data as it will likely be overwritten by the script!

Using Minecraft NFC Blocks

Now for some fun, let's run a script to start using the NFC blocks written in the previous step in Minecraft.  First you'll want to run the mcpi_listen.py script to start the program that will listen for NFC blocks.  Run the following in the same terminal you were using to run the previous script:

Download: file
sudo python mcpi_listen.py

You should see the script display a message that it's waiting for a card to be swiped:

Even though Minecraft isn't yet running, try swiping a NFC card that was written earlier.  You should see a message displayed with the block type and a warning that Minecraft isn't running:

Now run Minecraft on the Pi to test out the block in the game.  With the terminal running the mcpi_listen.py script still open, click the Raspberry Pi menu in the upper left and drill down to the Games -> Minecraft Pi item.  You should see Minecraft start to load on the screen.

Once Minecraft loads click the Start Game button to start a game and either choose an existing world or create a new one to start the game.  

If you're new to Minecraft or the Raspberry Pi version of Minecraft be sure to read this tutorial on how to play.  Experienced Minecraft players will want to note that the Pi edition of Minecraft only has the creative / building mode and not the survival mode with enemies and combat.

Once you're inside a Minecraft world and can see the player's view you're ready to try swiping one of the NFC cards with a block written to it.  If you place a NFC card on the PN532 you should see your player start to move into the air.  Look directly down at the player's feet and you'll see blocks are being created where the player is standing.  Try moving around the world and you'll see blocks created along your path.  Pull the card off the PN532 and blocks will stop being created.

Woo hoo, now you can create blocks in Minecraft by just swiping a NFC card!  Try swiping other cards with different block types to see those blocks created in the Minecraft world.  If you created ready to explode dynamite blocks then try hitting them with the sword a few times to make them flash and explode.  Let your creativity flow by using NFC blocks to build amazing Minecraft creations!

When you're done playing Minecraft exit out of the program and return to the terminal that's running the mcpi_listen.py script.  Press Ctrl-C to stop the script from running.  Remember if you want to create blocks with NFC in future Minecraft games you must have the mcpi_listen.py script running the background.

Finally to complete the Minecraft building experience try making small papercraft Minecraft blocks that contain NFC tags.  I found this page allows you to build a Minecraft block that can be printed, cut out, and glued or taped together to make a papercraft block.  One thing to note is that the page doesn't seem to have an easy to use printing interface so you might need to take a screenshot of your completed block pattern, crop it in a graphic editor, and print the patten blown up on a piece of paper.  Below you can see a few blocks I printed, cut out, and glued together with NFC cards inside:

Remember NFC cards have a small range and can even be embedded in plastic or other material (but not metal!).  Try putting a NFC card inside a toy or other container to turn it into an object that interacts with Minecraft!

This guide was first published on Mar 24, 2015. It was last updated on Mar 24, 2015. This page (Minecraft NFC) was last updated on Apr 20, 2019.