-
Notifications
You must be signed in to change notification settings - Fork 11
Setting up your workspace
For this example, I'll be using Eclipse and Maven as they are the ones that I am most familiar with and that I use on a daily basis. If you are using IntelliJ IDEA or some other IDE, please look online for a tutorial on your IDE that covers how to use Maven.
Alright, let’s open up Eclipse and get started. After Eclipse has launched, go to the top and click on “File”, then “New” and select “Project” (not “Java Project”) from the list to the right. On the window that popped up, double click the “Maven” Folder, select “Maven Project” and click “Next”. You can modify any settings you want, or just leave them at their defaults and click “Next” again. Eclipse will now prompt you to choose an Archetype, and the quick start will already be selected. I will leave it at that, however, we will be changing some of the things it automatically generates later. If you also have Quick Start selected, you can click “Next”. On this screen, you must specify your Group ID, Artifact ID and Package. I will be entering com.chrismin13 for my Group ID, TutorialAdditions for my Artifact ID and com.chrismin13.tutorialadditions for my Package. Please specify your own Group ID, Artifact ID and Package. We will be leaving the version at 0.0.1-SNAPSHOT, but you can change it if you want. Once you’ve completed all that, you can click on “Finish” and Eclipse will generate the Project for you.
If we go ahead and open up our project, we can see that Maven has already generated a few things, which we will now edit. I'll start off by deleting everything under src/test/java as we won't be using it. After that, I will prepare the pom.xml file, so that Maven can download all the dependencies needed for coding our plugin. We will need to add some resources and dependencies to our pom.xml. Double click the file, and select the pom.xml tab in the bottom to view the xml file itself. Here, we’ll insert the following lines, which you can find in the description of the video:
<repositories>
<!-- Spigot Repository-->
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<!-- Additions API Repository -->
<repository>
<id>AdditionsAPI-mvn-repo</id>
<url>https://raw.github.com/chrismin13/AdditionsAPI/mvn-repo/</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
<dependencies>
<!--Spigot API-->
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.12-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!--Bukkit API-->
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.12-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!--Additions API-->
<dependency>
<groupId>com.chrismin13</groupId>
<artifactId>AdditionsAPI</artifactId>
<version>1.3.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>As you can see, I have inserted the dependencies within the ones that were already provided by default. These will tell Maven to download Spigot and the Additions API so that you can use them in your code. Make sure you replace the versions of all Dependencies with the ones that you'll be using! It is always recommended to use the latest version, so check which one it is and insert that as the version. Save this file and close the tab, as we won't be needing it anymore. Don't forget to refresh the project, by right clicking on it and selecting refresh, so that it can download the dependencies we specified.
We will now delete the App.java Class that was automatically generated under the main package, as I will be replacing it with the main class of our plugin. So, after you have deleted it, create a class with the same name as your Artifact ID, in my case TutorialAdditions.java. All this should be placed under the main package, in my case com.chrismin13.tutorialadditions. Now that our class has been created, we will extend JavaPlugin as this will be the main class of our plugin. I will also create a second class, Items.java, that will also be in our main package. This class will implement Listener, so go ahead and add that. We will be adding code in this class on the next episode. Back in our main class, TutorialAdditions, I will add an onEnabled method with a simple debug message so that we can see if the plugin is loading once we export it. While we're here, I'll go ahead and register our listener, the Items class. This is how I usually do it:
PluginManager pm = this.getServer().getPluginManager();
pm.registerEvents(new Items(), this);At this point, I'll also add a way to get our plugin's instance, as we'll need it later for accessing its data folder, jar file, and other things. This is how I usually do it, by adding a private variable at the top, setting it in my onEnable and adding a method for accessing it from any class.
private static JavaPlugin instance;
public void onEnable() {
instance = this;
PluginManager pm = this.getServer().getPluginManager();
pm.registerEvents(new Items(), this);
System.out.println("Tutorial Additions has been enabled!");
}
public static JavaPlugin getInstance() {
return instance;
}Check you got all the imports correct and that should be it for our main class. Next, we will set up the plugin.yml file. This is just like any old plugin.yml file, but we must also tell it that our plugin depends on the Additions API. We will add this file to the src/main/resources folder, as it contains any files that will also be added to the final JAR file. So, we must create the resources folder under the main folder and create a plugin.yml file inside of the folder. This is what the plugin will contain:
name: TutorialAdditions
version: 0.0.1
main: com.chrismin13.tutorialadditions.TutorialAdditions
author: chrismin13
depend: [ AdditionsAPI ]Of course, you must replace all my info with yours. So, go ahead and replace the Name with the plugin of your name, the author with your name and the main with the directory to your main class. As you can see, I have added a line that says that our plugin depends on the Additions API, so the server will make sure our plugin gets loaded after the API has already loaded. Save the file and we can move on.
This should be all we need to get our plugin off the ground! Now, we must exort our plugin and insert it to our server to make sure everything is running as it should. To do that, I will right click on our project and go to Run as. Then, I will pick the fourth option, Maven build... which will open up a window in which we can specify how our plugin will be built. I will leave everything to default, other than the name, which I will set to "TutorialAdditions - Export JAR File" (you can set it to whatever you want) and the goals, in which I will enter package. This will make sure our project is packaged in a JAR File. Click Apply, then Run. If everything goes well, Maven should report after a few seconds that the build was a success and so we now have a JAR File of our plugin! I'll go ahead and right click on the project and select Show In -> System Explorer. This will open up the Project's workspace. Here I can go into my project, then into target where I'll find the exported JAR File. I'll copy it, then go to my test server and paste it in the plugins folder. If you don't already have the Additions API JAR in your plugins folder, then please go on Spigot and download the latest build, as our plugin depends on it. After running the server, we can see that the plugin has printed the message that it has been enabled, so our build was a success!
This is the end of the first episode. You now know a bit more about the plugin's history, its abilities and you have started your own plugin which we'll be adding items to in the next episode! Thanks for watching.
Before you start
Creating and editing Custom Items
Tutorials made by other people