Praise for This Book A handful of boilerplate and about five lines of custom code, and suddenly, explod- ing arrows! All of a sudden, doing something cool in the context of a fully realized 3D game engine is very easy. And that’s how programming has to be learned…in easy, bite-sized chunks. ➤ Carl Cravens Linux system architect and Minecraft dad Learn to Program with Minecraft Plugins explains things very well—no programming experience required. It’s very helpful for new programmers. And so far, it’s been an excellent vehicle for some quality father-son bonding. ➤ Mel Riffe, Minecraft dad, and Noah Riffe, age 12 Phenomenal. Approachable and simple, without talking down to the audience. I could see anyone at any age reading this. ➤ David Bock, age 44 Well, first off, this is a wonderful book. The way it is written is amazing. I am learning BASH and Java! I’ve tried to learn BASH, but all the other books I’ve found are just way too hard. Not only that, but modding Minecraft while running a server? EPIC! Thank you for writing this book! ➤ Jack H. Age 13 I really liked making the server plugins. My favorite was the cow shooter. ➤ Jonathan Knowles, age 13 Go, you—this book is awesome! ➤ Stina Qvarnström Developer, Bool Noridc AB, Sweden Sssssssssss ➤ A creeper Learn to Program with Minecraft Plugins, nd 2 Edition Create Flaming Cows in Java Using CanaryMod Andy Hunt The Pragmatic Bookshelf Dallas, Texas • Raleigh, North Carolina Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trade- marks of The Pragmatic Programmers, LLC. Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein. Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://pragprog.com. The team that produced this book includes: Brian Hogan (editor) Potomac Indexing, LLC (indexer) Liz Welch (copyeditor) Dave Thomas (typesetter) Janet Furlow (producer) Ellie Callahan (support) For international rights, please contact [email protected]. Copyright © 2014 The Pragmatic Programmers, LLC. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. Printed in the United States of America. ISBN-13: 978-1-941222-94-2 Encoded using the finest acid-free high-entropy binary digits. Book version: P1.0—November 2014 Contents Acknowledgments . . . . . . . . . . . ix Start Here . . . . . . . . . . . . . xi 1. Command Your Computer . . . . . . . . . 1 Use the Command Line 2 Move Around in File Directories 3 Start at the Desktop 11 Common Commands 13 Next Up 13 2. Add an Editor and Java . . . . . . . . . . 15 Install an Editor to Write Code 15 Install the Java Programming Language 18 If the Java Command Is Not Found 20 Other Reasons It Might Not Work 22 Install the Minecraft Client and Server 23 Next Up 29 3. Build and Install a Plugin . . . . . . . . . 31 Plugin: HelloWorld 33 Configure with Canary.inf 35 Build and Install with build.sh 36 Using EZPlugin 39 Next Up 42 4. Plugins Have Variables, Functions, and Keywords . . . 43 Keep Track of Data with Variables 44 Plugin: BuildAHouse 46 Plugin: Simple 52 Organize Instructions into Functions 54 Use a for Loop to Repeat Code 60 • Contents vi Use an if Statement to Make Decisions 61 Compare Stuff with Boolean Conditions 62 Use a while Loop to Repeat Based on a Condition 64 Next Up 65 5. Plugins Have Objects . . . . . . . . . . 67 Everything in Minecraft Is an Object 67 Why Bother Using Objects? 69 Combine Data and Instructions into Objects 70 Plugin: PlayerStuff 75 Next Up 77 6. Add a Chat Command, Locations, and Targets . . . . 79 How Does Minecraft Know About Your Plugin? 79 Plugin: SkyCmd 80 Handle Chat Commands 81 Use Minecraft Coordinates 82 Find Nearby Blocks or Entities 84 Plugin: LavaVision 84 Next Up 87 7. Use Piles of Variables: Arrays . . . . . . . . 89 Variables and Objects Live in Blocks 89 Plugin: CakeTower 92 Use a Java Array 94 Plugin: ArrayOfBlocks 96 Use a Java ArrayList 98 Plugin: ArrayAddMoreBlocks 101 Next Up 102 8. Use Piles of Variables: HashMap . . . . . . . 105 Use a Java HashMap 105 Keep Things Private or Make Them Public 108 Plugin: NamedSigns 109 Next Up 115 9. Modify, Spawn, and Listen in Minecraft . . . . . . 117 Modify Blocks 117 Plugin: Stuck 118 Modify Entities 123 Spawn Entities 124 Plugin: FlyingCreeper 125 • Contents vii Listen for Events 126 Plugin: BackCmd 128 Check Permissions 133 Next Up 134 10. Schedule Tasks for Later . . . . . . . . . 135 What Happens When? 135 Put Code in a Class by Itself 137 Make a Runnable Task 138 Schedule to Run Later 139 Schedule to Run Once, or Keep Running 140 Plugin: CowShooter 140 Next Up 143 11. Use Configuration Files and Store Game Data . . . . 145 Use a Configuration File 146 Plugin: SquidBombConfig 149 Store Game Data in a Database 151 Plugin: LocationSnapshot 154 Plugin: BackCmd with Save 158 Next Up 167 12. Keep Your Code Safe . . . . . . . . . . 169 Install Git 169 Remember Changes 170 An Easy Undo 173 Visit Multiple Realities 177 Back Up to the Cloud 180 Share Code 183 Next Up 184 13. Design Your Own Plugin . . . . . . . . . 187 Have an Idea 188 Gather Your Materials 188 Lay Them Out 190 Try Each Part 193 Knit It All Together 201 Just the Beginning 209 A1. How to Read Error Messages . . . . . . . . 211 Java-Compiler Error Messages 211 Canary Server Error Messages 216 • Contents viii A2. How to Read the Canary Documentation . . . . . 217 Canary JavaDoc Documentation 217 Oracle JavaDoc Documentation 218 The Wiki and Tutorials 219 A3. How to Install a Desktop Server . . . . . . . 221 The Easy Way: LogMeIn 221 The Harder Way: By Hand 223 A4. How to Install a Cloud Server . . . . . . . . 229 What Is the Cloud? 229 Remote Operating Systems 230 Remote Access 232 Installing Packages 237 Installing Java 238 Running Remotely 239 Domain Name 240 What’s Next 240 A5. Cheat Sheets . . . . . . . . . . . . 241 Java Language 241 A6. Glossary . . . . . . . . . . . . . 247 A7. Common Imports . . . . . . . . . . . 253 Bibliography . . . . . . . . . . . . 255 Index . . . . . . . . . . . . . . 257 Acknowledgments A very special thanks to my son Stuart for suggesting this book and answering a lot of my dumb questions about Minecraft, and to the rest of my family for putting up with me as I disappeared under headphones and typed away in an imaginary world. Thanks to my editor Brian Hogan, managing editor Susannah Pfalzer, and production manager Janet Furlow, and everyone at the Pragmatic Bookshelf for helping to get this second edition finished in record time. Thanks to second edition tech reviewers, including Said Eloudrhiri, Ingo Haumann, Jack H., Dan Kacenjar, Andrés N. Robalino, and Zachary Thomas. Extra special thanks to Joshua McKinnon for his detailed and thoughtful review. Special thanks to the crew at CanaryMod, especially Jason Jones, for their support. Minecraft is ®, TM, and © 2009–2014 Mojang/Notch. CanaryMod is Copyright 2012–2014, CanaryMod Team, Under the management of PlayBlack and Visual Illusions Entertainment. All rights reserved. CanaryMod Team, PlayBlack, Visual Illusions Entertainment, CanaryLib, CanaryMod, and its contributors are NOT affiliated with, endorsed, or sponsored by Mojang AB, makers of Minecraft. The “CanaryMod” name is used with permission from FallenMoonNetwork. Photo of a toggle switch by Jason Zack at en.wikipedia report erratum • discuss