API Creation

Discussion in 'Plugin Development' started by NoLiver92, Dec 1, 2013.

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

    NoLiver92

    I am making an API for my plugin BUT here is my problem:
    I have variables which are in my main class file which i want the user to reach through the api, how do i get them into the api WITHOUT having to pass an instance of the plugin into a constructor?

    The reason for this is that when the second plugin trys to access the api I dont want them to use a constructor where they have to input a plugin instance. How do I do this?
     
  2. Offline

    BungeeTheCookie

    Make it static
     
  3. Offline

    ZeusAllMighty11

  4. Offline

    fireblast709

    In the case the API is in a different class, you can pass an instance of your main class via a method, for instance
    Code:
    public static void init(Main main)
    {
        getInstance().main = main;
    }
    (Or use a non-static version which you call via getInstance())

    On a sidenote: don't forget to set the singleton instance to null onDisable() to prevent a possible memory leak.
     
  5. Offline

    NoLiver92

    I dont think you understand my question!!!!
    fireblast709 this is what i dont want:
    Code:java
    1. public static void init(Main main)
    2. {
    3. getInstance().main = main;
    4. }


    i need to access the main WITHOUT having to pass the main plugin class into the constructor. this is because when another plugin wants to use the api, they shouldnt have to put in an instance of the main class file to get it to work.
     
  6. Offline

    MastaC

    In your constructor you could have

    Code:java
    1. YourPlugin plugin = (YourPlugin)Bukkit.getPluginManager().getPlugin("YourPlugin");


    right? That wouldn't require you to pass an instance along with the constructor. It would just look it up by name.

    or make a static method in your main class that returns itself. Have the constructor call that method to get the instance. I've heard that's bad joojoo though. Would anyone like to elaborate?
     
  7. Offline

    NoLiver92

    I looked at this but you need to have the instance of the main to get the instance if that makes any sense. aka not possible this way.

    I like the look of getting the plugin by name and that just might work

    MastaC that method wont work as i cant get the variables from inside the class file.

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

    xize

    NoLiver92
    hmm you could take a example of my ManCo api still very buggy but it works.

    https://github.com/xize/ManCo/blob/HEAD/src/tv/mineinthebox/ManCo/ManCo.java
    https://github.com/xize/ManCo/blob/HEAD/src/tv/mineinthebox/ManCo/api/api.java

    ive tried this with static but that won't make sense if you want it to instance it with 'new' like alot of plugins do hooking wise.

    edit
    though I need to say that this isn't the best practice as static could deliver synchroom errors by to many calls in one time.
     
  9. Offline

    1Rogue


    No, it works perfectly fine. You just need to cast it to your class type appropriately, unless you don't have public fields/accessors for the variables you are modifying.
     
  10. Offline

    fireblast709

    You would call init from, for instance, your onEnable(), and only once. Any other plugin can just use the API with just the use of static methods in the API.
     
  11. Offline

    NoLiver92

    could someone give me an example of this? im getting slightly confused on what i need to do. I have the main class file which uses example1 class and passes an instance of the main class into the constructor.

    then using example2 class i need to access the functions in example1 WITHOUT having to pass the instance into the constructor.

    an example would be good...
     
  12. Offline

    HyrulesLegend

    NoLiver92 We are not here to feed you code.
     
  13. Offline

    NoLiver92

    Max_The_Link_Fan I am here for help. im not expecting to get my code written for me, just an understanding of how the solution works!!!
     
    Darq likes this.
  14. Offline

    Darq

    NoLiver92 this is how I'd do it:
    Declare a private static INSTANCE variable inside of your main class, eg "private static Plugin INSTANCE;", then initialize it with your plugin's instance in onEnable(), eg "INSTANCE = this;". Then, make a public static method to return INSTANCE. You can then simply call that when you need an instance of your main class from anywhere in your API.
     
  15. Offline

    NoLiver92

    Oh ok.. so if i use 'static' variables and functions, i dont need to initialize the calling class, i just use those functions. ok i think i understand it now, i will go and have a go at it. cheers
     
  16. Offline

    Darq

    NoLiver92 something like that.. the 'static' modifier is very important to object oriented programming. It allows a variable to share the same value across all instances of the class it's declared in (the value is also only stored in one place). What @ZuelAllMight11 mentioned , Singleton is a pattern in which a class can only be initialized once, and that single instance is typically stored in a private static variable, private so some class can't just come along and change its value, and static so it can be accessed by a public static method declared in the same class. Edit: format
     
Thread Status:
Not open for further replies.

Share This Page