Minecraft Blogs / Tutorial

Learning Java: Bukkit Style - Part 1 (Or how to program a minecraft plugin)

  • 24,138 views, 2 today
  • 38
  • 13
  • 28
Tux2's Avatar Tux2
Level 42 : Master Unicorn
52

Introduction


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



  1. Click on the link above to download the JDK, accept the license agreement, then download the appropriate package for your operating system.



  2. 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.



  3. 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

  1. Open up the location where you downloaded Eclipse. Right click on the zip file, and click on Extract All...
  2. Change the location of where you want the files to be extracted to to: C:eclipse, Then click the Extract button.
  3. 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)

  4. 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

  1. 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.
  2. Now, let's create or first ever plugin! Go to File->New->Java Project
  3. For the project name put "ExampleProject", then click on the Finish button.
  4. 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.
  5. 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.
  6. 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.
  7. 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.
  1. 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)
  2. 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:

    examplepng

  3. 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.
  4. Go to Source-->Override/Implement Methods. In the dialog box, check the boxes next to onCommand, onDisable, and onEnable. Then click the OK button.
  5. At the moment, the code should look like this:
    examplepng

    Let's remove all those lines with super in them to make the code look like this:
    examplepng

  6. 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.
  7. Now we have the file, but there's still nothing in it! Put the following information in it:

    name: ExamplePlugin
    main: yourname.exampleplugin.ExamplePlugin
    version: 0.1

    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.
  8. 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!

Conclusion



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

Create an account or sign in to comment.

1
03/26/2016 2:25 am
Level 1 : New Miner
Dragonsgaming2
Dragonsgaming2's Avatar
Man can someone help me make some plugin or teach me how to make them
1
12/28/2014 1:16 am
Level 1 : New Explorer
prince_planet_2
prince_planet_2's Avatar
Great tutorial, although some steps that the Wiki has that make programming heaps easier have been missed in this tutorial.
1
08/31/2014 7:31 am
Level 2 : Apprentice Explorer
youp0711
youp0711's Avatar
I love it
1
08/31/2014 7:31 am
Level 2 : Apprentice Explorer
youp0711
youp0711's Avatar
This is the easiest tutorial to follow for beginers
1
03/13/2014 8:37 am
Level 23 : Expert Nerd
Baamoink
Baamoink's Avatar
Came across this blog through a plug-in called HyperMerchant, who gave Tux2, & this blog specifically, credit for getting started on the plug-in that he developed.

Being pretty new to the whole Java coding environment, this tutorial is a god send. I'm surprised this blog hasn't received more attention than it has. Incredibly helpful! Greatly appreciate the time you spent in putting this together Tux2.

On to part 2!
1
10/29/2013 5:11 pm
Level 2 : Apprentice Explorer
Cis112233
Cis112233's Avatar
Thank you!
1
08/14/2013 1:20 pm
Level 27 : Expert Musician
skunkapunk
skunkapunk's Avatar
Thank you for making this! It's really helpful for people starting to make plugins for the first time.
1
07/02/2013 12:33 pm
Level 1 : New Miner
BudderPigPlaysMC
BudderPigPlaysMC's Avatar
I backspaced the text written at #2 on Your First Plugin. How do I fix this? I deleted the file.

ALSO: How do you retrieve files without the welcome screen?
Thanks!

-BudderPigPlaysMC
1
04/19/2013 9:25 am
Level 40 : Master Pixel Painter
Pixel_King
Pixel_King's Avatar
I am on Mac, is it the same thing? :3
1
04/19/2013 1:24 pm
Level 42 : Master Unicorn
Tux2
Tux2's Avatar
Installation of Eclipse and the Java JDK might be a bit different for your platform, but after installation the instructions on how to use Eclipse and program in Java apply equally to Mac, Windows, and Linux computers.
Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome