API Implementation

Discussion in 'Plugin Development' started by silthus, Jun 12, 2011.

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

    silthus

    Hey,

    I am having some problems with integrating an API into my plugin. I want to let the API access only a part of my RCPlayer class and give that to the public through an Interface, but I dont know how -.-".

    Here is my interface class which I want to extend at any given point:

    Code:
    public interface RCSHandler {
        
        public RCPlayer getPlayer(Player player);
        
        public int getExp();
        public int getSkillpoints();
        public int getLevel();
        public int addExp(int amount);
        public int substractExp(int amount);
        public int addSkillpoints(int amount);
        public int substractSkillpoints(int amount);
        
    }
    Code:
    public class RCSkillsHandler implements RCSHandler {
    
        private RCPlayer p;
        
        public RCPlayer getPlayer(Player player) {
            p = new RCPlayer(player);
            return p; // this object should be returned but rather be used as a basis for the following methods
        }
    
        public int getExp() {
            
            return p.getExp();
        }
    
        public int getSkillpoints() {
            
            return p.getSkillPoints();
        }
    
    .....
    .....
    .....
    I want the methods which come after the getPlayer(Player player) method to depend on getPlayer and only be accessible like tha:
    Code:
    RCSkillsHandler handler = new RCSkillsHandler();
    handler.getPlayer(player).getExp();
    
    The thing is that I only want certain Methods to be accessible of the RCPlayer class.

    Thanks a lot,

    Silthus
    Source: https://github.com/Silthus/RCSkills
     
  2. Offline

    Adamki11s

    I think you can make certain methods 'protected' by using the protected keyword so those methods can only be called from within the class/package itself.
     
  3. Offline

    Bone008

    There are actually 4 different visibilities in Java:

    public: visible for everyone
    default: only visible for classes in the same package
    protected: only visible for classes that extend the class
    private: only visible for the class itself

    The one you would be aiming at is the default one. It can be done by simply not specifying any visibility.

    Code:
    // accessible for everyone
    public void doSomething(){
    
    }
    
    // only visible for classes in the same package
    public void doSomething(){
    
    }
     
  4. Offline

    silthus

    I know that I could make some methods protected, but that doesn't really help me since I need to use those methods in diffrent packages in the same plugin and if I make them protected I wont have access to them.

    Isn't there an other way to make only certain methods available in an API without having to reorder whole packages?
     
  5. Offline

    Adamki11s

    Maybe if you make every method private and then just have a public function which call certain private functions?
     
Thread Status:
Not open for further replies.

Share This Page