Creating a blank Spigot plugin in NetBeans

Nov 24, 2016
Creating a blank Spigot plugin in NetBeans
  • Creating a blank Spigot plugin

    In the NetBeans IDE


    This tutorial will demonstrate how you can create your first (blank) Spigot plugin project using the NetBeans IDE. Creating a plugin requires the use of the Spigot API (spigot-api.jar) which will be automatically set up when you compile the Spigot server. If you haven't done so already then please check out the BuildTools tutorial which will explain how to obtain BuildTools.jar and use it to compile both the Spigot server and set up the API.

    Start NetBeans & create a new project

    Click the New Project option in the toolbar or use the control-shift-n keyboard shortcut. You'll need to create a Java class library, the definition can be found under the default Java category. You can name the project any way you want, but for this tutorial we'll be using SpigotBlankPlugin as the project name.

    Add the Spigot library API to your new project

    In order to develop a plugin you'll need the Spigot API. It's advisable to add the API to your library manager (see the Tools => Libraries menu option) so that you can also access the source code and Javadoc, but in this example we'll only add the individual jarfile.

    To add the API collapse your project structure and right click on the Libraries folder. Select Add JAR/Folder and point the dialog to the Spigot-API.jar file. After selecting you should find it under the Libraries folder, together with the (default) Java JDK library.

    Create a new class

    Click the New File option in the toolbar or use the control-n shortcut using your keyboard. Specify the class name and also provide a package name. In this example I'll be using BlankPlugin for the class name and org.spigotmc as the package name. Obviously you can replace these with your own names. Just remember that it is advisable to use a reverse domainname notation for your packages.

    The new class, and the package, should now appear in your Projects tab, and the class also opens in the editor, showing you something like this:

    Code (Text):

    /* BlankPlugin - Created on 24-nov-2016, 3:21:40 */

    package org.spigotmc;

    /**
    *
    * @author  ShelLuser
    * @version 1.0
    */
    public class BlankPlugin {
     
    }
     
    Define your class as a server plugin

    In order to set up a plugin your class must extend from the JavaPlugin class. This will give it access to all the required fields and methods. So change the class definition accordingly:

    public class BlankPlugin extends JavaPlugin {

    NetBeans will automatically add the required import (import org.bukkit.plugin.java.JavaPlugin;) if you use code completion and have selected the right class from the popup window. You can also simply add the extends definition manually, then use alt-enter while on the class definition line and select the provided 'Add Import' solution option manually.

    At this point you will also be able to use the source browser by control-clicking on the JavaPlugin definition, this will open the abstract JavaPlugin class in the editor (read only) which allows you to check up on all its fields and methods. Also be sure to check the Navigator pane to get a good overview.

    Override any required methods

    In order to make your plugin do the things you want you'll need to override some existing methods and create new behavior for those. A minimal requirement for a plugin is to override the onEnable and onDisable methods, these determine what a plugin does when it's enabled and disabled. For this example you can leave both methods empty:

    Although not required it is highly recommended that you use the @Override annotation as well because it will make it easier to differentiate between native methods and overridden methods.

    So now you should end up with adding something like this:

    Code (Text):

        @Override
        public void onEnable() {
        }

        @Override
        public void onDisable() {
        }
     
    Add the plugin definition file

    When Spigot loads a plugin it will check for the presence of a YAML file in order to determine the main class of the plugin. Therefor the addition of this file is mandatory, your plugin won't work without it.

    So click the New File option, or use the control-n keyboard shortcut, open the 'Other' category and select the YAML file. By default it will add the file to the src (physical) folder, which will make it end up under the Default Package folder in your project tab.

    Add the following to the file:

    Code (Text):

    name: BlankPlugin
    version: 1.0
    main: org.spigotmc.BlankPlugin
     
    Optionally you can also add other definitions such as author or website.

    Build your project

    Assuming that there are no errors shown you can now build your project. Right click on your project and click Build. If your project is defined as the main project you can also click the Build Main Project icon in the toolbar or simply press F11 on the keyboard. When done you can open the Files pane, collapse your project and you should find the new jarfile in the dist folder.

    If you check the jarfile properties (either using the Properties pane, or right click and select the Properties menu option) you will find the physical location of the jarfile, use this to find and copy the jarfile to your Spigot server plugin folder.

    When working on a big project, and assuming you can directly access your Spigot server plugin folder, you could consider editing build.xml (also shown in the Files pane) and create an Ant task which can copy the generated jarfile automatically for you. Explaining this in full detail goes beyond the scope of this beginners tutorial, but if you want to learn more about these options then please check out the Ant section on the NetBeans user wiki.

    Testing your new plugin!

    After you copied the jarfile to your Spigot server plugin folder you should see it getting mentioned on the console. Of course you can also simply check the latest logfile. For example:

    Code (Text):

    [11:36:22] [Server thread/INFO]: Preparing spawn area: 85%
    [11:36:23] [Server thread/INFO]: Preparing start region for level 2 (Seed: -1083268869392818446)
    [11:36:24] [Server thread/INFO]: [BlankPlugin] Enabling BlankPlugin v1.0
     
    Congratulations, you successfully completed your first steps on becoming a new Spigot plugin developer using NetBeans!
  • Loading...
  • Loading...