Minecraft Blogs / Tutorial

Learning Java: Bukkit Style - Part 2 (Hello World!)

  • 6,886 views, 2 today
  • 7
  • 3
  • 18
Tux2's Avatar Tux2
Level 42 : Master Unicorn
52

Prerequisites

  • Completion of part 1 of this tutorial: Link to part 1
  • A ready mind ready to learn and listen to instructions.

Hello World Minecraft!



The big boss server owner (BBSO) messages you on Skype; "I hear you've been learning Java," he starts off with, "I'm needing a really simple plugin made for my server."

You're skeptical about the whole thing, but reply, "What type of plugin do you have in mind?"

"I want it to say Hello Minecraft! in chat every time a user does /hi in chat. Think you can make it?"

"Sure," you reply, "piece of cake!"

"Good," he replies, "we needed it yesterday."

Knowing how impatient the BBSO can be you fire up Eclipse and get right to work.

Some Real Code



Up to now you've been building a base, but today we're actually going to write some code that actually does something in Minecraft, the fabled "Hello World," the first program that any programmer writes in any new language. The last time we left off with the skeleton code needed for the plugin, but now we are going to flesh it out. First, open up the plugin.yml that you added in the last tutorial and add the following lines:


commands:
hi:
description: Says Hello Minecraft! to the server.
usage: /hi



The plugin.yml should now look like this:
examplepng

Now that you've got it added to the yml, we are going to add the actual code to the ExampleProject.java file. Under the onCommand() function add this line:


sender.sendMessage("Hello Minecraft!);
return true;


Also, delete the "return false;" line. Your ExampleProject.java file should now look like the following:
examplepng

Excellent, just make sure to save it! We are now ready to package it in a jar, ready to run on your Minecraft server. Right click on the ExampleProject folder and click on Export. This should open the Export dialog box. Select the Java->Jar File option as shown.
examplepng
Now click next. On the next window uncheck the .classpath and .project files, and tell it where you want to save the resulting jar file, then click on finish. The dialog box should look similar to this when you're ready:
examplepng

Tah dah! You've just successfully created your first plugin. Let's load it up in Minecraft and make sure there's no errors.
examplepng

If you run into any errors, make sure to look over the tutorial again and make sure everything is done correctly. Also, make sure that you have the correct indentation in the plugin.yml file, as that can throw everything off.

So, What Does it all Do?

With all of this copying, pasting, and running, you probably don't know what much of anything really does inside the plugin. Well, let's take a look at it:
package tux2.exampleproject; - this is the first line in the file and identifies the package that the file belongs in.
import org.bukkit.plugin.java.JavaPlugin; - The import lines tell your plugin which files to look in for the stuff that they need and the classes that they want.
public class ExampleProject extends JavaPlugin - This can actually be broken down into several parts, and declares the class file. Every single file in java stands for a separate class file. When you compile the files they actually go into .class files.
  • public - This declaration means that any other java class can reference this class.
  • class - It's a class declaration
  • ExampleProject - The class name
  • extends JavaPlugin - If this class extends the functionality of another class file, which class file is it? In this case we are extending the JavaPlugin class from bukkit.


{} - These denote a code block. Anything between them belongs to what is right before them. So for a class file, everything inside of these belongs to that class file, for a for loop, anything inside them would be for the for loop, etc.

; - No, that's not an incomplete smiley face, the semi-colon ";" ends a statement, as statements can span multiple lines in java for readability purposes.

@Override - This is optional and tells the compiler that we are overriding a function in the parent class (JavaPlugin), and it is an easy way to spot code changes if they ever change that function in Bukkit.

public boolean onCommand(CommandSender sender, Command command, String label, String[] args) - This is a function declaration and is another one that we need to take apart and examine part by part, as there is quite a bit here.
  • public - Yup, we covered it above, this means that any class that uses this class can access this function. There's also the private and protected statuses that you can apply to classes, functions, or variables, but we aren't going to cover those until a later tutorial.
  • boolean - Oh, what's that? That's the return type for this function. This lets the other class that runs it know what it's going to return it. In this case it's a boolean, which returns either true or false, (yes or no). There's other return types that we will cover, like String, int, float, void, etc.
  • onCommand - this is the name of the function and what other classes/functions will use to use it.
  • (CommandSender sender, Command command, String label, String[] args) - These are all of the variables, with what they are going to be named in the function that the calling function needs to supply. So for this one we get who sent the command, the command itself, the command label, and a list of arguments after the command. We'll cover these more in depth in a later tutorial.


sender.sendMessage("Hello Minecraft!"); - this uses the sender instance of the CommandSender class, and calls the sendMessage function in it, with the string "Hello Minecraft!", which sends the message to the user.

return true; - this returns the success or failure to the function that called it.

public void onDisable() - This is the function that gets called when the server is shut down, or when the /reload command is done. We put any cleanup we need to do in this function. It doesn't return anything, so the return type is void.

public void onEnable() - This function gets called when the server is starting and when the /reload command is done in game. Anything that needs to be done when enabling your plugin should be done here. Since it doesn't return anything the return type is void.

Summary



In this lesson you learned how to add a command, some basics about plugins and the java programming syntax, as well as packaging your plugin into a jar file and actually running it on a server!

Coming Up



Next we will be taking an in depth look at classes, functions, and variables, as well as all the different return types for functions, and how they all work together.

<< Part 1 - Intro | Part 3 - FUNCTIONal Java >>
Tags

1 Update Logs

Update #1 : by Tux2 04/25/2013 9:15:34 pmApr 25th, 2013

Added link to part 3

Create an account or sign in to comment.

1
03/24/2016 4:20 pm
Level 1 : New Miner
cminesgold_YT
cminesgold_YT's Avatar
Hey so I know this post is like a year old, but I really need help. I did all of the stuff correctly, but I cant tell if I screwed up putting it in my plugins folder or something, but its not working. Can you please help?
1
01/21/2015 10:43 am
Level 1 : New Explorer
Cotton0419
Cotton0419's Avatar
Hi! I know this is a bit old, but I'm going to try to get help anyways. My layout is exactly same (name's different, obviously). Everything's right, I install the plugin onto my server, type /hi, only to say "Unknown Command". Help?
1
02/14/2015 6:46 am
Level 2 : Apprentice Miner
ShadowCraft3
ShadowCraft3's Avatar
I get the exact same thing...
1
03/28/2014 7:49 pm
Level 1 : New Crafter
Graxwell
Graxwell's Avatar
Slight problem: The text ("Hello Minecraft!); Is missing a quotation mark on the end. this caused no end of havoc for me, until I figured it out...
1
03/21/2014 3:11 am
Level 1 : New Miner
supergomguy
supergomguy's Avatar
Hi thanks so much for this tutorial. Its really helpful. But when i try and use the plugin in a minecraft server it has an error and doesnt work?? Can you help please. Is it because im running beta bukkit files maybe?
1
03/26/2014 2:53 pm
Level 42 : Master Unicorn
Tux2
Tux2's Avatar
Without the error from console I can't tell you what's going on. The console gives you quite a few clues as to why it failed.
1
11/27/2013 12:29 am
Level 52 : Grandmaster Blob
gabe4356
gabe4356's Avatar
When I try to add multiple commands, would I make another class file for that command?
1
08/18/2013 6:03 am
Level 23 : Expert Nerd
Maxonomous
Maxonomous's Avatar
This did not help me at all. When I run the plugin I get all of these ";" and "}" Syntax errors.
1
08/19/2013 11:00 pm
Level 42 : Master Unicorn
Tux2
Tux2's Avatar
It sounds like you messed up your coding.
1
07/24/2013 3:53 pm
Level 19 : Journeyman Network
Arrxzon
Arrxzon's Avatar
will you be continuing this ? really enjoyed the first 3 parts
Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome