Create a Minecraft Mod with the Spigot API

Mods are special Java programs that run on a Minecraft server. They can’t be run anywhere else.
Writing a mod requires the use of the Spigot API, a set of Java programs that do all of the background work necessary for the program to function inside a Minecraft game. This chapter from Absolute Beginner's Guide to Minecraft Mods Programming, 2nd Edition demonstrates how a mod is created.

Now that you have a Spigot server for Minecraft set up and running and have installed the NetBeans integrated development environment (IDE), you’re ready to create and deploy a mod.

Mods are special Java programs that run on a Minecraft server. They can’t be run anywhere else.

Writing a mod requires the use of the Spigot API, a set of Java programs that do all of the background work necessary for the program to function inside a Minecraft game. A Java program also is called a class, so the Spigot API is called a class library.

The Spigot class library handles things like determining the (x,y,z) location of any object in the game, including a player. Everything you interact with in the game is represented in Spigot.

Before this book takes a full trip through Java, from the basics of the language into advanced features, this chapter demonstrates how a mod is created. This will give you a chance to see where all this material is headed. Many programming concepts will be unfamiliar to you, but all will be fully explained in subsequent chapters.

Creating Your First Mod

Mods are packaged as Java archive files, also called JAR files. NetBeans, the free integrated development environment from Oracle used throughout this book, automatically creates JAR files every time you build a project.

When you have finished writing a mod, you will be storing it under the server’s folder in a subfolder named plugins.

The mod you are creating is a simple one that demonstrates the framework you’ll use in every mod you create for Spigot. The mod adds a /petwolf command to the game that creates a wolf mob, adds it to the world, and makes you (the player) its owner.

CAUTION

There are two very similar terms you encounter a lot when doing Minecraft programming: mod and mob. A mod is a Java program that runs on a server to add something cool to the game. A mob is any living creature in the game, such as a creeper, chicken, cow, cave spider, or catoblepas.

Actually, there are no catoblepases in Minecraft. I was just making sure you were paying attention.

Each mod will be its own project in NetBeans. To begin this project, follow these steps:

In NetBeans, select the menu command File, New Project. The New Project Wizard opens.

In the Categories pane, select Java, and in the Projects pane, select Java Application. Then click Next.

In the Project Name field, enter PetWolf (with no spaces and capitalized as shown).

Click the Browse button next to the Project Location field. The Select Project Location dialog appears.

Find the folder where you installed the Minecraft server. Select it and click Open. The folder appears in the Project Location field.

Deselect the Create Main Class check box.

Click Finish.

The PetWolf project is created, and two folders appear in the Projects pane, Source Packages and Libraries, as shown in Figure 3.1.

Looking at this framework, the only things that will change when you use it for a different mod are the three things that refer to PetWolf because those are specific to this project:

The name of the program is PetWolf.

The argument inside label.equalsIgnoreCase("petwolf") is the command the user will type in the game to run the mod. This program implements the command /petwolf (commands in a mod are preceded by a slash (/) character). The label object, which is sent as an argument to onCommand(), is a string that holds the text of a command entered by the user.

The statement that calls log.info("[PetWolf] Howl!") sends a log message that is displayed in the Minecraft server window.

Everything a mod does when its command is entered goes at the spot marked by the comment // do something cool here. Comments are messages in a program that explain what it does to humans reading the code. They’re ignored by the computer when the program runs.

The first thing the PetWolf mod needs to do is learn more about the game world, using these three statements:

A Player object called me is the character controlled by the person playing the game.

With this Player object, you can call its getLocation() method to learn the exact spot where the player is standing. A method is a section of a Java program that performs a task. Here, the method retrieves the player’s current location as a Location object. Three things you can learn about a location are its (x,y,z) coordinates on the three-dimensional game map.

The Player object has a getWorld() method that responds with the World object that represents the entire game world.

Most of the mods you create need these three Player, Location, and World objects.

This mod creates a new mob that’s a wolf, using the spawn() method of the World object:

Wolf wolfie = world.spawn(spot, Wolf.class);

There’s a class for every type of mob in the game. The two arguments to the spawn() method are the location where the wolf should be placed and the class of the mob to create.

This statement creates a Wolf object named wolfie at the same spot as the player.

The color of the wolf’s collar is set in this statement:

cat.setCollarColor(DyeColor.PINK);

After the wolf has been created, the player becomes its owner by calling the wolf’s setOwner() method with the Player object me as the only argument:

wolf.setOwner(me);

Now you can begin typing. Put all this together by entering Listing 3.1 into the source code editor and clicking the Save All button in the NetBeans toolbar (or select File, Save).

The import statements in Lines 3–7 of Listing 3.1 make five packages available in the program: one from the Java Class Library and four from Spigot. Packages are groups of Java classes that serve a related purpose. For instance, the org.bukkit.entity package referenced in Line 6 is a group of classes for the mobs in the game (which in Spigot are called entities).

NOTE

These classes are used in the PetWolf program: Logger from java.util.logging.Logger, Location and World from org.bukkit, Command from org.bukkit.command, Wolf and Player from org.bukkit.entity, and JavaPlugin from org.bukkit.plugin.java.

You learn more about these packages as you use them to create more mods beginning in Chapter 18, “Spawn a Mob.”

The return statements in Lines 32 and 35 are part of the standard mod framework. A method in Java can return a value when its task is completed. Your mods should return the value true inside the onCommand() method when the mod handles a user command and false when it doesn’t.

You have created your first mod, but it can’t be run yet by the Spigot server. It needs a file called plugin.yml that tells the server about the mod.

This file is a YAML file, which you also can create with NetBeans using these steps:

Select File, New File. The New File dialog opens.

In the Categories pane, scroll down and select Other.

In the File Types pane, select YAML File and click Next.

In the File Name field, enter plugin. (Don’t put .yml on the end; this is done for you by NetBeans.)

In the Folder field, enter src.

Click Finish.

A file named plugin.yml opens in the source code editor with two lines in it:

## YAML Template.
---

Delete these lines. They aren’t needed in this file. Enter the text of Listing 3.2 into the file, and be sure to use the same number of spaces in each line. Don’t use tab characters instead of spaces.

LISTING 3.2 The Full Text of This Project’s plugin.yml

Replace Your Name Here with your own name. Telling people you wrote a mod is the first step toward becoming a legendary Minecraft coder.

To double-check that you have entered the spaces correctly, there are four spaces in Line 8 before the text petwolf and eight spaces in Line 9 before description.

The plugin.yml file describes the mod’s name, author, Java class file, version, command, and a short description of what the command does.

This file must be in the right place in the project. Look in the Projects pane, where it should be inside the Source Packages folder under a subheading called <default package>. This is shown in Figure 3.2.

FIGURE 3.2Checking the location ofplugin.yml.

If the plugin.yml file is in the wrong place, such as under the com.javaminecraft heading, you can use drag and drop to move it to the proper location. Click and hold the file, drag it to the Source Packages folder icon, and drop it there.

You’re now ready to build your mod. Select the menu command Run, Clean and Build Project. If this is successful, the message Finished Building PetWolf (clean, jar) will appear in the lower-left corner of the NetBeans user interface, way down at the bottom edge.

The mod is packaged as a file called PetWolf.jar in a subfolder of the project. To find it, click the Files tab in the Projects pane, expand the PetWolf folder (if necessary), and then expand the dist subfolder. The Files tab lists all the files that make up the project, as shown in Figure 3.3.

FIGURE 3.3Finding the PetWolf mod’s JAR file.

This PetWolf.jar file needs to be copied from the project folder to the Minecraft server. Follow these steps:

If the Minecraft server is running, stop it by going to the server window and typing the command stop; then press the spacebar or any other key to close that window.

Outside of NetBeans, open the folder where you installed the Minecraft server.

Open the PetWolf subfolder.

Open the dist subfolder.

Select the PetWolf file (a JAR file), and press Ctrl+C to copy it.

Go back to the Minecraft server folder.

Open the plugins subfolder.

Press Ctrl+V to copy PetWolf into it.

You have deployed your new mod on the Minecraft server. Start the server the same way you did before—by clicking the start-server.bat file you created. If you look carefully at the messages that display in the server window as the server loads, you see two new messages in the log file that display as it runs:

[PetWolf] Loading PetWolf v1.0
[PetWolf] Enabling PetWolf v1.0

These messages do not appear together. One appears close to the top and another close to the bottom.

If you don’t see these messages, but instead see some long, complicated error messages, double-check everything in PetWolf.java against Listing 3.1 and plugin.yml against Listing 3.2 to ensure they were entered correctly; then rebuild and redeploy the mod.

TIP

Still having problems making the PetWolf mod work? The source code, plugin.yml configuration file, and all other files for each book project can be found on www.javaminecraft.com. Visit the website to see files that were compiled successfully and run on a server.

After you run the Minecraft client and connect to your server, enter the command /petwolf. You now have a new wolf who will follow you around. Enter the command as many times as you like to keep adding wolves.

Figure 3.4 shows me and 20 wolves. Hostile mobs don’t last long against us. We will rule this world. Hoooooooooooooooooowl!

This chapter was a sprint through the subject of how to write a Minecraft mod. The rest of the book will be at a much gentler pace that explains all the concepts in Java programming and the Spigot API that are necessary to create sophisticated mods.

There are a lot of different ways to learn Java. Doing it inside Minecraft is one of the most entertaining. You will put your programming abilities to use in a three-dimensional world where you fight monsters, dig tunnels, build houses, and dodge the legions of the living dead—including some who ride chickens.

Minecraft is an excellent way to learn about object-oriented programming, one of the toughest aspects of the Java language to master. Everything you do in Java is accomplished with objects. You create them in constructors, give them knowledge in variables, and tell them to do things by calling methods. Objects indicate the tasks they can perform by implementing interfaces.

All these concepts are new to you now, but as you learn them in the next 14 chapters, you will find they directly relate to mod programming. Need a zombie? Create a Zombie object. Want the player to move to a new spot? Call his teleport() method. Did you lose your horse? Call its getLocation() method.

So dig as deep into Minecraft mod programming as you like. You’re going to use these skills far beyond the mines.