1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[Bukkit/Spigot Development] Basic Tutorial

Discussion in 'Software/Coding' started by TrinityDEV, Aug 25, 2015.

?

Want more tutorials

  1. Yes, please

    28 vote(s)
    68.3%
  2. No, go away

    8 vote(s)
    19.5%
  3. Maybe, do what you want

    5 vote(s)
    12.2%
  1. TrinityDEV

    TrinityDEV Member

    Joined:
    Aug 24, 2015
    Messages:
    126
    Likes Received:
    68
    Trophy Points:
    28
    Gender:
    Male
    Occupation:
    Student
    IMPORTANT: This tutorial is quite a long tutorial, so if you are not really interested in coding i would not recommend you to read any further. Also if you are an experienced developer dont hate its noobish coded, its coded the easy way so new coders will understand it. Thanks for understanding.

    Hey all.
    So in this thread I'll try to learn you something about coding a basic plugin for bukkit/spigot. Why I post this here? Hmm, good questions, I was just surfing around the forums and thought, why not. Its also my first tutorial, so I appreciate feedback.
    (Yes I use eclipse, because I just like it more then IntelliJ, and if you're wondering, the black theme is called MoonRise UI, with font called something like RainbowDrops)
    I'll not create a whole minigame here, or something like that, I just look into the basics of coding. You don't understand what i'm doing? I suggest you to learn basic java first, not needed if you stay with basic plugins, but if you want to make more advanced plugins its much better to know what you need to do. Or you can just comment and I will help you :)

    QUICK NOTE: English is not my main language so sorry if the grammar is badly typed, if you dont understand it, contact me and i will try to explain it better. If you are wondering, I am from The Netherlands (Europe)

    Lets get started :)

    1) Downloading Program
    You need a coding platform to create plugins for your server, in this tutorial I'll be using the program called "eclipse", it can be downloaded here in order to use eclipse you need to have Java JDK installed. You dont know how to do this? Well luckily for you, there is a tutorial about how to install it, that one can be found here

    2) Creating your project
    Now that you have launced eclipse you can see at the left a box called "Package Explorer", right click inside that box, now you will see something like this:
    [​IMG]
    (Ignore all the existing projects)
    Hover over New, and click Java Project. After you clicked that, you see a popup screen with a lot of text, its not needed to change anything in there, unless you want to change the Java version, or the workspace (where projects will save files to, and load files from)

    In the box at the top you can type the name of the plugin, for example "First Plugin", after that it will look something like this:
    [​IMG]
    Now click finish. It will take a few seconds to create your project. When its done your project will be displayed at the left (inside the package explorer. Click on the little arrow at the left of your project name, or double click it. You wil now see a map with the name "src", right click on that map, then click New > Package. A little screen will pop up now. Dont change the source folder, only type in the name of your package. Wait, not so quick, before we continue i will explain what a package is. A package is simply an sort of folder to keep your files into. We use packages to organize the classes, because in Java (almost) every time you got a lot of classes. And its a mess to keep it in one folder, so thats why we store it in packages.
    For example i got a plugin, that will teleport you to spawn on join, and a command to set the spawn. What i would do is create main package, (example name: eu.trinitydev.spawnplugin), and inside there i create a class called "Core", now i need (not really just example) 2 Classes for the commands, and 3 to handle the teleport on join. So i make 2 packages (eu.trinitydev.spawnplugin.commands and eu.trinitydev.spawnplugin.events), if you dont understand this its not that bad, you will not need this to learn to code. Interested in packages? Go check out this tutorial

    Lets continue on the plugin. So i name my package eu.trinitydev.firstplugin. The name does not really matter, most most times you use your domain, (because of copyright conflicts etc). If you dont have a domain you can just use me.yourname.pluginname, please dont use captial letters in your package name. After you typed in your package name you will got something like this:
    [​IMG]
    Now click finish.
    You will now see your package in the source folder at the left (package explorer). The small icon will look something white toast.
    Right click on your package, Click New > Class. A little box will open (again) Dont change package name etc, only type in a class name.
    It doesnt matter what you name it but it needs to start with a captial letter. Most times i name my main classes: "Core, Main or PluginName" For now i will call it Main. Click finish. The "white toast" icon will now color brown. That means its not empty anymore. Inside your package you will find your class, open it (if its not opened automatically). You thought you were done now? Nope, sadly we are not.
    We will ned to add an "external jar" to our project. In this case i will use the Spigot API. We need to do this to acces the core code of plugins. You add an external jar by right clicking your project, Buildpath > Configure buildpath. A box wil open up.. Yes again another box. You will see a button at the left called "Add external jars" click it, and look for you spigot/bukkit jar.
    [​IMG]
    If you dont have it you can download it easy, just google "bukkit jar download" or "spigot jar download"
    After you selected your spigot/bukkit jar, press OK.
    Now you are done with setting up the project, and we can finally continue to the code

    3) Loading up the plugin
    Open up your main class, that we created in the previous step
    You will see an almost empty file that will look something like this:
    Code:
    package yourpackage.firstplugin;
    
    
    public class ClassName {
    
    }
    Change the line with
    Code:
    public class ClassName {
    
    to
    Code:
    public class ClassName extends JavaPlugin {
    
    Ofcourse replace ClassName with the name of your class. Dont forget to import JavaPlugin, hover over it, or press ctrl + shift + o
    [​IMG]
    Well done, you just completed the first step. Wondering what "extends JavaPlugin" does? Very good, you are interested, nice. Let me explain it. Its simply basic code for the plugin. Servers (mostly) run using Bukkit or Spigot, and thats what we added to our buildpath. If we let this class extend JavaPlugin we basically scream to Bukkit or Spigot. "HEY, my main class is here, read this when starting up this plugin", so yeah, thats pretty much it.

    A main class always needs an method called "onEnable", what this does is basically loading up the plugin, you can add variables in here or for example loading files and stuff (more advanced coding) We just start with a basic message to log the console the plugin has been enabled.

    Create a onEnable method like this:
    Code:
    public void onEnable() {
    
    }
    
    Not hard isnt it?
    Now we add a line to message the console
    Code:
    public void onEnable() {
    getLogger().info("---> My First plugin is now launced, partyparty yeah. <---");
    }
    
    getLogger() is simply the plugin logging stuff, you could use System.out.printIn("message"), but thats just a empty message without time etc.

    Your code will now look something like this:
    Code:
    package eu.trinitydev.firstplugin;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
      
        public void onEnable() {
            getLogger().info("---> My First plugin is now launced, partyparty yeah. <---");
        }
    
    }
    
    Ofcourse again, class name and packages names will not match with yours.
    Now we are done with the basic loading up stuff. And we now continue to our first command! Yeah!

    4) Creating a command
    Spigot/Bukkit uses a simple method/boolean called "onCommand" to run commands. The onCommand method looks like this:
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]) {
            return false;
        }[/COLOR]
    
    Because its a boolean you will need a return statement at the end of the boolean. Dont understand it? Go look inside basic java tutorials
    Inside this boolean we can get the sender of the command, the command names, or command label, and the arguments. Lets start by checking if the sender (CommandSender that we assigned to variable sender in our boolean) is an player or a console. Oh, quick note again, if you got errors underlined CommandSender, and command, hover over it and import it, or just use ctrl + shift + o.
    [​IMG]

    Lets continue onto the code. First of all we check what type of command the player typed.
    Code:
    if(cmd.getName().equals("test") {
    //code here, oh and yeah quick note, comments in code start with double slashes like this
    /** or like this **/
    }
    
    What this means if a player or console typed /test. We assigned the value cmd to Command in our method.
    And if we want to check if sender is an player or not. Its pretty simple, inside our command boolean we just do this
    Code:
            if(!(sender instanceof Player)) {
              
            } else {
              
            }  
    [/COLOR]
    
    No worries, i will explain what it does, as you can see in the line "if(!(sender instanceof Player))" there is a "!". That means its negative, so when player is not an player. instanceof means is equal to.
    Example:
    Code:
    if(!(sender instanceof Player)) {
              //this means if sender is not equal to a player
            }
    
    if(sender instanceof Player) {
              //this means if sender is equal to a player
            }
    
    So what we can do for example:
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label,
                String args[]) {
    if(cmd.getName().equals("test") {
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.BLUE
                        + "Hey, you are a console, i see it with my code");
            } else {
                sender.sendMessage(ChatColor.GREEN
                        + "Hey, you are a player, i see it with my code");
            }
    }
            return false;
        }
    
    Dont forget to import ChatColor ;)
    What we did now if easy, when someone types /test it checks if its a player or the console, and them send them a message. Coding isnt that hard, isnt it?

    Our final result of the command:
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label,
                String args[]) {
            if(cmd.equals("test")) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.BLUE
                        + "Hey, you are a console, i see it with my code");
            } else {
                sender.sendMessage(ChatColor.GREEN
                        + "Hey, you are a player, i see it with my code");
            }
            }
            return false;
        }
    
    And our whole class:
    Code:
    package eu.trinitydev.firstplugin;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.material.Command;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
    
        public void onEnable() {
            getLogger().info(
                    "---> My First plugin is now launced, partyparty yeah. <---");
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label,
                String args[]) {
            if(cmd.equals("test")) {
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.BLUE
                        + "Hey, you are a console, i see it with my code");
            } else {
                sender.sendMessage(ChatColor.GREEN
                        + "Hey, you are a player, i see it with my code");
            }
            }
            return false;
        }
    
    }
    
    Pfew, we are almost done with our plugin, what we need to do now is adding a plugin.yml file, in this file we can set the plugin name, version, author, commands, permissions etc.
    Right click on your plugin project. New > File. And name the file: plugin.yml
    [​IMG]
    Press finish. Now the file will be created and openend. In my case it opens wordpad because ive set wordpad for .yml files.
    Inside there type this:
    Code:
    name: PluginName
    version: 1.0
    author: YourName
    commands:
      test:
        description: What ever you want
    
    And again, you change it to what you want and need. Save the file and close editor (if you use it)

    Now right click on your project. And click export now look for Java, open it and click Jar file
    [​IMG]

    Click next, select your destination, and click finish.
    You are done now :) Great news right? You finished the huge tutorial, good job, im proud.
    Got any questions left, or you want to learn more, comment it :)
    Any feedback is appricated, and again sorry for my english

    Greetings!
    :: TrinityDEV - Thiemo


     
    Last edited by a moderator: Aug 25, 2015
    mateu9999, OrangeGE, Jezu and 4 others like this.
  2. SevereWarning

    SevereWarning Member

    Joined:
    Nov 14, 2013
    Messages:
    5,676
    Likes Received:
    3,670
    Trophy Points:
    113
    Gender:
    Male
    Occupation:
    not much
    Location:
    Somewhere
  3. johnyestaloca

    johnyestaloca Member

    Joined:
    Oct 12, 2014
    Messages:
    1,171
    Likes Received:
    2,118
    Trophy Points:
    113
    Gender:
    Male
    Location:
    The land of the holy icecreams
    You should apply for Junior Dev. :3
     
    Accountrowan01 likes this.
  4. Stefanvld

    Stefanvld Sr. Mod Staff Member Sr. Moderator

    Joined:
    May 24, 2014
    Messages:
    10,690
    Likes Received:
    4,981
    Trophy Points:
    113
    Gender:
    Male
    Occupation:
    i do stuff
    Location:
    Behind you.
    Ha! @Eclipse1314 he uses you
     
  5. Eclipse1314

    Eclipse1314 Mod Staff Member Moderator

    Joined:
    Sep 18, 2014
    Messages:
    814
    Likes Received:
    2,173
    Trophy Points:
    93
    Gender:
    Female
    Occupation:
    Queen of Sweden, Norway, Denmark and Iceland
    Location:
    Europe
    Home Page:
    Thanks Stefan xDD
     
    Accountrowan01 likes this.
  6. TrinityDEV

    TrinityDEV Member

    Joined:
    Aug 24, 2015
    Messages:
    126
    Likes Received:
    68
    Trophy Points:
    28
    Gender:
    Male
    Occupation:
    Student
    Ofcourse, @Eclipse1314 is the best coding platform, duh
     
    Accountrowan01 and Eclipse1314 like this.
  7. Eclipse1314

    Eclipse1314 Mod Staff Member Moderator

    Joined:
    Sep 18, 2014
    Messages:
    814
    Likes Received:
    2,173
    Trophy Points:
    93
    Gender:
    Female
    Occupation:
    Queen of Sweden, Norway, Denmark and Iceland
    Location:
    Europe
    Home Page:
    ;))
     
  8. TrinityDEV

    TrinityDEV Member

    Joined:
    Aug 24, 2015
    Messages:
    126
    Likes Received:
    68
    Trophy Points:
    28
    Gender:
    Male
    Occupation:
    Student
    Lets say you are mind blown because of all my free time of typing this? :3
     
  9. SevereWarning

    SevereWarning Member

    Joined:
    Nov 14, 2013
    Messages:
    5,676
    Likes Received:
    3,670
    Trophy Points:
    113
    Gender:
    Male
    Occupation:
    not much
    Location:
    Somewhere
    No, the amount of stuff I don't understand in this is too (pg) "fudging" (pg) high.
     
    Accountrowan01 and PartyBlaze like this.
  10. SamueLogic

    SamueLogic Member

    Joined:
    Nov 2, 2014
    Messages:
    1,337
    Likes Received:
    1,171
    Trophy Points:
    113
    Gender:
    Male
    Occupation:
    Student
    Location:
    Colombia
  11. GuaaarrD

    GuaaarrD Member

    Joined:
    Dec 14, 2014
    Messages:
    1,745
    Likes Received:
    2,875
    Trophy Points:
    113
    Gender:
    Male
    Occupation:
    Spotify
    Location:
    Europe, Hungary
    ^ :)
     
    Accountrowan01 and johnyestaloca like this.
  12. NanoNet

    NanoNet Member

    Joined:
    May 23, 2014
    Messages:
    1,044
    Likes Received:
    345
    Trophy Points:
    83
    Gender:
    Male
    Location:
    IKEA
    Very nice... I'll be sure to use this when I decide to learn the Bukkit API.
     
    Accountrowan01 likes this.
  13. TrinityDEV

    TrinityDEV Member

    Joined:
    Aug 24, 2015
    Messages:
    126
    Likes Received:
    68
    Trophy Points:
    28
    Gender:
    Male
    Occupation:
    Student
    Well, I did two days ago, but it was was too late :rolleyes:

    Thank you :)

    Hey
     
  14. Mr_Poisonous

    Mr_Poisonous Member

    Joined:
    May 18, 2015
    Messages:
    148
    Likes Received:
    25
    Trophy Points:
    28
    Gender:
    Male
    Occupation:
    Gaming, Animating and life
    Location:
    The Netherlands
    Home Page:
    @TrinityDEV
    Tip: make a video of this, some peaple work better with videos, me to.
    but still great job and THANK u for this tutorial!
     
  15. TrinityDEV

    TrinityDEV Member

    Joined:
    Aug 24, 2015
    Messages:
    126
    Likes Received:
    68
    Trophy Points:
    28
    Gender:
    Male
    Occupation:
    Student
    My pc is too laggy to record, so thats why I made this tutorial
     
  16. DanOF

    DanOF Member

    Joined:
    Aug 20, 2015
    Messages:
    335
    Likes Received:
    230
    Trophy Points:
    43
    Gender:
    Male
    Location:
    New Jersey, USA
    Home Page:
    Why aren't you using maven?.
     
  17. TrinityDEV

    TrinityDEV Member

    Joined:
    Aug 24, 2015
    Messages:
    126
    Likes Received:
    68
    Trophy Points:
    28
    Gender:
    Male
    Occupation:
    Student
    I dont think users who are new into this need maven
     
  18. TrinityDEV

    TrinityDEV Member

    Joined:
    Aug 24, 2015
    Messages:
    126
    Likes Received:
    68
    Trophy Points:
    28
    Gender:
    Male
    Occupation:
    Student
    I really need an edit button though, now I am double posing but, if anyone is interested in how to make plugins using maven, just tell it. Can make simple guide for that
     
  19. Mr_Poisonous

    Mr_Poisonous Member

    Joined:
    May 18, 2015
    Messages:
    148
    Likes Received:
    25
    Trophy Points:
    28
    Gender:
    Male
    Occupation:
    Gaming, Animating and life
    Location:
    The Netherlands
    Home Page:
    ohh i understand, but if you are so good at programing you coud sale some plugins to other servers at http://www.mc-market.org/
     
  20. Mr_Poisonous

    Mr_Poisonous Member

    Joined:
    May 18, 2015
    Messages:
    148
    Likes Received:
    25
    Trophy Points:
    28
    Gender:
    Male
    Occupation:
    Gaming, Animating and life
    Location:
    The Netherlands
    Home Page:
    and from the money you coud earn with that u coud buy a better PC