Learning Java: Bukkit Style - Part 1 (Or how to program a minecraft plugin)
Level 40 : Master Unicorn
Level 40 : Master Unicorn
Posted on 4/15/2013 : 4/15/13 2:11 pm
Hey guys, Tux2 here, and I've just decided to start doing a series on how to make plugins! Now, you may ask why I'm not doing it as a video, and here's why: I dislike having to watch a video on everything new I want to learn as I would rather just read about it, then do it, so that's the style of approach I'm going to be taking (for now). So, let's get started!
What you will need to have in order to follow along in this series:
- A current version of the Java 7 JDK (Java Developers Kit): Download Here
- Eclipse, the IDE (Integrated Developer's Environment), get the "Eclipse IDE for Java Developers" : Download Here
- An up to date copy of Craftbukkit: Download Here
- The Minecraft client (Of course!): If you need a download link for this, then you probably shouldn't be doing this tutorial...
- A ready mind, willing to follow instruction and not skipping ahead and missing important parts.
Setting Up Your Environment
A properly set up environment is extremely important and shouldn't be overlooked. Although I know you're probably a bit impatient to get coding right away, I'd rather make sure your environment is set up properly so that you can code, rather than wondering why nothing is working quite right. Please note that in this tutorial series I will be using Windows 7 Home Premium to illustrate everything, as that is the most common OS, although these instructions should work almost equally as well in Windows 8 or even Windows Vista, and with some adaptation in Mac OS X as well.
Step 1: Install the Java 7 JDK
Click on the link above to download the JDK, accept the license agreement, then download the appropriate package for your operating system.
Run the file once it's done downloading and install Java. (Clicking next all the way through is fine for most installations). Once done it will open up a tab in your browser asking you to register. You can safely close it as it is not required.
Once it's installed click on your start button and type in "command", then click on "Command Prompt" in the search results that come up. In the black window that comes up type in java -version then press "Enter/return" on your keyboard. It should return the version of java that is on the computer. If not, then consult the FAQ
Step 2: Install Eclipse
- Open up the location where you downloaded Eclipse. Right click on the zip file, and click on Extract All...
- Change the location of where you want the files to be extracted to to: C:eclipse, Then click the Extract button.
- When it's done it should open up the folder with all the files. Double click on the Eclipse folder to open it, then right click on the purple Eclipse icon and navigate the menu to Send To->Desktop (Create Shortcut) (You can rename the shortcut whatever you want)
- Close out of the eclipse folder and open Eclipse from your desktop. When it asks you which workspace to use, just click "OK" as the default one is perfect for what we are going to use it for today.
Step 3: Setting up your first project
- When you first open Eclipse it should present you with a welcome screen. Just close out the welcome screen tab and we can get to work. You are now staring at the main screen of Eclipse. Once we get some content in there I'll show you what everything does.
- Now, let's create or first ever plugin! Go to File->New->Java Project
- For the project name put "ExampleProject", then click on the Finish button.
- Excellent! You've just made an empty project in Eclipse, ready for us to start putting files into. Let's start by downloading the Craftbukkit jar and adding it to the project.
- Go to: http://dl.bukkit.org/downloads/craftbukkit/ and download the latest version of the CraftBukkit jar. Open up your downloads folder, then right click it and click on Copy, then click on your name near the top of the window and open up the folder called workspace->ExampleProject. Right click and click on Paste.
- Pull up Eclipse to the foreground, right click on the ExampleProject in the Package Explorer pane and click on Refresh. Open it up, and right click on the Craftbukkit file you just downloaded and go to Build Path->Add to Build Path.
- Congratulations! You are all set up to start working on your first ever plugin!
Your First Plugin
Now that you've got your environment set up and your example project in place, let's start adding some stuff to that project.
- Open up your example project and right click on "src" and go to New->Package. For the package name you want to stick to all lowercase in this format: yourname.projectname, so in this case, for me, Tux2, it would look like this: "tux2.exampleproject". Once you're done typing it in, click on the Finish button.
(A package is a logical way of separating files within a project, as well as making sure that the files in your project don't conflict with anyone else's plugins as well)
- Now that we have a package to put all of our files in right click on the package that was just created and go to New->Class. For the name put "ExampleProject". For the superclass click on the browse button and type in "JavaPlugin" in the dialog box that pops up and double click on the first result that comes up, followed by the OK button. Finally, click on the Finish button in the main dialog box. At this point your window should look similar to the following:
- You're doing great! But, our project still doesn't do anything yet, so let's add the onEnable, onDisable, and a few other functions.
- Go to Source-->Override/Implement Methods. In the dialog box, check the boxes next to onCommand, onDisable, and onEnable. Then click the OK button.
- At the moment, the code should look like this:
Let's remove all those lines with super in them to make the code look like this:
- Excellent! We've now got a good base to extend our plugin on, but we're still missing one file: the plugin.yml file that goes in every single plugin. It tells Bukkit where to look for to find out how to run our little plugin we made. So, let's make it. Right click on your src folder, then go to New->File. For the file name put "plugin.yml", then click on the Finish button.
- Now we have the file, but there's still nothing in it! Put the following information in it:
So, let's break down what you just put in that file:
-For the Name, you put ExamplePlugin. That's the name that bukkit will show everyone when you do /plugins in minecraft.
-For the main, you put yourname.exampleplugin.ExamplePlugin. Now, the first part should correspond to your package name, so for me it would be: tux2.exampleplugin.ExamplePlugin, now, you might be wondering about the last part. That's the class file name, and tells Bukkit which file exactly it should load.
-Version? Well, that's just the plugin version.
- Save all the files, this is a good time to take a break. In part 2 of the tutorial we're actually going to start working on the plugin files we created and actually make it do something, so stay tuned!
So far you've set up your Java environment, installed Eclipse, and made a project file. Now, you may not know what they all do yet, but we'll be showing you all the ropes and what everything is in part 2. If you like the tutorial please remember to diamond. If you have any questions, or comments about the material covered in this part feel free to leave a comment below. Until next time, Tux2, over and out!
Part 2 - Hello World! >>
|Tags:||Tutorial, Tux2, Javal, Bukkit, Programming, Howto, How, Part1|