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
  • Extending MethodContext To Create Your Own API's


    Explicit

    Recommended Posts

    This is useful if you want to add your own API's that DreamBot does not include by default or even override their standard ones. The first thing to do is create a generic class which all custom API methods can extend from:

    import org.dreambot.api.methods.MethodContext;
    
    /**
     * Created with IntelliJ IDEA
     * User: Anthony
     * Date: 1/6/15
     */
    
    public abstract class API<C extends MethodContext> {
    
        protected C ctx;
    
        public API(C ctx) {
            this.ctx = ctx;
        }
    
    }
    

    Our use of generics in the class declaration will allow us to reuse this class for any custom context implementation. Next we'll actually create our first custom API class:

    import org.dreambot.api.wrappers.interactive.Entity;
    
    /**
     * Created with IntelliJ IDEA
     * User: Anthony
     * Date: 1/6/15
     */
    
    public class Methods extends API<RS07Context> {
    
        public Methods(RS07Context ctx) {
            super(ctx);
        }
    
        public boolean interact(Entity entity, String action, boolean rightClick) {
            // example method
    
            return false;
        }
    
    }
    

    Our ctx variable will be available to this class since it's stored within the super class. Now to extend MethodContext:

    import org.dreambot.api.methods.MethodContext;
    
    /**
     * Created with IntelliJ IDEA
     * User: Anthony
     * Date: 1/6/15
     */
    
    public class RS07Context extends MethodContext {
    
        private final Methods methods;
    
        public RS07Context() {
            this.methods = new Methods(this);
        }
    
        public Methods getMethods() {
            return methods;
        }
    
    }
    

    RS07Context will be able to access all of the default API methods (#getBank, #getCombat, etc.) and also our custom #getMethods method. Set it up at script start within your main class and you're good to go:

    import org.dreambot.api.script.impl.TaskScript;
    
    /**
     * Created with IntelliJ IDEA
     * User: Anthony
     * Date: 1/6/15
     */
    
    public class Main extends TaskScript {
    
        private RS07Context ctx;
    
        @Override
        public void onStart() {
            ctx = new RS07Context();
    
            getClient().getInstance().setMethodContext(null);
            ctx.registerContext(getClient());
        }
    
        @Override
        public int onLoop() {
            ctx.getMethods().....
    
            return 100;
        }
    
    }
    
    Link to comment
    Share on other sites

    • 2 months later...
    • 3 months later...

    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.