- Building Minecraft Server Modifications
- Cody M. Sommer
- 699字
- 2021-08-04 10:09:53
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 your future projects will start with the creation of these two files.
The plugin.yml file
Now we are ready to start programming a Bukkit plugin. The first file that we need is plugin.yml
. This is the file that the CraftBukkit server will read to determine how to load your plugin. Right click on Source Packages and click on New | Other... as shown in the following screenshot:

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

Set the File Name as plugin
, leave the folder as src
and click on Finish. Your project tree structure should now look as shown in the following screenshot:

plugin.yml
was created in the default package. This is where it needs to be so that CraftBukkit can find it. For now we will fill in the plugin.yml
file with the most basic settings. Your plugin.yml
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 make it Version 0.1.
Tip
If you wish to learn more about version numbers, Wikipedia has a great article at http://en.wikipedia.org/wiki/Software_versioning.
The simplest form of plugin.yml
is shown as follows:
name: MyFirstBukkitPlugin version: 0.1 main: com.codisimus.myfirstbukkitplugin.MyFirstBukkitPlugin
That is all you need in this file, but some other fields that you may wish to add are author
, description
, and website
. We are done with that file, so you can save and close plugin.yml
.
The plugin's main class
Now we need to modify our main class. Open MyFirstBukkitPlugin.java
, if it is not already open. We do not use the main
method in our plugins, so 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 executes. 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 any 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 these comments.
The first thing that we need to do is tell our IDE that this class is a Bukkit plugin. To do so, we will extend the JavaPlugin
class by adding extends JavaPlugin
between MyFirstBukkitPlugin
and {
. The modified line will look as shown in the following piece of code:
public class MyFirstBukkitPluginextends JavaPlugin {
You will notice 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 tell it to. Click on the light bulb and import JavaPlugin
from the Bukkit library, as shown in the following screenshot:

This will automatically add a line of code near the top of your class. As of right now, we could install this plugin on your server, but of course it will 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 notice another light bulb that will inform 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 spelled something wrong in the method header.
We now have the base of all of your future plugins.