23,99 €
Minecraft is a sandbox game that allows you to play it in any way you want. Coupled with a multiplayer server powered by Spigot, you can customize the game even more! Using the Bukkit API, anyone interested in learning how to program can control their Minecraft world by developing server plugins.
This book is a great introduction to software development through the wonderful world of Minecraft.
We start by instructing you through how to set up your home PC for Minecraft server development. This includes an IDE complete with the required libraries as well as a Spigot server to test on. You will be guided through writing code for several different plugins. Each chapter teaches you new skills to create plugins of increasing complexity, and each plugin adds a new concept of the Bukkit API By the end of the book, you will have all the knowledge you need about the API to successfully create any type of plugin. You can then practice and build your Java skills through developing more mods for their server.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 200
Veröffentlichungsjahr: 2015
Copyright © 2015 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: September 2013
Second edition: December 2015
Production reference: 1151215
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78588-302-6
www.packtpub.com
Cover Image by Cody M. Sommer
Author
Cody M. Sommer
Reviewers
Thomas E. Enebo
Pat Patterson
Commissioning Editor
Veena Pagare
Acquisition Editor
Vinay Argekar
Content Development Editor
Divij Kotian
Technical Editor
Mohit Hassija
Copy Editor
Vedangi Narvekar
Project Coordinator
Neha Bhatnagar
Proofreader
Safis Editing
Indexer
Priya Sane
Graphics
Kirk D'Penha
Production Coordinator
Shantanu N. Zagade
Cover Work
Shantanu N. Zagade
Cody M. Sommer graduated from SUNY Brockport with a bachelor's degree in computer science. During his time in college, he practiced his Java techniques by writing code for Bukkit plugins. The Bukkit project provided Cody with a fun way to develop software and continue to learn new things. After a few years, Cody authored the first edition of Building Minecraft Server Modifications in order to share his knowledge with the world. To this day, he creates new and exciting plugins for Minecraft servers. Cody has developed public plugins that are available for download, such as PhatLoots. He is also a private developer for servers such as ProspectMC. He even makes custom plugins for Minecraft events, which are hosted by him at his local library.
I would like to thank the CraftBukkit and Spigot community for making the Bukkit API available to developers for several years. They volunteer their time to update and improve the API so that others can use it to create wonderful things.
I would also like to thank my wife and daughter for supporting me while writing this book. Thank you for being patient with me while I spent busy nights typing away in my office. Teaching young people new skills through Minecraft is a passion of mine and I am glad that you understand that. I love you both.
Thomas E. Enebo is the co-leader of the JRuby project and author of the Minecraft plugin project, Purugin. He has been a practioner of Java since the heady days of the HotJava browser, and he has happily been using Ruby since 2001. Thomas has spoken at many Java and Ruby conferences, co-authored Using JRuby, and won the Ruby Hero award. He was awarded the "Rock Star" award at JavaOne. When Thomas is not coding, he enjoys jogging, reading, and drinking a nice India pale ale (IPA).
Pat Patterson has been working with Internet technologies since 1997. He has built software and worked with developer communities at Sun Microsystems, Huawei Technologies, and Salesforce. At Sun, Pat was best known as the community lead for the OpenSSO open source project. At Huawei, he worked on cloud storage infrastructure software.
Since joining the developer evangelism team at Salesforce in late 2010, Pat has worked with all aspects of what is now the Salesforce App Cloud, developing a focus on identity, integration, and the Internet of Things. Describing himself as an "articulate techie", Pat has coded everything from Linux kernel drivers to a Salesforce/Minecraft integration (seriously, you can Google it!), written many online articles, and spoken at conferences on five continents.
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books.
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.
This book is an introduction to programming Minecraft server plugins with the Bukkit API. Minecraft is a very versatile sandbox game, and players are always looking forward to doing more with it. Bukkit allows programmers to do just that. This book is geared towards individuals who may not have a programming background. It explains how to set up a Bukkit server and create your own custom plugins that can be run on the server. It starts with the basic features of a Bukkit plugin, such as commands and permissions, and continues towards advanced concepts, such and saving and loading data. This book will help readers create a complete Bukkit plugin, irrespective of whether they are new to Java or just new to Bukkit. The advanced topics cover portions of the Bukkit API that could even aid the current plugin developers in expanding their knowledge in order to improve their existing plugins.
Chapter 1, Deploying a Spigot Server, instructs readers how to download and set up a Minecraft server that runs on Spigot, which includes the forwarding of ports to allow other players to connect. In this chapter, common server settings and commands are explained as well.
Chapter 2, Learning the Bukkit API, introduces Bukkit by teaching how to read its API documentation. In this chapter, common Java data types and Bukkit classes are discussed.
Chapter 3, Creating Your First Bukkit Plugin, guides readers through installing an IDE and creating a simple "Hello World" Bukkit plugin.
Chapter 4, Testing on the Spigot Server, discusses how to install a plugin on a Spigot server as well as simple testing and the debugging of techniques.
Chapter 5, Plugin Commands, instructs how to program user commands into a server plugin by creating a plugin called Enchanter.
Chapter 6, Player Permissions, teaches how to program permission checks in a plugin by modifying Enchanter. This chapter also guides readers through installing a third-party plugin called CodsPerms.
Chapter 7, The Bukkit Event System, teaches how to create complex mods that use event listeners. This chapter also helps readers learn by creating two new plugins, namely NoRain and MobEnhancer.
Chapter 8, Making Your Plugin Configurable, teaches readers program configuration by expanding MobEnhancer. This chapter also explains static variables and communication between classes.
Chapter 9, Saving Your Data, talks about how to save and load program data through the YAML file configuration. This chapter also helps create a new plugin called Warper.
Chapter 10, The Bukkit Scheduler, explores the Bukkit Scheduler while creating a new plugin called AlwaysDay. In this chapter, Warper is also modified to incorporate scheduled programming.
In order to benefit from this book, you will need a Minecraft account. The Minecraft game client can be downloaded for free, but an account must be bought at https://minecraft.net/. The other software that is used in this book includes the Spigot server.jar (this is different from the normal Minecraft server.jar) and an IDE, such as NetBeans or Eclipse. This book will walk you through the process of downloading and installing both the server and the IDE on a Windows PC.
This book is great for anyone who is interested in customizing their Minecraft server. Even if you may be new to programming, Java, Bukkit, or even Minecraft itself, this book has you covered. All that you need is a valid Minecraft account. This book is a great introduction to software development. If you have no prior knowledge of Java or writing software, you can visit http://codisimus.com/learnjava for some introductory teaching to prepare yourself for working through the chapters of this book. If you are interested in programming as a career or hobby, this book will get you started. If you are simply interested in playing Minecraft with your friends, then this book will help you make that experience even more enjoyable.
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "<java bin path> should be replaced with the location of the Java installation. The java path depends on the version of Java that you have on your computer."
A block of code is set as follows:
Any command-line input or output is written as follows:
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Open the File menu and click on New Project...."
Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <[email protected]>, and mention the book's title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/BuildingMinecraftServerModificationsServerSecondEdition_ColorImages.pdf.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at <[email protected]>, and we will do our best to address the problem.
The first step to modifying Minecraft with the Bukkit API is to install a multiplayer server on your Windows PC. A multiplayer server is essentially the same as the single-player Minecraft server, but it allows for more customization and is not limited to the people in your home network. Spigot is a modified version of a Minecraft server, which will be used to load the plugins that you create. A plugin is a piece of software that hooks or plugs into another piece of software. The code that you will develop in this book will be in the form of plugins. These plugins will hook into the Minecraft code and change how Minecraft operates. We will set up a Spigot server and use it to test the plugins that you will write. By the end of this chapter, all of your friends will be able to log on to your modified Minecraft server and play together. By working through the following segments of this chapter, we will deploy a Spigot server, which will be modified in the later chapters:
As you set up your own server and begin to create plugins, you will encounter a few terms that may be new to you. The terms are Vanilla, Bukkit, CraftBukkit, and Spigot.
Vanilla refers to the normal Minecraft game developed by Mojang/Microsoft. The Vanilla Server is the official version of the game. It can be downloaded from minecraft.net and is typically named minecraft_server.jar or minecraft_server.exe. The vanilla server currently does not support any sort of mods or plugins. This is where Bukkit comes in.
Bukkit is an API that helps us to develop plugins. We will discuss this in detail in Chapter 2, Learning the Bukkit API. Until then, it is sufficient to know that when you hear the phrase bukkit plugins, it is referring to the plugins that are built against the Bukkit API.
The Bukkit API was originally developed by the CraftBukkit team. This brings us to the next term. CraftBukkit is a modified Minecraft server that replaces the vanilla server. CraftBukkit and vanilla Minecraft provide us with essentially the same game. The difference is that CraftBukkit has the ability to load Bukkit plugins and execute the code within the game. CraftBukkit translates the Bukkit methods and variables into Minecraft code, which was developed by Mojang. CraftBukkit also includes additional code to aid plugin developers with completing certain tasks, such as saving/loading data, listening for server events, and scheduling the code that needs to be executed. We will not mention CraftBukkit much in this book, because it has been replaced with a project named Spigot.
Spigot completely replaces the vanilla Minecraft server, just as CraftBukkit does. Spigot was built on top of the CraftBukkit project. Therefore, they share a lot of the same code. However, Spigot is more configurable via its settings; in many ways, it is much faster. The Spigot team now maintains all three of the projects, namely Bukkit, CraftBukkit, and Spigot. You will be able to use either CraftBukkit or Spigot to run a server, since the Spigot team has been kind enough to provide us with both. I recommend running the Spigot server for the reasons mentioned earlier.
We will start from scratch to set up this new server. If you wish to use a preexisting world, you will be able to do so after creating a new Spigot server. To start, let's create a new empty folder named Bukkit Server. We will run the Spigot server from this newly created folder.
The main file that you will need to start a new server is spigot.jar. A JAR file is an executable Java file. Minecraft, Spigot, and every plugin that we will create are all written in Java and therefore are run from a JAR file. The Spigot team updates the spigot.jar file as Mojang releases new versions of Minecraft. Typically, when connecting to a Minecraft server, you must be playing on the same version. In case you are unsure of your Minecraft version, it is displayed in the bottom-left corner of the Minecraft client. A client is the program that you use to play Minecraft, as shown in the following screenshot:
You can choose the version of Minecraft that you want to play by creating a new profile in the Minecraft launcher, as shown in the following screenshot:
For legal reasons, the Spigot team is not allowed to let you download spigot.jar. However, it does provide you with tools and instructions so that you can build the JAR file yourself. The Spigot team continues to improve this process by providing the latest instructions as well as a troubleshooting guide at https://www.spigotmc.org/threads/buildtools-updates-information.42865/. This chapter includes simplified instructions on how to obtain the needed JAR file. However, in case you run into problems while building these jar files, refer to the instructions provided on spigotmc.org.
You will need Git for Windows in order to run the build tools. You can download it from http://msysgit.github.io/. When installing Git for Windows, the default installation settings will be fine. You will also need to download the build tools JAR file, which can be found at https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar. Create a new folder to place BuildTools.jar in. Name this folder Spigot Build Tools. Also create a new text file within the same folder. Name this text file update.txt. Open the text file and write the following line of code in it:
<java bin path> should be replaced with the location of the Java installation. The java path depends on the version of Java that you have on your computer. Look for the Java folder inside the Program Files or Program Files (x86) directory. The Program Files directory can be found at the root of the main hard drive, which is typically C:\. If you do not see the Java folder, then you will have to install Java. You can download it at https://java.com/download/.
Once you are in the Java folder, you will see one or more java installation folders, such as jre7 or jre8. Open the installation folder. In case you see more than one, open the one with the higher version number. Within the java installation folder, open the bin directory. Here you should see java.exe, though it may be displayed as simply java. Copy the path from the top of the explorer window; this is the java bin path, as shown in the following screenshot:
If you have Java 8, then the line of code in the update file will be similar to the following code:
On most PCs, you can reference the java variable in place of the java.exe path. Therefore the line of code in the update file will be as follows: java -jar BuildTools.jar
Save the file and then rename it from update.txt to update.sh. If you don't see the .txt extension on the file, then you will have to adjust your folder options by performing the following steps:
Now, you can rename update.txt to update.sh. Run update.sh by double-clicking on it. This will execute the build tools, downloading all the code and applying changes until it is up to date. It will take several minutes to complete. Once it's complete, you will have spigot.jar, craftbukkit.jar, and bukkit.jar. The two server jars, namely spigot and craftbukkit, will be found in the Spigot Build Tools directory, where you placed BuildTools.jar. The bukkit.jar file is located in the Bukkit/target directory in the same folder. Each file will have a version number appended to it, such as spigot-1.8.8.jar and bukkit-1.8.8-R0.1-SNAPSHOT.jar
