The essentials of a Bukkit plugin

Each Bukkit plugin requires two specific files. These files are plugin.yml and the main class of the plugin. We will begin by creating the most basic versions of each of these files. All of your future projects will start with the creation of these two files.

The plugin.yml file

We are ready to start programming a Bukkit plugin. The first file that we will create is plugin.yml. This is the file that the Spigot server will read to determine how to load a plugin. Right-click on Source Packages and click on New | Other..., as shown in the following screenshot:

The plugin.yml file

In the window that appears, select Other under Categories. Then, select YAML File under File Types, as shown in the following screenshot, and click on Next:

The plugin.yml file

Set the File Name as plugin, let the name of the folder be src, and click on Finish. Your project's tree structure should now look like in the following screenshot:

The plugin.yml file

The plugin.yml file was created in the default package. This is where it needs to be so that Spigot can find it. For now, we will fill in the plugin.yml file with the most basic settings. The plugin.yml file must include the name of your plugin, its version, and its main class. We have already determined the name and main class, and we will give it a Version number of 0.1.

Tip

If you wish to learn more about version numbers, Wikipedia has a great article on this at http://en.wikipedia.org/wiki/Software_versioning.

The simplest form of plugin.yml is as follows:

name: MyFirstBukkitPlugin
version: 0.1
main: com.codisimus.myfirstbukkitplugin.MyFirstBukkitPlugin

That is all that you need in this file, but some other fields that you may wish to add are author, description, and website. We are done with this file. You can save and close plugin.yml.

The plugin's main class

We need to modify the main class. Open MyFirstBukkitPlugin.java if it is not already open. We do not use the main method in plugins. Hence, we will delete that section of the code. Now, you will have an empty Java class, as shown in the following code:

package com.codisimus.myfirstbukkitplugin;

/**
 *
 * @author Owner
 */
public class MyFirstBukkitPlugin {

}

Tip

You may see additional comments, but they will not affect how the program is executed They are there for anyone who may be reading the code to help them understand it. It is always a good idea to comment on the code that you write. If someone ends up reading your code, whether it is a fellow developer or yourself a week from now, they will thank you for adding comments.

The first thing that we need to do is tell the IDE that this class is a Bukkit plugin. To do so, we will extend the JavaPlugin class by adding extends JavaPlugin immediately following the class name. The modified line will look like the following piece of code:

public class MyFirstBukkitPlugin extends JavaPlugin {

You will see that a squiggly line and a light bulb appear. This will happen a lot, and it usually means that you need to import something from the Bukkit API. The IDE will do this for you if you ask it to do so. Click on the light bulb and import JavaPlugin from the Bukkit library, as shown in the following screenshot:

The plugin's main class

This will automatically add a line of code near the top of your class. Right now, you can install this plugin on your server, but it will of course not do anything. Let's program the plugin to broadcast a message to the server once it is enabled. This message will show up when the plugin is enabled as we test it. To do this, we will override the onEnable method. This method is executed when the plugin is enabled. Mimic the following code to add the method:

public class MyFirstBukkitPlugin extends JavaPlugin {
  public void onEnable() {

  }
}

You will see another light bulb that will ask you to add the @Override annotation. Click on it to automatically add the line of code. If you were not prompted to add the override annotation, then you may have misspelled something in the method header.

We now have the base of all of your future plugins.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.82.23