[Tut] Bukkit Programming For Dummies - Another Bukkit Programming Tutorial

Discussion in 'Resources' started by elfin8er, Feb 9, 2014.

Thread Status:
Not open for further replies.
  1. Currently, this is just a project for School, but I'm hoping to greatly expand off of this, and create a full guide to creating Bukkit plugins. This guide is created for those that have little to no programming knowledge. Unfortunately, the Bukkit forums don't allow the use of spoiler tags (it doesn't appear), so I'll be posting the entire thing on Google Docs. The Google Docs page is always up-to-date, is formatted nicer, has extra information, and includes pictures. It's also a good place to go if you need help because you can comment exactly where you're having trouble.

    For those of you that already know a thing or two about Bukkit Programming, feel free to take a look, and give me some feedback. I could use it.

    Preview
    Show Spoiler


    Downloading

    There's not a whole lot that any Java programmer can do, without first downloading some preliminary software. The first bit of software that we’ll need, is the Java Developer Kit, or JDK for short. This is required for any Java programmer to begin developing Java projects. The Java JDK can be found by going to HERE.

    Once there, you will have to click on the blue download button below the JDK column. After reading and accepting the license agreement, you will next have to click on your current operating system, at which point an EXE file will begin to download. This is the Java JDK installation program. Launching the program will then instruct you to install the Java JDK. This will also install the Java JRE, or Java Runtime Environment, if you don’t already have it. This will allow you to run Bukkit, and the plugins that you’ve created. Chances are, if you’re already running a Bukkit server on your computer, you already have some type of JRE.

    There we go! We now have the bare minimum of what’s required to create Bukkit plugins. However, to actually create Bukkit plugins without any more software can be very difficult. That’s where the wonderful software programmers call IDE’s come in. IDE stands for Integrated Development Environment. An IDE will give us advanced features that allow us to easily write, and compile our code. You’ll see these features once we start using our IDE.

    Although there are many IDE’s we could choose from, the most popular among Bukkit programmers, and even Java programmers, is Eclipse. You can get Eclipse HERE.

    Simply click on the Eclipse Standard download for your operating system, and run the EXE that downloads, following the simple steps to install the program. The download is quite big, over 200 MB, so download time make take a while depending on your internet speed. No worries though, I’ll be right here when the download is finished.

    While Eclipse downloads, you will need to download the Bukkit server. If you’re running your server on your local machine, you probably already have Bukkit. Otherwise, you can download it from the official Bukkit website.

    After going to that website, you will be presented with several download options. You may get either the Recommended Build or the Beta Build, but I’m going to get the Development Build. To download the Development Build, click on Alternate Versions and then click on Development Build on the right side of the page. The Development version of Bukkit should begin downloading. When Bukkit finishes downloading, be sure to move it to a place where it won’t be moved, or deleted.

    Preparation

    Once Eclipse has finished downloading, extract it’s contents somewhere you’ll be able to find it later. After running eclipse.exe, which should be included in the folder that you downloaded, you will be prompted to choose a destination for your workspace. This is where all of your files will be stored for your plugin.

    After that window closes, you should be presented with the Eclipse IDE. This is where you’ll be spending your next few days and nights, in a life-like nightmare that seems to never end. Here, you’ll be sleep deprived, starved, and dehydrated as you toil through the horrid beasts that live inside. If you’re willing to face this battle, feel free to keep reading. Otherwise, please delete Eclipse, and burn your hard drive to keep the beasts from escaping.


    Now that we’ve got that out of the way, we can continue. You should currently see the welcome screen of Eclipse. This gives a few options that allow you to explore Eclipse, and Java. Feel free to take some time to read through these. After you’re done, close out the Welcome Screen by clicking the X in the top left on the tab that says Welcome. Eclipse will now look slightly different.

    This is what Eclipse should look like after closing the Welcome screen.

    1. Package Explorer - This is where your project(s) will be displayed. This allows you to easily switch between projects, and files in your project.

    2. Code/Text Editor - This is mainly going to be where you’ll be editing your code. At some points, you may also edit plain text files.

    3. Error Reporter - This is where errors in your code will show up. Any errors in here usually means you’re doing something incorrectly.


    Now that you know the basic terms of different windows in Eclipse, we can continue with actually making our plugin. To do so, we’ll need to create a Java Project. Right click on the Package Explorer, and select New -> Java Project. You will now be presented with a window that contains what appears to be a very complicated mess, however, continuation is quite simple. Simply choose a name for your plugin, and type it under Project Name and click Finish.

    You should now have a new package in the Package Explorer. Next, we must import Bukkit in our project so that we can use it’s API in Eclipse. To do so, right click on the package in the package explorer, and go to Build Path -> Configure Build Path. At the top of that window, choose the Libraries tab, and click on the Add External Jars button on the right side of that window. You will now be presented with a file browser, where you will need to browse to Bukkit. After selecting Bukkit, and clicking Open, Bukkit will be added to your project. You may now click the OK button to close the Build Path window. We can now create our package, and start programming our plugin.

    To create a package inside of your Java Project, you’ll first need to open your Java Project. To do so, click on the white arrow to the left of your Java Project. This will open your project to view the contents of your project.

    The next step, is to create a package in our Java Project. Each Java project can have several packages, but for now, our project will have only one package. To create a package, right click on the folder that says src and click New -> Package. This will bring up the Package Creation window. Type the following into the Name text field.

    Code:
    com.<YourName>.<PluginName>

    Be sure to replace <YourName> with your name, or alias, and <PluginName> with the name of your plugin. This is traditionally how package names are setup, however there are many alternative ways of naming your package. Your package name must be unique to your plugin.

    Once you have filled out your package name, click Finish to close the New Java Package window, and create your package. Your package should now appear under the src folder in the package explorer. Now, you’ll have to create a class inside of your package. This is a plain text file that will hold your code. To create a class file, right click on your newly created package, and select New -> Class. The class creation dialog should come up. All you need to fill out is the class name. I named mine main but you can name yours whatever you’d like. A class file should now be created, with some pre-defined text. This is what mine looks like.


    Code:java
    1. package com.elfin8er.tutorial;
    2.  
    3.  
    4.  
    5. public class main {
    6.  
    7.  
    8.  
    9. }


    Let me just take a second to go through line-by-line explaining what this code does.

    package com.elfin8er.tutorial; - This is defining where this class file is located. In our case, it’s located in the package com.elfin8er.tutorial. Notice how this line of code ends in a semi-colon. This means that we’re done with that line of code.

    public class main { - This line, is like the main constructor of our code. This is where all of our plugin code will be kept. Notice the curly brace at the end of this line? This is the start of this codes body. Any code in between this curly brace, and the next curly brace, is part of this codes body. Also note how there is no semi-colon because this line has a body.

    } - This second curly brace marks the end of the body from the previous line of code.

    All of this may seem a bit complicated, but it shouldn't matter a whole lot for now.


    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.

    Code:java
    1.  
    2. package com.elfin8er.tutorial;
    3.  
    4. public class Tutorial extends JavaPlugin {
    5.  
    6. }
    7.  


    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.

    Code:java
    1.  
    2. package com.elfin8er.tutorial;
    3.  
    4. import org.bukkit.plugin.java.JavaPlugin;
    5.  
    6. public class Tutorial extends JavaPlugin{
    7.  
    8. }
    9.  


    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.
    Code:java
    1.  
    2. package com.elfin8er.tutorial;
    3.  
    4. import org.bukkit.plugin.java.JavaPlugin;
    5.  
    6. public class main extends JavaPlugin{
    7. public void onEnable(){
    8. getLogger().info("Hello World!");
    9. }
    10. }
    11.  


    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.

    Code:
    name: YourPluginName
    main: com.elfin8er.tutorial.Tutorial
    version: 1.0
    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?


    NEW!
    I'm working on creating a video for each and every section of the Google Doc. You can view the playlist HERE!
     
    Sw_aG, Kuuichi and lol768 like this.
Thread Status:
Not open for further replies.

Share This Page