Published in 2020 by The Rosen Publishing Group, Inc.
29 East 21st Street, New York, NY 10010
Copyright 2020 by The Rosen Publishing Group, Inc.
First Edition
All rights reserved. No part of this book may be reproduced in any form without permission in writing from the publisher, except by a reviewer.
Library of Congress Cataloging-in-Publication Data
Names: Romphf, Joshua, author.
Title: Code a Minecraft mod in JavaScript step by step / Joshua Romphf. Description: First edition. | New York : Rosen Publishing, 2020. | Series: Coding projects for all | Includes bibliographical references and index. Identifiers: LCCN 2018057373| ISBN 9781725340206 (library bound) | ISBN 9781725340152 (pbk.)
Subjects: LCSH: Computer gamesProgrammingJuvenile literature. | JavaScript (Computer program language)Juvenile literature. | Minecraft (Game)Juvenile literature.
Classification: LCC QA76.76.C672 R645 2020 | DDC 794.8/1525 dc23 LC record available at https://lccn.loc.gov/2018057373
Manufactured in the United States of America
Minecraft is a trademark of Mojang (a game development studio owned by Microsoft Technology Corporation), and its use in this book does not imply a recommendation or endorsement of this title by Mojang or Microsoft.
CONTENTS
INTRODUCTION
M inecraft. You know it. You love it. And youre not alone! Since Markus Notch Persson and his company, Mojang, officially released the game in 2011, it has rocketed to the top of the gaming worldand stayed there for longer than anyone ever thought possible.
How does a game stay so popular for so long? One answer is simple: great gameplay. Another answer? The modding community. Minecraft is a game based on infinite creativity, and modifying the game to make custom blocks, new levels, or anything elsehas kept the experience fresh and fun for tens of millions of loyal fans.
What better way to learn about computer science than working with your favorite game? There are some amazing tools out there that let beginner programmers (hint: thats you) make awesome mods for Minecraft. Luckily enough, you hold in your hands the key to unlocking those tools and using your creativity to code and publish your very own Minecraft mods using the programming language JavaScript.
The first section will mostly contain pen-and-paper or pseudocode exercises, covering the more language-agnostic concepts well be using throughout the book. Woah, woah, I know throwing around phrases like language-agnostic this early is kind of scary. What that means, really, is that these concepts can be applied to many different programming languages not just JavaScript. Im a firm believer that the fundamentals of programming are useful no matter what language (or languages!) you learn. If youre able to learn the ins and outs of one languagelike JavaScriptits going to be a lot easier when you try to grasp the nuances and features of another. So with this in mind, were going to go ahead and start with some baseline building blocks. Well go through a brief introduction on how computers and programming languages work, followed by some language fundamentals, such as syntax and semantics. Then, well start to think about all of the different types of data that our programs work with. Finally, well get into the core logical concepts of programs and how they flow. Get your pen and paper ready!
Dont worryonce youre a master of the basics, well get into making your first awesome mod. The next section will show you the ropes of JavaScript more specifically. Youll learn about classes, variables, and more. Im also going to give you a rundown on using ScriptCraft, a custom tool for translating all your new JavaScript knowledge into Minecraft.
is where the fun really begins. Here, youll start putting your knowledge to good use, making a mod I like to call the Cellar Dweller. This mod has it all: randomly generated dungeons, awesome enemies, and some pretty sweet loot to boot. Im also going to walk you through publishing and documenting all your hard work for all the world (or just your friends) to see.
Its going to be a wild ride!
Chapter One
HOW PROGRAMMING LANGUAGES WORK
B efore you can get started with programming exercises and building awesome stuff with JavaScript, its probably a good idea to know a little bit about how programming languages work in general.
You know what a computer isyou probably use one every day! But do you ever stop to think about how it works? At a basic level, computers are electromechanical machines that can take instructions from humans and do a lot of different things with them. People write programs or softwareto communicate with the electromechanical pieces, or hardware. Everything you do on a computer has some sort of programming behind it, from surfing the internet, to sending an e-mail, to playing Minecraft.
Though theyre capable of doing some unbelievable stuff, computers can understand only very straightforward and limited instructions. In fact, they can understand only 1s and 0s. This is called binary. It works like this: when a computer sees a 1, it knows to turn an electrical pulse on; when it sees a 0, it knows to turn that electricity off. Based on the order and length of these binary instructions, the hardware does different things.
The instructions that are sent directly to the computers central processing unit, or CPU, are known as machine code. Machine code is the lowest form of binary communication between a human and a computer. That doesnt mean its bad in computer science, the lower a code is, the closer it is to machine code. The higher it is, the closer it is to human language.
Why does this matter to you? Because Im sure you dont really want to type 01101000 01100101 01101100 01101100 01101111 just to say hello! In the early days of digital computing, though, programmers had to write instructions for their hardware just like thisusing binary. These instructions were actually punched onto a piece of paper and fed into the computer. Phew, sounds awful, doesnt it?
When people wanted to execute complex tasks on a computer, they realized that they would need to use languages that were easier for humans to read and write. Only supernerds would really want to type everything in machine code. The result was Assembly Language, which is a set of more human-readable instructions that are converted to machine code. Still, early computer scientists wanted more. After all, why should a human have to learn the language of a machine when he or she could just design the machine to understand human language?
Most software developers and programmers use higher-level languages that are closer to human, or natural, languages. That allows them to do more complicated things while typing significantly less code. Think about it like this: you could probably write a lot using English, but what if I asked you to do it in Latin?
High-level code might be easy for humans to understand, but a CPU can read only machine code. So how does the language get translated? Thats where interpreters and compilers come in.
A compiler directly translates high-level code to native machine code. That means it produces code that can communicate directly with the CPU. Compiled code is usually faster for this reasonthe distance between the code and the processor is pretty small!
On the other hand, an interpreter will compile the code to an intermediate format thats called byte code. Then, it will execute each line as machine code at runtime (when the program is actually running), versus at compile time (when the program is being compiled, or translated).
Next page