Introduction: Minecraft Spigot Server Mod Creation

Minecraft servers are awesome. They allow you to build friends across the world, talk with people, and mess around with all the commands spigot and all the plugins that are available for it. If you would like to help expand the plugins available for spigot and all of its users, then you should give it a shot. It takes only a couple minutes with some prior Java knowledge, and you can make almost anything!

Step 1: Prerequisits

Though this Instructable will be full of instructions, there are some pre-requisites, which I may tap upon in a further Instructable:

  • Java SDK installed (I am using Java 8)
  • Some Java knowledge
  • A Java IDE (I will be using Intellij)
  • A basic Spigot v1.9 server (And the server .jar file on your local machine)

A notice before we start: Spigot is based off of Bukkit, another server type. Methods in Bukkit will work in Spigot, unless they import net.minecraft.[MC version]. You can see their differences in this post.

Step 2: Creating Up Your Project

The first step in creating a server plugin, or any java project, is creating your project.

To create a project in Intellij, is to go to open it, and if you have a project already open, go to File > New > Project. If you don't have a project open and are greeted with a Welcome To Intellij IDEA screen, click Create new Project.

On the left bar of the window that pops up, make sure Java is selected, and the Project SDK is selected at 1.8 (or whatever version you are using). If it says something like , then click New > JDK and select your installed JDK path. Once everything is good, click Next

Click Next again, and name your project. I will name mine "Instructables". You can go to the text box right under it to select a path for your project. I have all of my projects in my coding drive, E:\, so that is where this will go. Click Next and you should be greeted with a bigger window.

Step 3: Configuring Your Project

Before we start coding out Minecraft plugin, we need to configure some things in our project.

First we need to add spigot as a library, so we can access the methods to create our plugin. To do this, go to File > Project Structure. In the window that pops up, go to the tab Libraries. Click the green plus sign toward the top left of the window, and click Java. From there, select in the popped up file browser the location your spigot server jar file is, and click OK, and then OK again in a Choose Modules window.

The next step is to go to the tab Artifacts in the left pane of the same Project Structure window. Click the green plus, and hover over Jar and click Empty. In the Name section at the top of the content added by clicking Empty, type whatever you want the compiled jar file's name to be once we are done. This can be different from what your project name is, but I will just name mine "Instructables". In the Output Directory section, you ca put wherever you want the plugin to compile. You can leave this default, which is what I will be doing, but you can change it. Make sure to select Build On Make, which basically Buildsthe project when you Make it. Going down further, click and drag the 'Project Name' compile output in the drop down menu under Available Elements, to 'Your project name'.jar to the left of that. Once you do that, you are done configuring your project, and you can click OK.

Step 4: Creating the Main Class

Now we are almost ready to start coding. To the left of your main screen you were greeted with when you created your project, click the arrow next to 'Your project name' at the very left of the window. Right click the blue src folder, hover New, and click Java Class. In the Name section of the popup window, type in the package name(s) you want separated by periods, then by convention 'Main' with another period before this, as this is your main class. You can name it something else, but it makes the project mush easier to work with. An example can be seen in one of the pictures in this slide. Click OK.

Step 5: Plugin.yml

Before we start coding in our Java class, we need to make a small but essential part of our plugin. On the left side of the main project window, right click src and hover over New and click on File. For your file name in the popup, type in plugin.yml. It needs to have this name and extension, because this is the file that the server looks for for some information on the plugin. We will be adding to this later, but this is what is needed to start.

The information in plugin.yml is similar for all plugins, so i will show an example of what should be put in, and then a line-by-line breakdown of the code.

name: Instructables
main: com.instructables.main.Main
description: A plugin for the awesome website Instructables.com
website: www.instructables.com
author: RubbaBoy
version: 0.1

Now time to break down the code.

name: Instructables

Is the name of your plugin the server will recognize. This can be different from your project name and jar name.

main: com.instructables.main.Main

Is the path to the class you just created in the previous class. This tells the server what class is the main one to access some methods we will be creating in a few steps.

description: A plugin for the awesome website Instructables.com

This is a description of the plugin, which can be anything you want. Use a \ before special characters, like regular java strings, like ", ', and \.

website: www.instructables.com

Is your website to contact you, or one you own, like, or anything. This can be blank, as it is not mandatory for the server to have.

author: RubbaBoy

As the name of this implies, this is your name people identify you as on the internet with. This can be multiple people, and is sometimes accessed by McMyAdmin (A server management program), or some plugin management plugins.

version: 0.1

This again tells you what it is by the name. This is the version of your plugin, which I personally find helpful to change whenever a new plugin version gets released, but you can have whatever number you want here.

Side note:

You can put in other things to your plugin.yml, that can be viewed here or here.

Step 6: Coding: the Template

Now that we are finished with setting up the project, configuring it, and creating the plugin.yml, we are finally ready to start coding. We will be creating the basics that every plugin should have in this step, and then expanding upon it in further steps.

First, we need your main class to extend JavaPlugin, and import it. This labels this class as the main class, and there can only be one class that imports JavaPlugin, or else errors will occur.

There are two functions we need to create, and that is onEnable and onDisable. These should Override their parent functions in JavaPlugin. They should look like this:

@Override
public void onEnable() {
    
}

@Override
public void onDisable() {
    
}

The function onEnable is run when the plugin is being enabled while the server starts or reloads. As you can probably conclude, onDisable is run when the plugin is being disabled/stopped, like when the server reloads or stops. These functions may not seem useful for some plugins, but they are for other plugins that require registering of events/event handlers, or registering things like commands. They override their parent methods from JavaPlugin, as well.

Step 7: Coding: Command

Now we finished the base two methods, and making our class the official main class, we can make the command function.

For our command, we want to be organised, so we will have it in a separate class from our main one. Go go to your left and create a new class. I will name mine "CommandClass". We want this new class to implement CommandExecutor. If you implement the methods, you will get something like this:

@Override
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) { 
	return false; 
}

Now we have a method that gets executed when you type in a command. We want to listen for a certain command, so we will something like this before our method returns false:

if (command.getLabel().equalsIgnoreCase("instructables"))

This gets what you typed in, command.getLabel and checks if it is equal to "instructables". as you may know, equalsIgnoreCase checks if our command is equal to what we typed in no matter if some characters are upper case or not.

Add some curly brackets after that if statement, and add this:

commandSender.sendMessage(ChatColor.GOLD + "Put whatever you want here!");

What this does, is it gets the variable commandSender which is whoever sent the command (or console), and we send a message to them with the function sendMessage which accosts a String. One thing that may seem different from a string is this ChatColor.GOLD. This makes whatever is after it the color gold. The colors you can do are nicely listed right here. Use the technical names when putting it after ChatColor..

Then right after that, still in the curly brackets, have the function return true:

return true;

This is because we are telling the server that the command was completed/recognized.

In its completed form, the onCommand function should look something like this:

@Override
public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
    if (command.getLabel().equalsIgnoreCase("instructables")) {
        commandSender.sendMessage(ChatColor.GOLD + "Put whatever you want here!");
        return true;
    }
    return false;
}

Step 8: Coding: Finishing the Command

We have finished the Java part of our command, but we still need to add something in the plugin.yml so the server knows it is a command to put in things like /help.

First, we need to hop back into the plugin.yml. Add this at the bottom of the file:

commands:
   instructables:
      description: Activated our Instructables command!
      usage: /instructables

Replace instructables with whatever you named your command (Note this needs to be the SAME thing as the command you put in in your command class). What this code does, is the first line, commands:, starts a list in YML (the plugin.yml file type), and then where I put instructables and you put your command name, it starts another list inside that commands list. Note, if you have many commands, make sure they all are in the commands list, not the one command's list. In this Your Command list, we are defining two things. The first part, description, is the description our server says when we do /help. This can be anything you want. The next one, usage, is the usage /help shows. Usually, not to confuse the player, you would put your command there, and any arguments afterwards, which we will go over later.

Now, compile/build the project. In Intellij the hotkey is Ctrl + F9. Then put in your plugins folder, and start/reload your server.

Step 9: Testing!

Now we have finished, we can finally test our command. You can go on your server and do /help [your plugin name] and it will show the command you have made. If you do the command, you should get the text you put in the code, which in my case was "Put whatever you want here!" in gold.

Step 10: Expanding Further: 1

We have a nice plugin base here, which doesn't do much. I can't leave you hanging, can I? In this next section, we will be making our plugin put whatever block is in your hand on your head.

Step 11: Expanding Further: 2

First for adding hats, we need to make sure only the player does the command, because if console does it, well, there's no hand to get a block from, and no head to put it on. So before the sendMessage and return true statement in our onCommand function, we need to check if the commandSender is an instance of Player. This can be done, as you may know, by this:

if (commandSender instanceof Player) {

With the curly brackets going around the sendMessage and return true things in our function. We can just add an else statement after that if statement we just added, telling the commandSender that the command can only be used by a player (don't forget to return true, as the command was still recognized), which can be done by:

} else {
    commandSender.sendMessage("You need to be a player to use this command!");
    return true;
}

Back to the inside of our instanceof statement, we need to add this, before the return statement:

Player player = (Player) commandSender;

This gets a Player object out of our commandSender, and it is safe to cast inside this if statement, because we already checked that our commandSenderis a player.

Next, we need to get the player's inventory, in order to get the block in the player's hand, and equip it to their head. This can simply be achieved by:

PlayerInventory inventory = player.getInventory();

The way this works, is as you probably can tell, is it gets out player variable, and gets their inventory, and puts it to a variable called inventory. This needs to be a PlayerInventory and not just an Inventory, because inventories can be things like chests, droppers, ect., while a Player has hands, armor, and other little bits that regular Inventories don't have.

The next task is to set the helmet to the item in the player's hand. This is more simple than you may think:

inventory.setHelmet(inventory.getItemInMainHand());

This can be broken down into two parts. There is the

inventory.setHelmet

And:

inventory.getItemInMainHand()

I will go by them one bit at a time. The first one gets our previously defined inventory variable, and sets the hat, or helmet, as whatever comes after it in parenthesis.

The second part, that is getting set as the hat, again gets our inventory variable, and gets the item in our main hand. Items can be in your main or off hand, but for this tutorial we will be getting the item in our main one. For setting the hat, that is about it! Now compile the plugin, and you're ready to test it!

Step 12: Testing! Again...

Build/make your project again, put it in your plugins folder, and reload/start your server like last time. Hold a block in your main hand, and type in your command. You should see the item get copied over on your head! You can do this with any item/block, some will look different than each other, and some won't render at all (Like items). If you try your command in your server's console, it should tell you the command needs to be preformed by a player, or whatever you put.

Step 13: Finished!

Well, you have finished now! You now know how to set up a project just right, how to make a basic template of a plugin, and how to make commands that actually do something!

If this gets popular, or you like this, put a comment down asking for a second tutorial with command arguments, particle effects/sound effects, or anything else you want to know how to do.

If you would like the code for this project, check out my Github here:

https://github.com/RubbaBoy/Instructables-Spigot/

Step 14: Helpful Links

Here's a list of links you can go to for help (If you are too lazy to ask me something), and can be helpful if you just want to browse around trying to learn something new.

NOTE: YouTube also is REALLY helpful. There's a lot of great tutorials for anything you can imagine, and that's where I learned most of my programming knowledge from (The bases of the languages at least).