How to create a Basic Bukkit Plugin {Noob Friendly}

Discussion in 'Resources' started by Patto ottaP, Jan 23, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Patto ottaP

    __________________________
    |How To Create a Bukkit Plugin|
    |The Noob Friendly Guide to a simple Bukkit Plugin|
    |Prologue|
    If you have ever wanted to make your own Bukkit plugin, but don't know where to start? well here is your starting point. The programming language we are going to use to code our plugin is java. If you are not familiar with Java, I recommend watching some tutorials on java eclipse.
    |Requirements|
    There will be some things we will need to make our plugin, a JDK (Java Development Kit), a Bukkit Build and a Server to try our our plugin on. Press here to get Java JDK. When thats done.​
    you are all set up to create a basic plugin! So lets get coding!​
    |Setting Up Eclipse|
    There are a few things we have to do to set up Eclipse (JDK). First, we have to create a workspace. So go ahead and open up Eclipse.exe. When you do, after a few seconds, you will gat a message like shown in Figure 1-1.
    Figure 1-1:
    [​IMG]


    Just name the workspace "BukkitPlugins" or anything you want to, it doesn't really make a difference. Then a few seconds after, a window will pop up like in figure 1-2. I am using
    Windows 7, so on a Mac or Linux it may look different, but there should be a arrow which says something like "go to workbench" on it.
    Figure 1-2: The Eclipse Home Page (The arrow is on the right)
    [​IMG]

    After you click the arrow, it should take you to the workbench. Now, lets create a Plugin!

    |The 'Welcome to my server!' Plugin|

    So the plugin we are going to make is going to be called WTMS(Welcome to my server). What its going to do is display a message whenever a player joins the game. Click File > New > Java Project. In the Project name tab type anything you want but don't include things like
    Java, Bukkit, Class, etc. then click Next. Then click on the "libraries" tab then click on "Add External Jars" then find the craftbukkit.jar in your server. then click Finish.
    Hooray! Were done with all the boring stuff! so now on the left side of the screen, a thing that looks like a folder with the name you typed should have appeared like in figure 2-1.
    Figure 2-1: The Package explorer with my package named 'p'
    [​IMG]

    Right click that folder and click New > Package. Then name the package "me.YourMCName.Yourpluginname" so, mine would be "me.patrickcrowne.WTMS".​
    Then click Finish. Now your project should look similar to figure 2-2.​
    Figure 2-2: Our Project with a package
    [​IMG]
    Now, right click our package (looks like a white box with a black cross going through it) and do New > Class. Name that Class "Main" and then click Finish. When you do that some text in the middle of the screen will appear something similar to this:​
    Code:
    package me.patrickcrowne.WTMS;
     
    public class Main {
     
    }
    on the end of "public class Main", type extends "JavaPlugin"​
    Code:
    package me.patrickcrowne.WTMS;
     
    public class Main extends JavaPlugin{
     
    }
    But when you do this, a error should pop up on the left side of the screen, it looks like a light bulb. Click on it then a little menu should pop up and one of the options should be "import JavaPlugin", Click on that. your code should look like this:
    Code:
    package me.patrickcrowne.WTMS;
     
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class main extends JavaPlugin{
     
    }
    Now, we are going to type two methods, onEnable and onDisable.
    Code:
    public class Main extends JavaPlugin{
        public final MyPlayerListener PlayerListener = new MyPlayerListener(this);
      public void onDisable(){
      System.out.println(" WTMS is Disabled!");
      }
      public void onEnable(){
    PluginManager pm = getServer().getPluginManager();
    pm.registerEvent(Event.Type.PLAYER_JOIN, PlayerListener, Priority.Normal, this);
    System.out.println(" WTMS is Enabled!");
      }
    }
    I know, that seems allot to take in, but let me go over it:​
    Code:
    public final MyPlayerListener PlayerListener = new MyPlayerListener(this);
    the PlayerListener , is the thing that listens for player actions like joining the game. a red line will be under this on your screen, so click on that light bulb but click Create new Class 'MyPlayerListener'. Then a window will pop up, click finish, then click on the tab we have our main class in (Main). We will be doing more stuff with that class later.​
    Code:
    System.out.println(" WTMS is Enabled!");
    and
    System.out.println(" WTMS is Disabled!)
    Just printing the message in our server console (" Hey, My plugins enabled!")
    Code:
    PluginManager pm = getServer().getPluginManager();
    Again, just click on the light bulb and import PluginManager. This Line of code helps register the events like a player joining, a block breaking etc.​
    Code:
    pm.registerEvent(Event.Type.PLAYER_JOIN, PlayerListener, Priority.Normal, this);
    Just logging the event of a player joining, the PlayerListener will handle this and its priority is normal.​
    To Be Continued...
     
    evan9999 and Gilbertus like this.
  2. Offline

    vercuiel

    D: Continue please, helpful..

    Do we Import 'Priority'??

    Also May I Make a video if I learn??

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 23, 2016
  3. Offline

    gyroninja

    Yeah import it, make sure its from bukkit not something like AWT.
     
  4. Offline

    ThatBox

    This should be in resources. It also shows the old way of using listeners.
     
  5. Offline

    Silentspy

    Moved to appropriate forum.
     
  6. Offline

    WizzleDonker

    Move to resources (got moved while I typed this!), and the new way of using listeners would be nice :p

    Listeners work like this now (sample main class):

    Code:
    public class ListenerTest extends JavaPlugin implements Listener {
     
        public void onDisable() {
            // TODO: Place any custom disable code here.
            log("Plugin has been disabled.");
        }
     
        public void onEnable() {
            PluginManager pm = getServer().getPluginManager();
     
            //Register all the events :P
            pm.registerEvents(this, this);
     
            log("Plugin has successfully enabled.");
        }
     
        public void log(String text) {
            System.out.println("[" + this + "] " + text);
        }
     
        @EventHandler
        public void whenPlayerJoins(PlayerJoinEvent event) {
          event.getPlayer().sendMessage("You joined a server with a new event listener!")
        }
    }
    I find it a lot better than the old method of using events. Remember, you can put the event handler tag in any class which implements the Listener class, meaning that this is diverse. The RegisterEvents(this, this) method registers every event in a given class. The first argument specifies the class to register, the second the class (extending javaplugin), or the 'Plugin' to register the event to.
     
    Gilbertus likes this.
Thread Status:
Not open for further replies.

Share This Page