Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • How to create API interfaces?


    pootisspencer

    Recommended Posts

    Hello,

    I'm still kinda a beginner to Java. I wanted to know how exactly I could make a .jar API file with real class, method, field names, but the actual contents (body) of the code not visible when importing it into the project. To clarify what I'm talking about :- The same thing when importing the bot's client.jar file to your script project and programming using that API, - how do you do it?

    Link to comment
    Share on other sites

    So creating an API is actually pretty easy, I'll do a quick example below to hopefully show you the process.

    There are a few ways to can go about this, but personally, I like using interfaces for structure, then implementing them for different use-cases.

    So, for example, we have a library project, where we are making our API. Inside we want to create an interface.

    package com.project.api;
    
    public interface IExample {
    	// This is the method we will be implementing.
    	int getValue();
    }

    After creating our interface, let's make an impl package in the same location as the interface, and inside the new impl package, create an implementation of the Example class like so.

    package com.project.api.impl;
    
    import com.project.api.IExample;
    
    public class ExampleImpl implements IExample {
          @Override
          public int getValue() {
          	  return 1337; 
          }
    }

    After you have created your implementation, you can now package your project as a JAR file, and add it as a dependency in another project. To call this API from a new project, ensure you have added it as a dependency, then use it like so.

    package com.different.project;
    
    import com.project.api.IExample;
    import com.project.api.impl.ExampleImpl;
    
    public class TestAPI {
    
    	//Here we reference our interface, as our implementation is of the same type.
    	private IExample example;
    	
    	public TestAPI() {
    		//Here we set our reference to our Example implementation
    		this.example = new ExampleImpl();
    	}
    
    	@Override
    	pubic String toString() {
    		return String.format("Example implementation value: %d", this.example.getValue());
    	}
    }

    Now once we have referenced our new API, you can now access functions within the interface, and they will behave based on the implementation provided. So for example, we printed our toString in the TestAPI class above, we should have: "Example implementation value: 1337" print out, as our ExampleImpl class returns that value for the overridden function.

     

    I hope this helps answer a few questions you were having. Also, this code is untested and was written in the browser, so there could be syntax/logic errors, though the idea remains the same either way.

    Link to comment
    Share on other sites

    On 10/27/2018 at 1:11 AM, Notorious said:

    So creating an API is actually pretty easy, I'll do a quick example below to hopefully show you the process.

    There are a few ways to can go about this, but personally, I like using interfaces for structure, then implementing them for different use-cases.

    So, for example, we have a library project, where we are making our API. Inside we want to create an interface.

    
    package com.project.api;
    
    public interface IExample {
    	// This is the method we will be implementing.
    	int getValue();
    }

    After creating our interface, let's make an impl package in the same location as the interface, and inside the new impl package, create an implementation of the Example class like so.

    
    package com.project.api.impl;
    
    import com.project.api.IExample;
    
    public class ExampleImpl implements IExample {
          @Override
          public int getValue() {
          	  return 1337; 
          }
    }

    After you have created your implementation, you can now package your project as a JAR file, and add it as a dependency in another project. To call this API from a new project, ensure you have added it as a dependency, then use it like so.

    
    package com.different.project;
    
    import com.project.api.IExample;
    import com.project.api.impl.ExampleImpl;
    
    public class TestAPI {
    
    	//Here we reference our interface, as our implementation is of the same type.
    	private IExample example;
    	
    	public TestAPI() {
    		//Here we set our reference to our Example implementation
    		this.example = new ExampleImpl();
    	}
    
    	@Override
    	pubic String toString() {
    		return String.format("Example implementation value: %d", this.example.getValue());
    	}
    }

    Now once we have referenced our new API, you can now access functions within the interface, and they will behave based on the implementation provided. So for example, we printed our toString in the TestAPI class above, we should have: "Example implementation value: 1337" print out, as our ExampleImpl class returns that value for the overridden function.

     

    I hope this helps answer a few questions you were having. Also, this code is untested and was written in the browser, so there could be syntax/logic errors, though the idea remains the same either way.

     

    Whoa man who said you could have that S++ rank?! You need to pass the test first!

    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.