LearnToProgram

Learn to program!

Latest Tutorial: Bukkit Plugin Tutorials. Language: Java

This tutorial will teach you how to create basic & advanced Bukkit plugins.

This tutorials requires little Java knowledge. You must know what methods, if statements and variables are in Java. To learn Java(This is a tutorial on Bukkit plugins not Java) we suggest watching TheNewBostons videos.


To start off, install Eclipse Java IDE, and make sure you have Java JRE 1.7 installed as well. Im not going to tell you how to install it as their are many tutorials online on how to install it.

Once you have that installed start it up, select your workspace location and click ok.

Making a project in Eclipse

Click the File button in the top left corner, then move over to new and then click Java Project. In the project name put the name of the plugin you would like to create. Im going to call it Tutorial. Make sure Java SE 1.7 is selected in environment JRE.

Creating a package and class file

Right click your project you just created go to new and select Class. In the name box put Main, in the Package box Im going to put me.ledship.tutorial but you may want to put something such as me.YourUsername.tutorial then click finish, its a Java convention is to use a domain name you own, whenever I create plugins or programs for people I usually use com.ledscraft.PluginName as I own ledscraft.com. The dots in the package name represents a new folder basicly, so it using the package name me.ledship.tutorial would create a folder called me with another folder called ledship inside and so on and so on.

We now want to add the dependencies(BukkitAPI), go to http://dl.bukkit.org/bukkit/ and download the latest stable build. Then go back to Eclipse, right click your project and click properties. Then click Java Build Path on the side in the new window which pops up, if it is not on the libraries tab already then click Libraries. Next click "Add External Jars" and select the .jar file you downloaded from Bukkit's website before and click Ok.

Lets start coding!

In your Eclipse program you want to add the following code to the end of

public class Main

Add extends JavaPlugin and import it by hovering your mouse over it and clicking Import JavaPlugin

Your code should now look something click this

package me.ledship.tutorial;

import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin {

}

Printing messages on start!

To make it print a message when the plugin starts we need to create a onEnable method.

Type:


	public void onEnable() {
		
	}
	

This method will do what ever is in the curly brackets on the start of the plugin.

Then put this before "public void onEnable() {" but after "public class Main extends JavaPlugin {"


	private static String name;
	private static String version;
	public static Logger log = null;
	

This is just defining the variables we will use in a minute. "private static String name;" This creates a string called name which is private(Meaning it can not be accessed in other class's) and makes it static.

"private static String version;" is the same as the name however this time its called verison.

"private static Logger log = null;" creates a public(it can be accessed through other classes) Logger(A way to print out messages in the console and such) called log which is null(Meaning it is equal to nothing). You must import Logger from java.util the same way you did with JavaPlugin

Your code should now look something like this:


	package me.ledship.tutorial;

import java.util.logging.Logger;

import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin {
	
	private static String name;
	private static String version;
	public static Logger log = null;
	
	public void onEnable() {
		
	}
	
}

Now we print the message!

In your onEnable method add


		name = this.getDescription().getName();
		version = this.getDescription().getVersion();
		log = Logger.getLogger("Minecraft.tutorial");
		log.info(name + " " + version + " enabled.");
		

As you can see this is setting name equal to the name we will define in the plugin.yml later in the tutorial. The same goes with the version however it is setting it equal to the version of the plugin which we will add in the plugin.yml

It is then setting our log variable to get Minecraft.Tutorial(You do not need to know exactly what this does but you must remember to put it in all your plugins if you are doing this)

It is then printing the name and version and saying it is enabled

However it wont work unless we create a plugin.yml.

Right click your project go to new and select File

Call the new file "plugin.yml" and click finish.

Then open plugin.yml with your favourite text editor. Inside it you want to put


main: me.ledship.tutorial.Main
name: Tutorial
version: 1.0.0
description: Tutorial Plugin
		

Where it says "main: me.ledship.tutorial.Main" this defines where the main class file(The one which has extends JavaPlugin) is so when you start up the server it can find it.

"name: Tutorial" is the name of the plugin which will showup when you do /plugins on the server and what it says when the server stats(What we programmed it to say).

"version: 1.0.0" defines the version of the plugin. This also is broadcasted on the plugins startup.

"description: Tutorial Plugin" is the description of the plugin when you do /help Tutorial it will display this

Do control s to save the plugin.yml file while in it and then right click your plugin and click Refresh, this will refresh the plugins directory to include the changes to the plugin.yml file.

Compiling the plugin!

To compile the plugin right click it and select Export

Click the down arrow next to Java and then select JAR File and then next. Make sure you have your project selected(Ticked/Box filled in with Blue)

Then select your location for where you wish the compiled plugin to be located at once compiled

Remember to have the name of the jar file and .jar on the end of the location for it to work, an example of this is where mine will be located: C:\Users\ledship\Desktop\Tutorial.jar

Then select finish and if a window pops up about saving the Main.java file select Ok.

Compiling the plugin makes it into a .jar File allowing Java programs to start it.

Thats all for this tutorial, Thanks for reading. If you have any questions, queries or found something wrong in this tutorial please feel free to contact us.