Plugins Archive

Adding Logging and a Command to your Spigot Plugin

In our last post, I showed you how to create a Java Spigot plugin.
This plugin, while it will compile and run, won’t do anything.

Its time to make the plugin do something. First it would really nice to log things to the server log as the plugin runs. This lets you and the server owner running your plugin know that your plugin is working correctly or if its not, what is wrong.

The JavaPlugin class that your plugin is extended from includes a logger. You need to call getLogger() to get access to it. Also to make it easier to do this from any class your plugin, I recommend make a reference variable to your main plugin class as Static.

The code line plugin.getLogger().info(“My log information”); can be used anywhere in your plugin to create log lines.

public class BasicPlugin extends JavaPlugin {
   
    static BasicPlugin plugin;

    @Override
    public void onEnable() { 
        plugin = this;
        plugin.getLogger().info("Plugin Starting up!");

    ///....

Starting a new Minecraft Plugin with Intellij

1. Open IntelliJ (If you don’t have IntelliJ you can get it from JetBrains https://www.jetbrains.com/idea/)
2. Start a new Project.

Just choose Java, you don’t need any of the other options. If you don’t have a Java SDK, you should close IntelliJ and go get that first. Click Next.

You don’t need a template. Just click Next.

Name your plugin and Click Finish.

3. Add Spigot API Jar to libraries
Now you need to add the Spigot API jar to the Libraries in the Project Structure screen.
(If you don’t have the Spigot API, you need to go build it with BuildTools from https://www.spigotmc.org/wiki/buildtools/ )

Click on the Green + and choose Java.

Find the Spigot API Jar on your computer. If you ran BuildTools it will be in a Target folder under the SpigotAPI folder.
Choose modules dialog will appear with your project in it and highlighted. Click OK

Click Ok on the Project Structure window to close it.

4. Create First Package
Java likes to organize code into Packages.
Right-Click on the src folder. Choose New > and then Packages

You put in your domain and name. If you do not own a domain, you can just write something like: org.myname

5. Create First Class
Right-Click on your Package name in the tree under src and choose New > Java Class

Name your first class the same name as your Plugin.

Enter this code for you first class. The Package line should already be there for you.
This code will allow your plugin to load, but it currently does nothing.

package you.packagename.here;
import org.bukkit.plugin.java.JavaPlugin;
public class BasicPlugin extends JavaPlugin {
   @Override
   public void onEnable() {
        super.onEnable();
   }

   @Override
   public void onDisable() {
        super.onDisable();
   }
}

6. Add Plugin.yml
In order for Spigot to understand your plugin, you will need a plugin.yml file in your Jar.
Right-click on the src folder again, this time choose, New > File.

Name the new file: Plugin.yml

Add this code to your Plugin.yml

#the plugins name as it should appear in the plugin list /pl
name: YourPluginName
#Plugins Version
version: 1.0
#Author's Name
author: YourName
#your main class were the onEnable and onDisable methods are
main: your.package.path.plus.classname

7. Create Artifact
To compile your code into a Jar file, you need to create an Artifact in IntelliJ
Click on the Project Structure option on the File menu.

Choose Artifacts and Click on the green + to add one.

Choose JAR and empty

Name your plugin and double click on the Compile Output on the right.

Add the Plugin.yml by clicking on the Green + and choose File.

8. Build Jar
Ok now you can build your Plugin.

It will go into the Target directory in your project folder.

9. Add Jar to the plugin folder of your Spigot Server and Test

Additional line to add to your code
This is how you can log information from the onEnable and onDisable methods.
this.getLogger().info(“Plugin Starting up!”);