Creating a blank Spigot plugin in Eclipse

Jul 19, 2017
Creating a blank Spigot plugin in Eclipse
  • Creating a blank Spigot plugin

    In Eclipse IDE


    The purpose of this tutorial is to teach you how to create your first blank plugin that can be loaded in your Spigot server. This tutorial will also show you one possible development workflow of setting up the workspace to exporting a JAR file.

    Prerequisite
    This guide assumes you have already used the build tools and have run the Spigot server at least once. It also assumes that you have the Spigot-API jar file which we will use. Consult the wiki if you need assistance with either.

    1) Start Eclipse; you may change the workspace location if desired.
    2) Create a new Project
    • Set the project name to whatever you wish. Here, we chose SpigotBlankPlugin. Click next. Help image
    • Select Add External JARs under the Libraries tab. In the JAR Selection dialogue box, select the spigot-api-shaded jar file, which can be found in Spigot/Spigot-API/target/ inside your BuildTools folder. Select Finish (Help image)
    3) Add a new package
    Right click on src and click on New > Package. You may use any namespace convention you wish, just be consistent. Here, I use reverse domain name notation (i.e: com.google.android). (Help image)
    4) Create a new class
    Right-click on the newly created package and select New > Class. Give it any name; since this is a blank plugin, I gave it the same name as the project. (Help image). Inside the editor, the newly created Java class will open. The code should look somewhat like this:
    Code (Java):
    package com.meeku.tutorialPlugin;
    public class SpigotBlankPlugin {
    }
    5) Modify class declaration
    Your class must extend from JavaPlugin. This makes your class inherit all fields and methods of the JavaPlugin class. This is the concept of inheritance (homework). Change the class constructor. Eclipse will produce an error as it does not know what JavaPlugin is. If you have successfully imported the Spigot-API, you will be able to import JavaPlugin by adding the import statement. You do not need to manually type that line, simply click the error and select the appropriate action (Help image). Your code should now look like:
    Code (Java):
    package com.meeku.tutorialPlugin;
    import org.bukkit.plugin.java.JavaPlugin;

    public class SpigotBlankPlugin extends JavaPlugin {

    }
     
    6) Implement the necessary methods
    The JavaPlugin class has some abstract methods which must be implemented by your plugin. Hence, add the onEnable and onDisable functions which will be triggered when the plugin is disabled or enabled in the console. You can leave these blank for now. You are also required to write @Override above the method.

    Note: You do not need to add a getLogger when your plugin is enabled or disabled, Bukkit already does that for you.

    Code (Java):
    package com.meeku.tutorialPlugin;
    import org.bukkit.plugin.java.JavaPlugin;

    public class SpigotBlankPlugin extends JavaPlugin {
        // Fired when plugin is first enabled
        @Override
        public void onEnable() {
        }
        // Fired when plugin is disabled
        @Override
        public void onDisable() {

        }
    }
     
    7) Create plugin.yml file
    Right click the project and create a file New > File. Name it plugin.yml. Paste in the following:
    Code (YAML):
    name: SpigotBlankPlugin
    main
    : com.meeku.tutorialPlugin.SpigotBlankPlugin
    version
    : 1.0
    commands:
    8) Export
    Since there are no errors, we can export this project as a JAR. Right-click on the project name, select Export. In the consequential dialogue box, select JAR file (Help Image). Click Next. You can uncheck the classpath and project include and change the export destination to your plugins folder (Help Image)
    9) Running
    Run the bat file and you should see that the plugin was enabled
    [​IMG]


    Regards,
    Spigot wiki contributor(s)
  • Loading...
  • Loading...