Creating a blank Spigot plugin in IntelliJ IDEA

Apr 10, 2022
Creating a blank Spigot plugin in IntelliJ IDEA
  • Creating a blank Spigot plugin

    In IntelliJ IDEA




    Prerequisites(top)


    Preparing the Project(top)


    When you start up IntelliJ for the first time you will be presented with the welcome screen.
    [​IMG]
    Click on Create a new Project.

    [​IMG]
    On left select Java, and on the right side for your Project SDK select the respective Java version for your plugin. Click Next. You will be presented with the option to create the project from a template. Assuming this is your first plugin on IntelliJ, just click next there.

    [​IMG]
    In this window, you can set the project name and location where the source code and other project files will be stored at. Once you have filled this in you can click Finish.

    [​IMG]
    The IntelliJ workspace will pop up and you see on the left side the Project explorer where Modules will be stored at. If you previously wrote your plugins in Eclipse you can change from the modules view to the package view.


    Setting Up the Dependencies(top)


    Now you have to add the Spigot library to IntelliJ. Open the Project Structure from File > Project Structure...

    [​IMG]
    From here select Modules on the left side and click on Dependencies below Name. Click on the + button and select JARs and directories..., locate your Spigot-API-shaded jar and click Apply.


    Creating the Plugin(top)


    Creating the package(top)


    The packages act like folders for your plugin.

    [​IMG]
    In the project explorer on left, locate your module and right-click on the source folder - src, and select New > Package.

    You can name your package following these guidelines:
    • Only lowercase letters
    • No spaces
    • Dots act as folder separators
    • To avoid conflicts with other plugins with the same Main class directory, the general guideline is:
      • If you own a website or a domain, set the package name to [tld].[domain].[pluginname]
      • If you don't, use me.[yourname].[pluginname] or [country].[yourname].[pluginname]
      • If both are taken, try your email, e.g. com.gmail.[[email protected]].[pluginname]
      • *note: the IDE cant tell you if your package already exists for another plugin, you have to make sure by yourself. You can simply google the name.


    Creating your Main class(top)


    [​IMG]
    Now right-click on the package you just created and choose New > Java Class. It's common to name it with your plugin name or just Main.

    Modify the class declaration(top)


    Your Main class should extend JavaPlugin. This makes your class inherit all fields and methods in the Spigot JavaPlugin class. This is the concept of inheritance (homework). Change the class constructor.

    [​IMG]
    You will see IntelliJ giving you some suggestions as you type. You can choose a suggestion by using the arrow keys and Enter but you can also use the mouse.

    [​IMG]
    Otherwise you will see the word JavaPlugin in red because you haven't imported the class location from the Spigot API. Import the class manually by pressing Alt + Enter and choose import class.

    Your code should now look similar to this:
    Code (Java):
    package me.yourname.pluginname;

    import org.bukkit.plugin.java.JavaPlugin;

    public class MyPluginName extends JavaPlugin {

    }

    Implement the necessary methods(top)


    Now we want to add the onEnable() and onDisable() methods. You should use the @Override annotation on these methods because they belong to the parent class.

    onEnable() example:
    Code (Java):
    @Override
    public void onEnable() {
        Bukkit.getLogger.info(ChatColor.GREEN + "Enabled " + this.getName());
    }
    onDisable() example:
    Code (Java):
    @Override
    public void onDisable() {
        Bukkit.getLogger.info(ChatColor.RED + "Disabled " + this.getName());
    }
    You can pick the @Override methods on your own or you can import them by clicking on Code > Overriden methods or pressing the keys Ctrl + O, select the methods you want to import and click OK.

    Your final Code should look like this:
    Code (Java):
    package me.yourname.pluginname;

    import org.bukkit.plugin.java.JavaPlugin;

    public class MyPluginName extends JavaPlugin {
           @Override
           public void onEnable() {

           }

          @Override
          public void onDisable() {

          }
    }

    The plugin.yml(top)


    The sample plugin is now finished but we must tell the server multiple things including who loads the plugin, where the main class is, who the author is, what the plugins' name is, the developer's website and the commands in the plugin.

    You have to create a file named plugin.yml. If you do not have this file, your plugin won't be loaded and the console will send you an error.

    Right-click on the src folder and select New > File, set the name to plugin.yml, and click OK.
    The file MUST be stored in the src folder otherwise it won't work.

    In the plugin.yml you can have a lot of attributes, but not all of them are required to load the plugin, as some are required and others are optional.

    The simplest form of the plugin.yml should look something like this:
    Code (YAML):
    name: Plugin Name # the plugins name as it should appear in the plugin list /pl
    version
    : 1.0 # the plugin's version
    author
    : Your Name
    main
    : me.yourname.pluginname.Main
    api-version
    : # the version of the API you want to use, required starting with 1.13
     
    For more details check out the plugin.yml wiki.

    Compiling and Building Your Artifact(top)


    Compiling and building is simple, but first we need to create the artifact.

    Bring up the Project Structure menu like before and this time select Artifacts on the left navigation bar.

    [​IMG]
    Click on the + at the top. Choose JAR > From modules with dependencies.... In the first box make sure to select the Module you want to export. Leave the other default values. Click OK to start editing it. Select your Artifact and set the Output directory to where you want to build your plugin jar.
    [​IMG]
    Now you need to include your plugin.yml file to the artifact output by clicking the + > File and locating your file (make sure you do this for your future files such as config.yml too or they simply wont compile in your plugin and appear as missing). Click OK and build it by clicking Build > Build Artifacts... from the top menu and selecting your artifact.

    Running the Plugin(top)

    Copy the plugin from the artifact Output directory to your server plugins folder and start your server.

    You can also use build tools like Maven or Gradle or Ant but artifact compilation was used in this tutorial as it is the easiest way to compile your first plugin.
  • Loading...
  • Loading...