[Tutorial] Bukkit coding tutorial for starters 1

Discussion in 'Bukkit Discussion' started by Laxer21117, Jun 23, 2014.

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

    Laxer21117

    Hey guys it is Laxer2117 and today I am going to be giving you a quick bukkit tutorial for starters!

    First things first is just the basic setup of the plugin. You are going to need to download either eclipse, Intellij, Or netbeans. Download links will be at the bottom.

    To start creating the plugin you are going to need 3 things. The java project, A package, And a class. (I will be using eclipse for this tutorial.)

    To start click file

    FileEclipse.PNG

    Once you click file you will see "new". Hold your mouse over new and click "JavaProject". (Sorry I was unable to get pictures for this).

    When you click JavaProject it will then open the menu below. You will then type the name of your project in that menu.

    ProjectName.PNG

    After that your project will be created and you will see this folder..

    JP.PNG

    Once you open that folder you will find "src".

    src.PNG

    Right click "src" and click "new" then click "package". When you click package you will see this:

    package.PNG

    You are going to want to name you package "me.YOURNAME.MyProject" like this:

    me.myproject.PNG

    Next thing you are going to do is right click your "package". Once you have right clicked your package click new again. But this time click "Class". When you have clicked "Class", You will see this:

    Class.PNG

    Where it says "name" you are going to want to put "Main". (Most tutorials recommend this).

    ClassName.PNG

    If you have followed all of the directions above, Your project should look like this:

    ProjectSetup.PNG

    But you still are not done. You need to go to download craft bukkit (Link at bottom). Once you have downloaded craft bukkit DO NOT OPEN IT!!! Just save it to a folder and keep it there. To import craft bukkit to your project you need to right click you project then go to the bottom and click "Properties". Once you are inside of properties it should look like this:

    properties.PNG

    On the side you will see "Java Build Path" Click that. and go to "libraries". Once you are in libraries you will click "Add External Jars". Then add your bukkit.jar into the project and click "Ok".

    You have now completed the first step to setting up your bukkit plugin! Time to code! :)

    To start coding you need to open up "Main.java".

    Now it is time to code. Inside of "Main.java" You will see this:

    Code:java
    1. package me.Laxer2117.MyProject;
    2.  
    3. public class Main {
    4.  
    5. }
    6.  


    To start you need to add "extends JavaPlugin" Like this:

    Code:java
    1. package me.Laxer2117.MyProject;
    2.  
    3. public class Main extends JavaPlugin {
    4.  
    5. }
    6.  


    When you have written the code you will see that JavaPlugin is highlighted with a red squiggly line. Hover your mouse over that and click import "org.bukkit.plugin.java.JavaPlugin". So now the code should look like this:

    Code:java
    1. package me.Laxer2117.MyProject;
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4.  
    5. public class Main extends JavaPlugin {
    6.  
    7.  
    8. }



    Now we will create an onEnable and onDisable method. To create them you will type this:

    Code:java
    1. public void onEnable() {
    2. Logger log = Logger.getLogger("Minecraft");
    3. log.info("MyProject has been enabled!");
    4.  
    5. }
    6.  
    7. public void onDisable() {
    8. Logger log = Logger.getLogger("Minecraft");
    9. log.info("MyProject has been disabled!);


    Once you have done that your code should look like this:

    Code:java
    1. package me.Laxer2117.MyProject;
    2.  
    3. import org.apache.logging.log4j.Logger;
    4. import org.bukkit.plugin.java.JavaPlugin;
    5.  
    6. public class Main extends JavaPlugin {
    7.  
    8. public void onEnable() {
    9. Logger log = Logger.getLogger("Minecraft");
    10. log.info("MyProject has been enabled!");
    11. }
    12.  
    13. public void onDisable() {
    14. Logger log = Logger.getLogger("Minecraft");
    15. log.info("MyProject has been disabled!");
    16. }
    17. }


    Now that you have your onEnable and onDisable methods we can move on to creating a command.

    To create a command you are going to want to type this:

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    2. if (cmd.getName().equalsIgnoreCase("MyProject") {


    That is what your code should look like when you make the command. Do not add a "/" into the code otherwise in game you have to type "//Command". Now I will explain what the code for the command means. When you make "if (cmd.getName().equalsIgnoreCase("Command") {" You are saying find the command sender "cmd.getName()" and then make sure he can type it in any CaSE ".equalsIgnoreCase". By type it in any case I mean that he can type it with uppercase or lowercase letters. Then after "equalsIgnoreCase" You have typed the command "("Command") {"
    Now that the command is setup you can make the action that the command executes.

    Code:java
    1. sender.sendMessage(ChatColor.GREEN + "You typed MyProject!");




    That has sent the player a message that says "You typed MyProject!" in the chatcolor green. The reason we type sender is because we haven't defined a player so to define a player just type
    Code:java
    1. Player player = (Player) sender;
    That defines the player and you can then type "player.sendMessage". You can now hover "CommandSender and Command" with your mouse and import "org.bukkit.command" for both of them. You will now add a return statement at the bottom like this:




    Code:java
    1. sender.sendMessage(ChatColor.GREEN + "You have typed MyProject!");
    2. }
    3. return false;
    4. }
    5. }



    You have just coded your first plugin and it should now look like:

    Code:java
    1. package me.Laxer2117.MyProject;
    2.  
    3. import org.apache.logging.log4j.Logger;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Main extends JavaPlugin {
    10.  
    11. public void onEnable() {
    12. Logger log = Logger.getLogger("Minecraft");
    13. log.info("MyProject has been enabled!");
    14. }
    15.  
    16. public void onDisable() {
    17. Logger log = Logger.getLogger("Minecraft");
    18. log.info("MyProject has been disabled!");
    19. }
    20.  
    21. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    22. if (cmd.getName().equalsIgnoreCase("MyProject")) {
    23. sender.sendMessage(ChatColor.GREEN + "You have typed MyProject!");
    24. }
    25. return false;
    26. }
    27. }
    28.  


    Wait!!! You still have now finished! You need your plugin.yml!!!! To create the plugin.yml you need to right click you project and click "new" then click "file" once you have clicked file you need to name it "plugin.yml" After that it should open up a text editor and you need to type everything that I type in the plugin.yml exactly how I type it. The plugin.yml should look like this:

    Code:
    name: MyProject
    main: me.YOURNAME.PROJECTNAME.Main
    version: 1.0
    description: On command it sends player a message.
     
    commands:
      MyProject:
        description: Sends message.
    Now the final step! We need to export it. (Sorry guys I am unable to upload anymore pictures so you need to read this carefully). Right click your project and you will see "Export" click on export and it will open a menu. In that menu you will see "Jar File". Click "Jar File" and you will see some select boxes. For the ones at the top you need to make sure you have "Export generated class files and resources" I only have that one selected. At the bottom I always select "Compress the contents of the jar file" and "Overwrite existing files without warning". After that make sure you have the destination you want it saved to and the name. After that you are done!!!

    You have just created your first bukkit plugin! :) :) :)

    I hope this helped! :D :D :D

    http://dl.bukkit.org/
    http://www.eclipse.org/downloads/
    https://netbeans.org/downloads/
    http://www.jetbrains.com/idea/download/
     
  2. Offline

    MrSparkzz

    Laxer21117
    This is pretty much a copy of the Official Bukkit Plugin Tutorial. Thank you for trying to help beginners, but I feel that you just made a tutorial to make a tutorial. I would understand if it were more advanced or actually different from the official tutorial, but it's not.

    I was planning on making an advanced tutorial series for YouTube, but I've held off on it because I want to make sure I'm doing it right. I'm taking my time, also I need a mic that doesn't pick up every little background noise.
     
  3. Offline

    Laxer21117


    I am sorry you feel this way but I wasn't even thinking of that tutorial at the time that I was making it.
     
  4. Offline

    Dpasi314

    Laxer21117
    Though you do go through the basics you don't really explain anything. The first thing that caught me was that you don't explain WHY you need to "extends JavaPlugin" you just say you have to do it. It's better if people know what they're doing rather than aimlessly copy-pasting code for the hell of it.
     
    MrSparkzz likes this.
  5. Offline

    Laxer21117


    Ok I will edit that now. Thanks for the constructive feedback :)

    Dpasi314
     
  6. Offline

    MrSparkzz

    Laxer21117
    I wasn't trying to be a jerk, I was just saying. There's already basically the same exact document out there, that is more in depth than yours, yet still simple and easy to understand.
     
  7. Offline

    GamerAuthority

    Thanks, I could use this. I've been wanting to learn code for around 4 months, but I'm not even near finished yet. This could be a lot more useful if you explained a little more about what I'm actually doing, but it's a start.
     
  8. Offline

    Laxer21117

Thread Status:
Not open for further replies.

Share This Page