I need help writing a API

Discussion in 'Plugin Development' started by blackwolf12333, Jun 1, 2012.

Thread Status:
Not open for further replies.
  1. Hello guys,
    What i am trying to achieve is to create an api for my plugin, i have been playing around with interfaces and stuff, but i don't get it working because i have no idea how to do something like this:(
    I hope you guys can help, greetz blackwolf12333
     
  2. Offline

    cdncampbell

    A couple of questions;

    - What does your plugin do?
    - Which bukkit interfaces is the plugin trying to implement, or are the interfaces for another library?
    - Are you trying to create an interface for your classes?
     
  3. My plugin is a simple anti grief plugin which logs a few events, you can read all that on the bukkitdev page(see my signature)
    I want to create an API of my own plugin, so i think i should want to create interfaces of my own classes, the ones that search through the file everything gets logged to.
    greetz blackwolf12333
     
  4. Offline

    cdncampbell

    OKies then,

    Well in Java an interface is a group of methods with empty bodies. It's really a contract between the class implementing the interface and the behaviour it will provide. An example of this could be:

    Code:
    public interface LogSearch {
        //any final or static fields can be here
        //any abstract method declarations as well.. see below:
         void caseInsensitive(String newLogRow);
         void caseSensitive(String newLogRow);
         void rowHighlight (String newLogRow);
     
    }
     
    public class MyLogSearch implements LogSearch {
        public void caseInsensitive(String row){
              //code here ... 
        }
        public void caseSensitive(String row){
              //code here ... 
        }
        public void rowHighlight(String row){
              //code here ... 
        }
    }
    
    In a nut shell, you can have an interface which denotes how the class implementing it should behave, if you try to implement LogSearch with out the declared methods of the Interface you will get a fatal error.

    Clear as mud? lol
     
  5. I came so far already:p but what i don't get is how to use this in another plugin, because when i try this i have to reimplement the methods, so the behavior of the class changes...and that's just what i don't want:p
    greetz blackwolf12333
     
  6. Offline

    Malikk

    I've got a lot of experience with interfaces, and once you know how to use them, they are very useful and look very clean.

    First, make the interface, it should look like this. All it needs is method names and the parameters they take. You can add long javadocs that explain everything here as well. Also, you don't need an constructor, since we'll be registering this a different way.

    Code:
    public interface MyInterface {
    
        /*
         * The Main API
         */
        
        /**
         * @return a list of all found regions
         */
        public ArrayList<String> getRegions();
        
        /**
         * Gets the regions that the Entity is in
         * 
         * @param entity
         * @return region name, or null
         */
        public ArrayList<String> getRegions(Entity entity);
     
    }
    
    You don't actually have any code in here, just method names.

    The next step is making another class which will implement your interface.

    Code:
    public class MyAPIManager implements MyInterface{
        
        Plugin plugin;
        
        public MyAPIManager(Plugin instance){
            plugin = instance;
        }
        
        /*
         * Plugin Methods
         */
        
        @Override
        public ArrayList<String> getRegions(){
            //do stuff
        }
        
        @Override
        public ArrayList<String> getRegions(Entity entity) {
           //do stuff
        }
    }
    
    Now we've got to register our interface. This basically registers the relationship between our interface and the class that implements it. You'll want to add this to your onEnable()

    Code:
    MyInterface api = new MyAPIManager(this);
    Bukkit.getServicesManager().register(MyInterface.class, api, this, ServicePriority.Normal);
    
    When someone wants to use your api, they'll have to get your interface object like this.

    Code:
    RegisteredServiceProvider<MyInterface> provider = getServer().getServicesManager().getRegistration(MyInterface.class);
            MyInterface api = provider.getProvider();
    
    Now, to use any of the methods in you api, they can use that variable, like this.

    Code:
    ArrayList<String> regions = api.getRegions();
    
     
  7. I think this is the information i was looking for, but i'll try this out tomorrow, but thanks:)
     
Thread Status:
Not open for further replies.

Share This Page