Plugin Development Tutorial For Newbs

Discussion in 'Resources' started by deasertman, Mar 24, 2014.

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

    deasertman

    Hello there bukkit users! I'm a plugin developer, and when I was still a beginner, I was very clueless of what to do, I looked up some tutorials and I was too stupid to understand many of them, so I'm here today to create a very simple tutorial which will help beginners in developing their first plugin. I hope this helps.

    [Note] This tutorial will only help you gain very basic knowledge. To become better at this, it's up to you to figure out everything else.
    Now let's get started, shall we?
    ---------------------------------------------------------------------------------------------
    Items Needed
    ---------------------------------------------------------------------------------------------
    ---------------------------------------------------------------------------------------------
    Creating A New Project
    ---------------------------------------------------------------------------------------------
    1. Once you've installed eclipse, open it, and goto File > New > Java Project.
    2. Insert your project's name, then click Next.
    3. Press Libraries.
    4. Press External Jars and browse to bukkit's latest dev build which you've downloaded.
    5. Press Finish.
    6. Go to the package explorer on the left of eclipse.
    7. Right click your project's name and click New > Package.
    8. Name it anything you'd like. (Don't use spaces) I prefer to use "me.username.pluginname".
      Actually refer to this for naming your package. If you don't understand, just name it what I've posted above.
    9. Press Finish.
    10. Right click your package and click New > Class
    11. Name your class MyPlugin.
    ---------------------------------------------------------------------------------------------
    Getting Started
    ---------------------------------------------------------------------------------------------
    Your main class should look something like this:​
    Code:java
    1. package your.package.name;
    2.  
    3. public class Main {
    4.  
    5. }


    Change it to:
    Code:java
    1. package your.package.name;
    2.  
    3. public class Main extends JavaPlugin implements Listener {
    4.  
    5. }


    Now, this is what the plugin does when it's enabled or disabled:
    Code:java
    1.  
    2. public void onEnable(){
    3. getServer().getPluginManager().registerEvents(this, this);
    4. //Registers the events we're going to listen to later on. [Discussed under]
    5. }
    6.  
    7. public void onDisable(){
    8. getLogger().info("Oh noes you disabled me!");
    9. //Sends a message to the console saying it was disabled
    10. }

    ---------------------------------------------------------------------------------------------
    Handling Events
    ---------------------------------------------------------------------------------------------
    Events are triggered when a certain thing occurs, such as a player joining, or an entity dying. Events can be found here: http://jd.bukkit.org
    In this plugin, we will be handling the PlayerJoinEvent.​
    So right now, our code should look like this:​
    Code:java
    1. package your.package.name;
    2.  
    3. public class Main extends JavaPlugin implements Listener {
    4.  
    5. public void onEnable(){
    6. getLogger().info("Enabled");
    7. //Sends a message to the console saying it was enabled
    8. getServer().getPluginManager().registerEvents(this, this);
    9. //Registers the events.
    10. }
    11.  
    12. public void onDisable(){
    13. getLogger().info("Disabled!");
    14. //Sends a message to the console saying it was disabled
    15. }
    16. }


    Now what we will add, will listen to the playerjoinevent, and will send a message to the player who has joined. Note that you need to always put "@EventHandler" every time you're going to listen for an event. If you have multiple, you need to put it before every one.

    Code:java
    1. @EventHandler
    2. public void onJoin(PlayerJoinEvent event) {
    3. //Listens for the PlayerJoinEvent
    4. Player player = event.getPlayer();
    5. //Declares a variable, player, which is the player who joined.
    6. player.sendMessage("Welcome," + player.getName());
    7. //Sends the player a message. getName() is the function which gets the name of the player.
    8. }


    When you want to listen for a different event, just replace PlayerJoinEvent with the event name you want. Note that some events can be cancelled, such as the PlayerPickupItemEvent. You can simply cancel it by typing:
    Code:java
    1. event.setCancelled(true);


    Now the final code should look like this:

    Code:java
    1. public class Main extends JavaPlugin implements Listener {
    2.  
    3. public void onEnable(){
    4. getLogger().info("My First Plugin has been enabled!");
    5. //Sends a message to the console saying it was enabled
    6. getServer().getPluginManager().registerEvents(this, this);
    7. //Registers the events
    8. }
    9.  
    10. public void onDisable(){
    11. getLogger().info("My First Plugin has been disabled!");
    12. //Sends a message to the console saying it was disabled
    13. }
    14. @EventHandler
    15. public void onJoin(PlayerJoinEvent event) {
    16. //Listens for the PlayerJoinEvent
    17. Player player = event.getPlayer();
    18. //Declares a variable, player, which is the player who joined.
    19. player.sendMessage("Welcome," + player.getName());
    20. //Sends the player a message. getName() is the function which gets the name of the player.
    21. }
    22. }


    Most of this code when pasted into eclipse will be underlined, that is because you need to import them from bukkit. To import them, just hover over the underlined sentence/word and press "Import ___ ".

    If you need help you can message me on skype: deasertman.69. Or you can message me on twitter, @deasertman.

    I hope you guys enjoyed this tutorial. I will make sure to update it with help for plugin.yml and commands, with photos, and maybe even a video! If you have any fixes / suggestions please do comment them. Cheers, deasertman.

     
  2. Offline

    RainoBoy97

    Nice guide.

    You forgot the @EventHandler in the final code block, and you never registered the event :p
     
    AoH_Ruthless and Jumla like this.
  3. Offline

    deasertman

    RainoBoy97 Ah yes, I missed that. Thanks, I will fix it right away!
    Edit:
    Fixed! Thanks for pointing that out :)
     
  4. Well...
     
    Garris0n likes this.
  5. Offline

    deasertman

    Ribesg Well, what's that supposed to mean >_>
     
  6. Offline

    Jumla

    Not exactly...
    That will do nothing.
    You mean event.setCancelled(true);



    With all due respect, you aren't at a place where you should be teaching others in my eyes - you're still learning yourself.

    Try to gain a bit more experience before trying to spread it.
     
  7. You're still a total beginner, in Java and Bukkit. You don't know nothing, but you're clearly not at a level at which you can write tutorials.
    • Indentation is broken
    • You're logging messages onEnable and onDisable are completely useless, Bukkit already does that for you
    • event.setCancelled() == true
    • Linking to latest dev build
    • Class called Main
    • Suggesting package naming the wrong way (should follow standards)
    • I don't have more time to dig into what you did
    Also, there is already a good tutorial on the wiki.
     
  8. Offline

    deasertman

    Jumla Yeah sorry about that, I'm tired and it seems I'm doing alot of mistakes. And hey, I know I'm not that much of a good coder, but I don't see any harm where I'm trying to help others learn how to code. I do know what I'm posting, I'm just tired right now. Besides, if there's any mistake I'm sure people will tell me, and I'd try to fix it right away.
     
  9. The problem is that actual beginner will come here and copy your choices, like you certainly copied your logging things from an outdated tutorial. They will copy your errors!
     
  10. Offline

    Jumla

    I understand and I do appreciate the effort. I just want to prevent the spread of false information through the community. You are obviously dedicated, and I hope you republish this when you have a bit more experience.
     
  11. Offline

    deasertman

    Ribesg I can certainly say I haven't copied my "logging things" from another "outdated" tutorial. I just saw a tutorial which said it and I've done it everytime I've coded. I understand I'm a beginner, but you see, you and others stated my mistakes and I'm fixing them so that others can. Plus most of my mistakes are just stupid mistakes, since I'm tired. And honestly I have no idea how I can indent in bukkit posts (in the code things). Though I do have a question: why is creating a class called Main wrong?
     
  12. Offline

    drtshock

    If anything, people are helping you learn from this ;D
     
    Niknea likes this.
  13. Offline

    deasertman

    drtshock Haha, nah. I'm just doing a plenty of mistakes due to my lack of sleep. Though I admit I can learn a few things.
     
  14. Read that sentence again :D



    Standards says a Main class is a class with a public statis void main(String[] args) method which can be executed. That's usually what anyone expects from a class called Main. But even in this case, Main is usually a bad choice. "Main" of what?

    The "main" class of a plugin should (and is almost always) called by the plugin's name.
     
  15. Offline

    deasertman

    Ribesg Oh, thanks for the info. And sorry I misunderstood your sentence, I thought it implied me copying the tutorial. Anyways I will make sure not to repeat these mistakes again.
     
  16. Offline

    AoH_Ruthless

    deasertman
    With all due respect and appreciation for the effort you put into this, I don't really see what makes this resource more special than the wiki that you yourself linked.

    http://wiki.bukkit.org/Plugin_tutorial seems to go more in-depth than you do and while the information you presented is sound, valid (with the edits you made) and succinct, it doesn't speak to me more than the wiki page does.
     
    Garris0n likes this.
  17. On this subject, the Plugin Tutorial on the wiki should be split, I think. The page is too long.
     
Thread Status:
Not open for further replies.

Share This Page