Using customevents in API

Discussion in 'Plugin Development' started by JeroenV, Jan 2, 2013.

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

    JeroenV

    Hey,

    I'm currently working on an API (for the first time), I want to work with custom events but I'm unsure if I were to call the event in one plugin that I could listen to it in another plugin through the API.

    So let's say there's a customevent named 'FullReset' in the API, which is called when one of the plugins issues a FullReset. Could another plugin that's hooked (depend) into this API listen to it? From my normal logic point of view it would be possible, but my own logic has been proven wrong many times :)

    I also have a second questions, I've been working on a 'definelist' method within the API that would load all the lists from the config files and put them in their ArrayLists variables. However if I use APICLASSNAME.LISTNAME after having used the definelist method it will still return null. I was hoping that I could get all the data from the config files using the API and then just use the API as some sort of easy-acces database where I can easily remove and add values to lists and have that take effect in every plugin hooked into it. Is this actually possible?


    Thanks in advance :),
    Jeroen V.
     
  2. Offline

    fireblast709

    Event question: yes. If you create an event and fire it, others would be able to listen to it like you do with all the other events
    API question: yes it is possible. Just note that creating a non-static API and retrieving that via the main class is a good method to keep things correctly managable with the main plugin. You could just get the API via Bukkit.getPluginManager().getPlugin("name").getAPI() (or something like it)
     
  3. Offline

    JeroenV

    fireblast709
    Ah great :) I was hoping it would work, however I still have some issues with the second question. I've tried doing this before and as my first comment stated, it still returned null. Let me give you some more info about the context of the error:

    The APIClass with the defineLists() method:
    Code:
    public class CWAPI_Main extends JavaPlugin{
     
        public CWAPI_Main(){   
        }
       
        public void defineLists(){
    //List adding here
        }
    

    An example for a class from a plugin using the API:
    Code:
        CWAPI_Main CWAPIM = new CWAPI_Main();
     
    onEnable(
        Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {      
              @Override
              public void run(){
     
    CWAPIM.defineList();
    Bukkit.BroadCastMessage(CWAPIM.Listname);
    
              }    }, 2*20L);
    )
    
    Now here it will just broadcast the message "[]" is there something different I need to do? I know it reached the defineList method by using a BroadCastMessage in there too. However whenever I try and retrieve the lists it'll give me an empty list.
     
  4. Offline

    fireblast709

    Just a raw example of what I mean.
    The main class of the plugin (for example an equivalence of your plugin):
    Code:java
    1. public class PluginHasAPI extends JavaPlugin
    2. {
    3.  
    4. private API api;
    5.  
    6. public void onEnable()
    7. {
    8. api = new API(this);
    9. api.defineList();
    10. }
    11.  
    12. public API getAPI()
    13. {
    14. return this.api;
    15. }
    16. }

    The API class
    Code:java
    1. public class API
    2. {
    3. private PluginHasAPI main;
    4. public API(PluginHasAPI main)
    5. {
    6. this.main = main;
    7. }
    8.  
    9. private ArrayList<String> names = new ArrayList<String>();
    10.  
    11. public void defineList()
    12. {
    13. names.add("Fireblast709");
    14. }
    15.  
    16. public List<String> getList()
    17. {
    18. return this.names;
    19. }
    20. }

    The other plugin using your API
    Code:java
    1. public class usingAPI extends JavaPlugin
    2. {
    3. public API api;
    4.  
    5. public onEnable()
    6. {
    7. Plugin pha = Bukkit.getPluginManager().getPlugin("PluginHasAPI");
    8. if(pha != null)
    9. {
    10. // Get the API reference
    11. this.api = ((PluginHasAPI)pha).getAPI();
    12. }
    13. else
    14. {
    15. Bukkit.getPluginManager().disablePlugin(this);
    16. return;
    17. }
    18. List<String> names = api.getList();
    19. System.out.println(names.toArray(new String[0]));
    20. // This would print out something like '{"Fireblast709"}'
    21. }
    22. }

    This way it is easier for the other plugin to communicate with yours, as you can handle all the internal changes inside the API, and the other plugin just has to call the correct methods
     
  5. Offline

    JeroenV

    @fireblast709 Thanks you so much for this :)

    Would this work both ways, so that every plugin using the API can retract/add values to lists/global variables or is it just the main plugin that changes things in the api?
     
  6. Offline

    fireblast709

    You can use the API as you wish. The API is ment as a communication between the plugin and other plugins
     
Thread Status:
Not open for further replies.

Share This Page