Creating a blank Spigot plugin in IntelliJ

Mar 10, 2015
Creating a blank Spigot plugin in IntelliJ
  • Creating a blank Spigot plugin

    In IntelliJ IDE


    First you have to Install IntelliJ IDEA on your computer, you can download the IDE from Jetbrains.com

    Preparing the IDE

    Now you start IntelliJ and you see the welcome screen from IntelliJ. Now you click on “Create a new Project”.(Picture) A new window will pop up and you have to choose JavaProject on the left side and on the right side you have to choose your the SDK you want to use for your plugin (in my case oracle java 8)(Picture) click on next here you can choose what template you want to use, but there is no template for SpigotPlugins - of course – so you can click again on Next. In the next window you can set the project name and the location were the source code and stuff will be stored at. If you filled in what you want you can click on “Finish”.(Picture)

    The IntelliJ workspace will pop up and you see on the left side the Project explorer were Modules will be stored at.If you write your plugins in Eclipse before you can change from the modules view to the package view.(Picture)

    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 (Picture). 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 were the SpigotAPI is stored. Click on “Apply”. (Picture)

    Swich to 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.(Picture)


    1) add a new package
    Expand the Module and right-click on src, choose new and in the next window package. If you have a website or an domain, you should set the package name to [topleveldomain].[domain].[pluginname]
    If you have no website or domain you can choose me.[yourname].[pluginname] but its matter of taste. (Picture)

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

    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:
    PHP:

    package de.freemine.SpigotPlugin;
    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 have to use the @Override statement over the method you declare with this that you want to use the method from the Spigot library.

    The OnEnable method should look like this:
    PHP:

    @Override
    public void OnEnable(){
    //Fired when the server enables the plugin
    }
     
    And the OnDisable method:
    PHP:

    @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:
    PHP:

    package de.freemine.SpigotPlugin;
    import org.bukkit.plugin.java.JavaPlugin;

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

    }

    @Override
    public void onDisable(){

    }[/INDENT]
    }
     
    5) the plugin.yml
    The Main Code is now finished but we must tell the server who loads the plugin were the Main class is, who is the author, whats the plugins' name, the devs website, the commands created with this plugin.

    You have to create a file which name is “plugin.yml”.If you don't have this file your plugin won't be loaded and the terminal will send you an error.

    Right click on the src folder and select “new → file” set the name to “plugin.yml” and klick OK.
    The file MUST be stored in the src folder otherwise it won't be exported.

    In the plugin.yml you can set much information but not all information is depend to load the plugin.


    You must set:
    PHP:

    name: //the plugins name as it should appear in the plugin list /pl
    version: //the plugins version
    author: //you name
    main: //your main class were the OnEnable and OnDisable methods are
     
    Other variables are
    PHP:

    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
     
    6) Compiling
    Now all work for a blank plugin is done and we must compile the plugin. You can do that by clicking on “Build → Build Artifacts” and choose in the window which appears in the middle of your workspace the artifact you want to compile. If that’s the first time you compile the plugin you should choose Build in the next menu but if that’s the second or x compilation you can choose rebuild.

    Drag the plugin into your plugins folder of a test server and start the server:


    You can also use build tools like maven or Ant but I choose the artifact compilation because its the most easy way to create the first plugins.

    thanks to @meeku for some verbalizations

    Sorry for the grammatical mistakes, I just had a surgery and don't speak English as well as I want.
  • Loading...
  • Loading...