Creating a blank Spigot plugin in IntelliJIDEA

Oct 6, 2017
Creating a blank Spigot plugin in IntelliJIDEA
  • Creating a blank Spigot plugin

    In IntelliJ IDE


    First you have to install IntelliJ IDEA on your computer, IntelliJ can be downloaded at https://www.jetbrains.com/idea/

    Preparing the IDE

    When you start up IntelliJ for the first time you will be presented with a welcome screen. Click on “Create a new Project”. (Picture) A new window will pop up. Here you should select Java on the left side, and on the right side you have to select the SDK you wish to use for your plugin. If you haven't already, download a JDK here.[​IMG]
    Click next. Now you will be presented with the option to create the project from a template. Unless you have previously made a plugin project template, just click next here. In the next window you can set the project name and the location were the source code and other project files will be stored at. Once you have filled this in you can click “Finish”. (Picture)

    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.
    [​IMG]

    Now you have to add the spigot library to IntelliJ. To open the Project Structure press the keys “Ctrl” + “Alt” + “Shift” + “S” or click on the button at the top of the right side. A new window will appear. Switch to Modules on the left side and click on dependencies, now click on the green + at the right side choose the first option JARs and directories. In the window which now is coming up you have to browse to your location where the SpigotAPI is stored. Click on “Apply”.
    [​IMG]


    Creating the Plugin
    1) add a new package
    Expand the Module and right-click on src, choose new and in the next window package. You can name your packages anything you want, but the general guideline is this:
    If you have a website or a domain, you should set the package name to [tld].[domain].[pluginname].
    If you don't, go for [country].[yourname].[pluginname] or me.[yourname].[pluginname] if the domain isn't taken.
    If both are taken, use your email, e.g. com.gmail.[[email protected]].[pluginname] (Picture)

    2) create a new class
    After you finished creating the package you right click on the package you just created and choose new → class. Enter the class name you want its common to choose the plugin name or just “Main”. (Picture)

    "Warning" Scroll up,read all the steps again, and Add a server.jar to your external library list, before continuing to step 3. Otherwise the intellijidea program will not show the auto type suggestion popup for JavaPlugin.

    3) 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.
    If you look at your screen while typing you see that IntelliJ give you some suggestions (Picture). You can choose a suggestion by using the arrow keys and Enter but you can also use the mouse. If you don't you will see the word JavaPlugin is red so you have to import the class manually by pressing “Alt” + “Enter” and choose “import class”.(Picture)

    Your code should now look similar to this:
    Code (Java):
    package org.spigotmc.tutorial;

    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin {

    }
    4) Implement the necessary methods
    OK we imported the library and make the class to an plugin class, now we have to add the onEnable() and onDisable() methods. You should use the @Override annotation on these methods because they belong to a parent class.

    The onEnable method should look like this:
    Code (Java):
    @Override
    public void onEnable(){
        //Fired when the server enables the plugin
    }
    And the onDisable method:
    Code (Java):
    @Override
    public void onDisable(){
        //Fired when the server stops and disables all plugins
    }
    You can write the @Override methods by your own or 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 org.spigotmc.tutorial;

    import org.bukkit.plugin.java.JavaPlugin;

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

           }

          @Override
          public void onDisable(){

          }
    }
    The plugin.yml
    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 command's created with this 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 be exported.

    In the plugin.yml you can set a lot of information but not all of the information is required to load the plugin, as some information, for example, may depend on whether your plugin has dependencies or not.

    The simpliest form of the plugin.yml should look something like this:
    Code (YAML):
    name: //the plugins name as it should appear in the plugin list /pl
    version
    : //the plugin's version
    author
    : //the author's name
    main
    : //your main class were the onEnable and onDisable methods are. (Ex: org.spigotmc.tutorial.Tutorial)
    Make sure you have the right CASE-SENSITIVE name of your class. (Example)

    Other variables are:
    Code (YAML):
    depend: //tell the server that an external plugin must be loaded before your plugin can be loaded
    website
    : //your website appear with /version <pluginname>
    commands
    : // if you have some commands
          <command>
    :
                   description
    : //describe your command it will appear with /help
                   usage
    : //how is the commands' syntax
    Compiling

    Compiling is as simple as clicking "build" --> "build Artifacts", and selecting the artifact you wish to build. 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. Click on the green + at the top. Choose JAR and in the next box “From Modules with dependencies” a window will appear. In the first box you have to choose what Module you want to export, make sure you set it to the Module you want to export. Click “OK” to apply your changes. Select your Artifact and set the location were the compiled output should be located at.
    [​IMG]

    Running the Plugin
    Drag the plugin from where you told your artifact to export into your plugins folder of a test server and start the server (Picture)

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