You are previewing A Beginner’s Guide to Writing Minecraft Plugins in JavaScript.
O'Reilly logo
A Beginner’s Guide to Writing Minecraft Plugins in JavaScript

Book Description

The book is for anyone who wants to learn how to modify Minecraft--no previous programming experience required! The book uses JavaScript, a popular programming language for creating websites and scripting.

Aimed at kids who already play Minecraft, this guide will teach coding through a series of "Recipes" (the term used in-game when crafting new objects). For example, in the game, one of the first things any player must do is create a Workbench, or Crafting Table, which will in turn enable the player to create sophisticated tools. Recipe 1 in the book (the term Recipe and Chapter is interchangeable) is 'A modding Workbench' and its ingredients are the tools the reader will need to begin modding.

The goal of each Recipe/chapter is to introduce a new JavaScript concept or expand upon a previously introduced concept. The author will present each recipe as a useful addition to the game while gently introducing programming concepts in an approachable style. This unique approach gets over the problem of introducing JavaScript in an interesting way and avoids spending 3 or 4 chapters explaining JavaScript core concepts in a vacuum.  Several Recipes will be covered, including Rolling Dice, Snowball Fight, Piggie Pandemonium, Advanced Modding, and more.

Table of Contents

  1. Title Page
  2. Copyright Page
  3. Dedication Page
  4. Acknowledgments
  5. About the Author
  6. Contents
  7. Preface
    1. Who Is This Book For?
    2. Why I Wrote This Book
  8. Introduction
    1. Why JavaScript?
    2. About the Upcoming Minecraft API
    3. Online Bonus Appendixes
  9. Part I: Building a Modding Workbench
    1. Chapter 1. Running Your Own Minecraft Server
      1. Client-Server Networking
      2. Why Run Your Own Server?
      3. Which Minecraft Server Software Should I Use?
      4. CanaryMod
      5. Installing CanaryMod
        1. Download Instructions
        2. Installing CanaryMod on Windows
        3. Installing CanaryMod on Mac OS
        4. Installing CanaryMod on Linux (Ubuntu)
      6. Agreeing to the End User License Agreement
      7. Configuring Your Server
        1. The server.cfg File
        2. The ops.cfg File
        3. Permissions
      8. Basic Server Administration Commands
        1. Starting and Stopping Your Server
      9. Connecting to Your Server
      10. Achievement Unlocked!
      11. Other Server Options
        1. Commercial Minecraft Hosting
        2. Minecraft Realms
    2. Chapter 2. Setting Up ScriptCraft
      1. What Is ScriptCraft?
      2. Installing ScriptCraft
      3. Verifying ScriptCraft Is Installed
      4. Achievement Unlocked!
    3. Chapter 3. Exploring JavaScript in Minecraft
      1. Basic Math Operations
      2. Comparing Numbers
      3. Variables
        1. Creating Variables
        2. String Variables
        3. Escaping Quotes
        4. Naming Variables
        5. Making Mistakes
        6. The var Keyword
        7. The null Keyword
        8. Declaring Multiple Variables
        9. Adding and Subtracting
        10. Data Types
      4. Functions
        1. Using Functions
        2. Writing Your Own Functions
      5. Achievement Unlocked!
      6. Summary
    4. Chapter 4. Choosing and Using a Text Editor
      1. Choosing an Editor
      2. gedit
        1. Installing gedit on Linux
        2. Installing gedit on Windows
        3. The File Browser
        4. The gedit File Browser on Microsoft Windows
        5. gedit Preferences
        6. View Preferences
        7. Editor Preferences
        8. Fonts & Colors
        9. Plugins
      3. Installing TextWrangler on Mac OS
      4. First Steps with Your Editor
        1. Create a New Folder
        2. Create a New JavaScript File
        3. Saving Your Work
      5. Summary
  10. Part II: Basic Modding
    1. Chapter 5. Your First Minecraft Plugin
      1. Hello World
      2. Achievement Unlocked!
      3. Making Your Function Reusable
      4. Private and Public Variables
      5. A Short Note About Objects
      6. Summary
    2. Chapter 6. Rolling Dice
      1. Randomness
      2. Think of a Number Between 0 and 1
      3. A Special Six-Sided Die
      4. Playing with Numbers
      5. ScriptCraft Folders
        1. The scriptcraft/plugins Folder
        2. The scriptcraft/modules Folder
      6. Modules
      7. Using Modules
      8. Calling Module Functions
      9. Troubleshooting
      10. Modules as Objects
      11. Achievement Unlocked!
      12. Digging Deeper
        1. Rounding Numbers
        2. Math
        3. The return Statement
      13. Summary
    3. Chapter 7. Multisided Die
      1. Flexible Functions
      2. Default Parameter Values
      3. Assigning to the exports Variable
      4. More on Comments
        1. Single-Line Comments
        2. End-of-Line Comments
        3. Multiline Comments
        4. Commenting Out Code
        5. Comments in This Book
        6. Commenting dice.js
      5. Achievement Unlocked!
      6. Summary
    4. Chapter 8. Greeting Players
      1. Event-Driven Programming
      2. Arrays
      3. First Steps with Events
      4. More on Modules
      5. Greeting Players in Style
      6. Achievement Unlocked!
      7. Summary
    5. Chapter 9. A Guessing Game
      1. Asking Questions
      2. The if Statement
      3. The if-else Construct
      4. The if-else-if Construct
      5. Nested Blocks
      6. Combining Conditions
      7. Logical AND
      8. Logical OR
      9. Complex Logic
      10. Summary
    6. Chapter 10. Animal Sounds
      1. The switch Statement
      2. The sounds Module
      3. Improving the Code
      4. More on Strings
      5. Summary
    7. Chapter 11. Leaderboards: More Fun with Arrays
      1. Array.sort()
        1. How to Compare Items in an Array
        2. Sorting Players by Name
        3. Sorting Players by Experience
      2. Reversing Arrays
      3. Sorting Players by Other Rules
      4. Player Statistics
      5. Displaying the Leaderboard
      6. The for Loop
      7. The while Loop
        1. Breaking Out of Loops
        2. Skipping a Turn in a Loop
        3. Infinite Loops
      8. Creating a New Command for Players
      9. The jsp Command
      10. Achievement Unlocked!
      11. Summary
    8. Chapter 12. Building a Skyscraper
      1. Building Using Drones
        1. Moving Your Drone
        2. Turning Your Drone
      2. Blueprints
      3. Blueprint Basics
        1. Modules Needed for Blueprints
      4. A Blueprint for a Skyscraper
        1. The skyscraper() Method
      5. Achievement Unlocked!
      6. Summary
    9. Chapter 13. Create a Fireworks Show
      1. The fireworks Module
      2. Deferred Execution
        1. The setTimeout() Function
      3. A Fireworks Show
        1. Me, a Name I Call Myself
      4. Canceling the Fireworks Show
      5. Summary
    10. Chapter 14. Animal Sounds Revisited
      1. Objects
        1. How to Make an Object
        2. Two Kinds of Objects
      2. Objects as Lookup Tables
        1. Why Use a Lookup Table Instead of a switch Statement?
      3. Objects and References
      4. Objects as Parameters
      5. Nested Objects
      6. JSON
      7. Achievement Unlocked!
      8. Summary
  11. Part III: Advanced Modding
    1. Chapter 15. Saving Player Preferences
      1. A Day in the Life of a Minecraft Plugin
      2. Chat Colors
      3. Tab Completion for Custom Commands
      4. Choosing Chat Color
        1. How You Count
        2. Character Codes
        3. Text Colors in Minecraft
      5. Plugin Data
        1. Loading Plugin Data
        2. Saving Plugin Data
      6. Saved Data
        1. More on JSON
      7. JSON, Persistence, and Java Objects
      8. Summary
    2. Chapter 16. Add New Recipes: The Ender Bow
      1. Crafting an Ender Bow
        1. Choosing What Materials Should Be Used
      2. Exploring the CanaryMod API
        1. Interfaces, Classes, Enums, Exceptions, and Annotations
      3. The Code
      4. Using CanaryMod Classes and Enums in JavaScript
      5. Enchantments
      6. The Recipe
        1. A new Way to Create Objects
        2. The Crafting Grid
        3. Calling Variable-Argument Java Methods from JavaScript
        4. Setting Ingredients
      7. Inheritance
      8. Summary
    3. Chapter 17. Arrows That Teleport You
      1. Making the Ender Bow Work
      2. Exploring Events
      3. The events Module and Event Packages
      4. The events.on() Function
      5. Types of Events and Event Properties
      6. Digging Deeper into Inheritance
      7. The Code
      8. More on Types
      9. JavaBeans
      10. A Note on Style
      11. Summary
    4. Chapter 18. Protecting Your Server Against Griefing
      1. Simple Protection
      2. Canceling Events
      3. How to Stop Listening for Events
      4. Prohibiting TNT
      5. Prohibiting Lava
      6. Player Plots
      7. Safe Zones
      8. Refactoring
      9. Creating Plots
      10. Claiming Plots
      11. Preventing Griefing on Plots
      12. Abandoning Plots
      13. Sharing Plots
      14. Dynamic Command Options
      15. Updating Event Handling to Accommodate Trusted Players
        1. Java Strings and JavaScript Strings
        2. Loops Within Loops
      16. Summary
    5. Chapter 19. Snowball Fight!
      1. Game Rules
      2. Logistics
      3. Keeping Score
        1. The /scoreboard Command
        2. Executing Commands from JavaScript
        3. ScriptCraft’s Scoreboard Module
      4. The Game Source Code
      5. Running the Game
      6. Allocating Teams
      7. The Game Loop
      8. Scope and Functions
      9. Listening for Snowball Hits
      10. Starting the Game
      11. Initializing and Updating the Scoreboard
      12. Displaying the Score
      13. Ending the Game
      14. Creating an Arena
        1. Protecting the Arena
      15. Making It Easy for Players to Start the Game
        1. Where Is the Player?
        2. Who Wants to Play?
        3. How Many Teams?
        4. Starting and Stopping the Game
      16. Summary
      17. Conclusion
  12. Appendix A. ScriptCraft Variables
    1. The server Variable
    2. The __plugin Variable
    3. The self Variable
  13. Appendix B. Using ScriptCraft with Other Plugins
    1. Issuing Commands
    2. Using Plugin Methods and Classes
  14. Appendix C. Events Reference
  15. Appendix D. Items Reference
  16. Appendix E. Function Declarations vs. Function Expressions
  17. Index
  18. Code Snippets