Bukkit Coding – (3) Hello World

Before we continue, it’s probably a good idea to test your plugin before continuing, to make sure that you’ve set everything up correctly. In this section, you’ll learn how to do what most programmers learn how to do when they learn a new language. You’ll be creating a plugin that says “Hello World” in the console when you start your server. Now, that may not seem like a huge deal, but it’s always a good idea to test to make sure you’ve set everything up correctly. It is now time to write our first line of code. Well, actually, we’re just adding to a line of code that’s already there.

Note: All changed code will be italicized.

extends JavaPlugin will pretty much tell our plugin that we’re going to be using some predefined code from Bukkit in our plugin.

After typing that, you’ll notice there is a red squiggly line below JavaPlugin. This means there is an error with that part of your code. Don’t worry, this is an easy fix. If you hover over JavaPlugin, you’ll notice Eclipse gives you the option to import JavaPlugin. Click on the import button to do so. You should now notice an extra line generated above your code.

Now, we’re going to add our own method inside of our main method.

public void onEnable(){ – Anything in the body of this code, will be run when the plugin has been loaded. This is where we’ll output our text to the console. Notice how there is an opening and closing curly brace, marking the start and finish of the body.

Next, inside of your onEnable() body, you’ll need to output text to the console. There are a few ways we can do so, but we’re going to do the most popular method. This is probably the method you should use.

getLogger().info(“Hello World!”); – This code is the “meat” of our test plugin. This is what will actually display “Hello World!” in our console. Noce the semi-colon at the end of this line.

Now’s probably a good time to mention a key feature of Eclipse, if you haven’t figured it out already. As you typed the previous line of code, you should have noticed something pretty cool after you typed getLogger(). Eclipse tried to guess what you wanted to type next.

You’ll notice that this feature will become pretty handy as you begin to write code on your own. You can use the up and down arrow keys to scroll through the options, and then ENTER to make a selection.

That’s all the main code you’ll have to write in this section. Our next step, is to create a plugin.yml file. This file will tell Bukkit things such as our plugin name, version of our plugin, and what package our main code is in. To create a plugin.yml, start by right clicking on your project in your Package Explorer, and going to New -> File. This will open up the New File Creation Window. Just fill out plugin.yml under File Name, and click Finish.

There are only three lines that you must type in your plugin.yml.

name – This is the name of your plugin. This is what will appear when you type /plugins on your server.

main – This is the full location of your class file. <PackageName>.<ClassName>.

version – This is just the version of your plugin.

Now, the only thing that’s left to do is to export your plugin as a Jar file. To do so, right click on your Java Project, and click on Export. Next, goto Java -> JARFile, and click next. On the next window, be sure that your project is selected, and browse for where you’d like your jar file to be saved.

After you click finish, you’re all good to go! Simply put your Jar file in your servers plugins folder, and start it up.

Congrats! You’ve made it through your very first plugin. Now that wasn’t very hard, was it?

Be the first to comment

Leave a Reply

Your email address will not be published.