[Tutorial (EASY)] Creating an API for your plugin!

Discussion in 'Resources' started by ColaCraft, Jan 27, 2014.

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


    So, let's say you have this huge plugin you're working on, and you want to keep it organized, or even just create your own methods so you don't have to rewrite the same thing over and over again- whatever the case, this will help!

    Start by making your API class- name it whatever you want. You can even have multiple of these per plugin!

    1. public class MyAPI{ //Starts with capital letter
    3. }


    Here are the terms you need to know before doing this...

    Here is a neat chart to start you out, followed by a brief definition of each modifier.


    public -
    adding this to your method means it can be accessed anywhere

    private -
    this will make your method only accessible via objects of the same class.

    protected -
    protected methods or Objects can only be accessed by classes in the same package, or sub classes.


    static -
    static means that you don't need to call the class to access the method

    1. public class MyAPI{
    3. public static doStuff(){
    5. doStuffHere();
    7. }
    9. }

    Instead of calling the method like this:

    1. MyAPI api = new MyAPI();
    3. api.doStuff();

    you can just do it like this:

    1. myAPI.doStuff();

    final -
    final stops the variable or method from being assigned to something else

    Boolean -
    Boolean is just a true or false statement.

    String -
    A string is any set of characters.

    int -
    int is any non-decimal number

    Double -
    Double is a number that always has a decimal

    void -
    void means that it doesn't return anything.

    (any other type) -
    It will return the type, say you use FILE it will always return a FILE.

    Example usage:

    1. public class MyAPI{
    3. <public/private> <static/final/none> <return type> <name>(<Args> <arguments>){
    5. doStuff();
    7. return <return type>;
    8. }
    10. }

    Now, this may be confusing, so let me try to break it down for you.

    Say you want a method that can be accessed by other classes, that is static, and represents a boolean to see if the player is online or not, we can do this like this:

    1. import org.Bukkit.Player;
    3. public class MyAPI{
    5. public static Boolean isPlayerOnline(Player p){ //We put Player p so that way we can
    6. //access their player of choice later
    8. if(p.isOnline()){
    9. return true; //Say true if they are on
    10. }else{
    11. return false; //Say false if they are offline
    12. }
    13. }
    15. }

    Then we can access this later like this

    3. Player PorterK = (Player) <get a player>;
    5. if(MyAPI.isPlayerOnline(PorterK){ //If PorterK is online (we put PorterK in the
    6. //parenthesis so that PorterK is the user we use
    7. //method.
    8. doStuff();
    10. }else{ //If player is not online do stuff
    11. doOtherStuff();
    12. }

    You just need to make sure you add in a return statement if it is not void.

    If the method is just void it will do whatever you want when you call upon it.

    If you call a method that is void, it will just do whatever is under the brackets and move on.

    I hope this helps! I've been looking for a tutorial on this and I couldn't find it, so here it is.

    Extra stuff added by xTrollxDudex

    Leave feedback/corrections below please, thanks!
  2. Thank you for making this tutorial! Same here, i could never find a tutorial for this when i needed it.
  3. Offline


    Great, now we learn some Java along with this as well!

    Private: Allows you to use reflection to get to it

    Static: Prevents a new instance of the class from being instantiated in order to access it

    Boolean != boolean. boolean is the primitive, Boolean is a wrapper.

    Class != Field. Class names start with a capital letter, plus your PorterK field isn't named properly.

    You should have an assessor in the main class so you can use getPlugin and get the API class. static should be avoided.

    Why so much Java background? Most of this stuff should be learned before even starting the Bukkit API.
    Jake6177, Chinwe, Garris0n and 3 others like this.
  4. Offline


    The terms thing is kind of funny. If you're decent Java developer, you should know these things.
  5. Offline


    If you're not a decent java developer, then you should have the terms.... so that you can become a decent java developer.
  6. Offline


    If you're not a decent Java developer, then you shouldn't be here.
    xTigerRebornx and jimuskin like this.
  7. Offline


    sigh :rolleyes:
  8. Offline


    Don't roll your eyes, because it's true. You shouldn't be learning an API in conjunction to learning Java. Learn Java first.

    I think my 1000th time of saying the same thing over and over celebration is coming up soon.
  9. Offline


    Well here is 1001 for you :rolleyes:

    But... you are right, the terms do seem out of place when compared to the scope of the post;)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 6, 2016
  10. Offline


    Just going to point out that by Java conventions, it should be "MyApi" rather than "myApi".
    Xp10d3, number1_Master and ColaCraft like this.
  11. In the isPlayerOnline() method, you can just return player.isOnline().
    public static boolean isPlayerOnline(Player player) {
        return player.isOnline();
  12. Offline


    For the love of god people, classes should start with a capital letter, instances without.
    1. MyAPI api = new MyAPI();
    2. api.doPublicMethod();
    3. MyAPI.doStaticMethod();
  13. Offline


    That makes the entire method useless. You could prepend a "player != null &&" to that, though, and then it would have some use.
  14. Garris0n
    True, I was just showing an easier way of doing what OP originally did.

    Edit: Also, this part is completely useless in the else statement
  15. Offline


    ColaCraft You should explain the protected keyword as well.

    It's pretty useful if you want your own plugin to call a method, but not outside plugins.
    xTrollxDudex likes this.
  16. Offline


    Derp, my bad

    @ everyone who is talking about the Java background.

    The whole reason I even began to learn Java was because of the Bukkit API, and there are soo many people who learn Java and the Bukkit API at the same time and don't know all these things. Tell them they should actually learn java first all you want, but it wont stop some kid who wants to make cool things for his friends from going to youtube and following a tutorial just to find himself at a point of "I don't know what to do next". I believe this is necessary, for their sake.


    Thanks, will throw into the OP.

    Also, I'll change myAPI to MyAPI. :oops:



    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 6, 2016
    Xp10d3 likes this.
  17. Offline


    It might be easier just to embed this image:
  18. Offline



    Right- it's in the OP
    tyzoid likes this.
  19. Offline


Thread Status:
Not open for further replies.

Share This Page