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
  • Try asking for help in the chatbox
  • Java Class Method Context


    liondedan
     Share

    Recommended Posts

    Hello, I'm a web developer, played RuneScape for years, and fancy having a go at making some dreambot scripts. The community here seems great and there are some very cool projects ongoing. I'm running into what is probably a very beginner Java problem, as I haven't actually read any Java docs to get here (reply replying on the small amoutn of similarities between Java and other web languages) - so feel free to shout at me. However, if I'm creating a new class, is it possibly to use dreambot methods from within the new class. Example:

     

    // Main.java
    public class Main extends AbstractScript {
        @Override
        public int onLoop() {
    
            Area varrockArea = new Area (3187, 3235,3196, 3228);
            TreeLocation varrock = new TreeLocation(varrockArea);
            varrock.getToLocation();
    	}
    }
    
    
    // TreeLocation.java
    public class TreeLocation {
        Object location;
    
        // This is the constructor of the class Employee
        public TreeLocation(Object location) {
            this.location = location;
        }
    
        public void getToLocation() {
            getWalking().walk(location.getRandomTile());
        }
    }

     

    Thanks and Hello :)

    Dan

    Link to comment
    Share on other sites

    You'll notice that you can only execute methods like that via the MethodContext class, which is a parent of AbstractScript. If you want to do something like this, you'll need to pass the method context around that is instantiated internally when the AbstractScript class is loaded by the class loader. Your constructor would need to look something like

    public TreeLocation(Area loc, MethodContext mc) {
      this.loc = loc;
      this.mc = mc;
    }
    
    public void walk(Area loc) {
      if (mc.getWalking().walk(/* a location that should be an Area#getRandomTile() or Tile */)) {
        // Sleep and stuff
      }
    }

     

    Link to comment
    Share on other sites

    Hey sorry - next question @Xephy . If I'm instantiating the TreeLocation class from the main class - how do I go about passing the MethodContext through?

     

    import org.dreambot.api.methods.MethodContext;
    
    TreeLocation varrock = new TreeLocation(MethodContext.this);

     

    Edited by liondedan
    Link to comment
    Share on other sites

    15 minutes ago, liondedan said:

    Hey sorry - next question. If I'm instantiating the TreeLocation class from the main class - how do I go about passing the MethodContext through?

     

    import org.dreambot.api.methods.MethodContext;
    
    TreeLocation varrock = new TreeLocation(MethodContext.this);

     

    getClient().getMethodContext();

     

    Link to comment
    Share on other sites

    23 minutes ago, Nuclear Nezz said:

    AbstractScript is also a method context in itself.

    So you could just do

    new TreeLocation(this); from the main script class.

    ^ this

     

    Remember that MethodContext is the parent class of AbstractScript, so you get access to those methods by passing in the AbstractScript.

    I would recommend not storing your location as an Object as well. I would recommend storing it as an Area; this is good design and practice both for clarity and because you won't have to cast it later. It's also good practice to store your variables explicitly as either 'private' or 'public'. Private variables can't be viewed or accessed from outside of the file, and public variables are viewable, accessible, and changeable from outside of the file. 

    Lastly, if a variable should not (or will not) change value in the future, another useful marker is 'final', which means the value will never be changed after when it's first assigned (which you're doing during instantiation of the object). It should look something like this:

    // TreeLocation.java
    public class TreeLocation {
        private Area location;
        private final MethodContext m;
    
        // This is the constructor of the class Employee
        public TreeLocation(MethodContext m, Area location) {
            this.m = m;
            this.location = location;
        }
    
        public void getToLocation() {
            m.getWalking().walk(location.getRandomTile());
        }
    }
    // Main.java
    public class Main extends AbstractScript {
        @Override
        public int onLoop() {
            Area varrockArea = new Area (3187, 3235,3196, 3228);
            TreeLocation varrock = new TreeLocation(this, varrockArea);
            varrock.getToLocation();
        }
    }
    

    Happy scripting :) 

    Edited by Im A Baller
    Link to comment
    Share on other sites

    On 12/10/2018 at 10:05 PM, Im A Baller said:

    ^ this

     

    Remember that MethodContext is the parent class of AbstractScript, so you get access to those methods by passing in the AbstractScript.

    I would recommend not storing your location as an Object as well. I would recommend storing it as an Area; this is good design and practice both for clarity and because you won't have to cast it later. It's also good practice to store your variables explicitly as either 'private' or 'public'. Private variables can't be viewed or accessed from outside of the file, and public variables are viewable, accessible, and changeable from outside of the file. 

    Lastly, if a variable should not (or will not) change value in the future, another useful marker is 'final', which means the value will never be changed after when it's first assigned (which you're doing during instantiation of the object). It should look something like this:

    // TreeLocation.java
    public class TreeLocation {
        private Area location;
        private final MethodContext m;
    
        // This is the constructor of the class Employee
        public TreeLocation(MethodContext m, Area location) {
            this.m = m;
            this.location = location;
        }
    
        public void getToLocation() {
            m.getWalking().walk(location.getRandomTile());
        }
    }
    // Main.java
    public class Main extends AbstractScript {
        @Override
        public int onLoop() {
            Area varrockArea = new Area (3187, 3235,3196, 3228);
            TreeLocation varrock = new TreeLocation(this, varrockArea);
            varrock.getToLocation();
        }
    }
    

    Happy scripting :) 

    Thank you for this ^^

    Link to comment
    Share on other sites

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now
     Share

    ×
    ×
    • 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.