INTRODUCTION

“Just three more diamonds, and then I’ll stop,” I remember telling myself while playing Minecraft. I needed the diamonds for a new pickaxe. I needed the pickaxe to mine more obsidian. I needed the obsidian to make a Nether portal. I needed to go to the Nether to pick up lava. And I needed the lava for . . . what did I need the lava for again? Oh, right. I was sculpting a giant phoenix statue in the side of a mountain, and I wanted to make lava pour out from its eyes and beak. Two hours later, I was still playing, unable to pry myself away from the task at hand.

Minecraft is an addictive game. It has sold over 107 million copies, making it the second most popular video game of all time, beaten only by Tetris. It’s an open-ended, creative platform for gathering resources and building whatever you can imagine. You can build castles to protect against zombie hordes, plant crops and tend to animals, or team up with friends to build massive works of art. Minecraft appeals to a diverse set of people: children, teenagers, and even adults love playing it.

In this book, you’ll use the ComputerCraft mod (also known as CC) to turn your addiction to building into an addiction to coding. But what exactly is ComputerCraft—or a mod, for that matter?

WHAT ARE MINECRAFT MODS?

Minecraft by itself, called vanilla Minecraft, is just the beginning. Minecraft can be modified and extended by third-party software called mods (short for modifications) to provide additional features such as blocks, environments, items, monsters, and even worlds that are not included with the vanilla version of the game. Due to its popularity, Minecraft has attracted one of the largest modding communities of any video game.

These fan-made mods are free to download. Some mods add space exploration and rockets. Others add sorcery and spells. You can even find Minecraft mods for creating your own dinosaur zoos, high-speed train networks, and bee-breeding apiaries. In this book, we’ll use the ComputerCraft mod to learn how to program.

WHAT IS COMPUTERCRAFT?

ComputerCraft is a Minecraft mod that adds programmable turtles to Minecraft. These turtles can do almost anything the player can do: dig mines, chop down trees, construct buildings, craft items, plant seeds, milk cows, bake cakes, and more (see Figure 1). An army of these box-shaped turtles can automatically perform all the time-intensive chores the player usually must do manually.

Figure 1: Turtles chopping trees (left) and mining (right)

The catch is that you have to learn to program first. These turtles understand code written in Lua (moon in Portuguese), which is a programming language used by professional software developers in fields such as embedded computing systems and video game development. The interpreter software that runs Lua scripts is just 100KB, which means that it can be easily embedded inside other pieces of software, such as a Minecraft mod. Lua is often used within video game code—for example, in World of Warcraft, Dark Souls, Portal 2, Factorio, and many others.

Although Lua is simpler than other programming languages, it’s still fast and powerful. These qualities work in your favor. Lua’s simplicity makes it a good language to start with if you have no programming experience.

HOW TO USE THIS BOOK

You’ll need a purchased copy of Minecraft for Windows or macOS, the free ComputerCraft mod, and the free ATLauncher software to use this book. See Chapter 1 for all download and installation instructions.

When typing the source code from this book, do not type the line numbers at the start of each line. For example, if you saw the following line of code, you would not need to type the 9. on the left side or the one space immediately following it:

9. print('What is your name?')

You’d enter only this:

print('What is your name?')

The numbers are there just so this book can refer to specific lines in the program. They are not part of the actual program’s source code.

Sometimes you’ll also see an unnumbered ...snip... line in the code. This indicates that some code has been omitted for brevity. The ...snip... is not part of the code itself.

WHAT’S IN THIS BOOK?

After the first few chapters, which cover basic programming concepts, each chapter in this book focuses on how to write a program your turtle can run to help you survive and thrive in Minecraft. You’ll also find bonus activities that help you test your programming skills.

Here’s what you’ll find in each chapter:

Chapter 1: Getting Started with ComputerCraft helps you install and set up Minecraft and the ComputerCraft mod so you can start programming.

The Name ID Reference lists commonly used Minecraft blocks and items along with their name IDs, which you’ll use to identify block types and other items in your programs.

GETTING HELP

Minecraft famously lacks a tutorial for new players. It doesn’t have an instruction manual or even a help menu. Minecraft forces you to be responsible for your own education. You’ll need to do online research, form questions, find answers, and sometimes just do some plain old experimentation. Minecraft cultivates a growth mindset in players. Even after falling into lava or having their base blown up by a creeper, players come back determined to learn how to overcome these problems.

However, this book is about the ComputerCraft mod, not the basics of playing Minecraft. To use this book, you should already know how to do the following in Minecraft:

Mine for ore, stone, coal, wood, and other blocks

Craft a workbench and tools such as axes, shovels, torches, and pickaxes

Craft a furnace, fuel it, and then smelt ore blocks or cook meat in it

Craft stairs, ladders, chests, doors, fences, and other parts you’ll use for buildings

Plant seeds and farm food

If you don’t know how to do all of these things, don’t worry. You can teach yourself by searching online. Go to the search engine of your choice and enter the word minecraft along with what you want to learn. For example, you could use the search terms minecraft smelt ore, minecraft bake cake, or even just minecraft basic tutorial to find the information you need. You can also search for Minecraft video tutorials on websites such as https://www.youtube.com/ by using the same search terms you would use in a search engine.

ONLINE RESOURCES

You can download all the programs in this book directly from inside the Minecraft game (see “Sharing and Downloading Programs Online” on page 42 for instructions). And, although Minecraft doesn’t support copying and pasting text from outside the game, all the code and resources for this book are available for reference on its companion website, https://www.nostarch.com/codingwithminecraft/. There, you can also download the code for the bonus activities if you get stuck and want to check out the solutions! You’ll also find links to the installation files (see Chapter 1 for detailed installation instructions). If you want to explore other programs or share your code, you can do so through https://turtleappstore.com/, which is a free website for ComputerCraft scripts (see “turtleappstore.com” on page 44 for details).

WHAT YOU LEARNED

Minecraft is a gaming phenomenon that can be played in many ways and appeals to a diverse crowd of players. In this book, you’ll learn how to build more in less time using ComputerCraft, a mod that lets you program turtles in the Lua programming language. By learning to program with Lua and CC, you can automate many jobs you would otherwise have to do by yourself, including mining, farming, building, and crafting.