The simplest method to add the Bukkit API to your project is to download the Bukkit.jar directly from the Spigot Repository and add it to your project's classpath. Legacy versions of Bukkit can be found at the Bukkit Repository.
The other is to add it as a Maven dependency, by adding the following lines to your pom.xml
:
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
</repositories>
<dependencies>
<!--Bukkit API-->
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>{VERSION}</version>
<scope>provided</scope>
</dependency>
</dependencies>
The plugin's main class is the entry point for Bukkit to load an interact with your plugin. It is a class that extends JavaPlugin
and only one instance of it should be created by your plugin. By convention it is good to give this class the same name as your plugin.
Here is an example of a main plugin class for the plugin "MyPlugin":
package com.example.myplugin; //{$TopLevelDomain}.{$Domain}.{$PluginName};
import org.bukkit.plugin.java.JavaPlugin;
public final class MyPlugin extends JavaPlugin {
@Override
public void onEnable() {
//Called when the plugin is enabled
getLogger().info("onEnable has been invoked!");
}
@Override
public void onDisable() {
//Called when the plugin is disabled
getLogger().info("onDisable has been invoked!");
}
}
To access your plugin instance from another class, you'll need to store the instance of your MyPlugin class created by Bukkit so that is accessible from outside of the class.
public class MyPlugin extends JavaPlugin {
private static MyPlugin instance; //Effectively final variable containing your plugin's instance
public MyPlugin(){
if(MyPlugin.instance != null) { //Unnecessary check but ensures your plugin is only initialized once.
throw new Error("Plugin already initialized!");
}
MyPlugin.instance = this; //A plugin's constructor should only be called once
}
public static MyPlugin getInstance(){ //Get's your plugin's instance
return instance;
}
//your other code...
}
Then, to access your main class from another class, simply use MyPlugin.getInstance()
public class MyOtherClass {
public void doSomethingWithMainClass(){
MyPlugin.getInstance().getLogger().info("We just used MyPlugin");
}
}
The plugin.yml file goes in the root of your final jar file and provides essential information to Bukkit to load your plugin. The most simple plugin.yml looks like this
name: {$PluginName} //The name of the plugin
main: {$PackageName}.{$MainClass} //The fully qualified name of the main class.
version: {$Version} //The plugin's version
For example with the above MyPlugin class
name: MyPlugin
main: com.example.myplugin.MyPlugin
version: 1.0