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
  • First Crack at Scripting - Chops and Banks Logs


    FooBar

    Recommended Posts

    Here's my first attempt at writing a useful script. It chops trees along the Al Kharid gate and banks them at Lumby banks. I really feel like the banking portion of this script could use some work. Any feedback is welcome. Feel free to use the script if you want it.

    package main;
    
    import org.dreambot.api.methods.container.impl.bank.BankLocation;
    import org.dreambot.api.methods.map.Area;
    import org.dreambot.api.script.AbstractScript;
    import org.dreambot.api.script.Category;
    import org.dreambot.api.script.ScriptManifest;
    import org.dreambot.api.wrappers.interactive.GameObject;
    
    @ScriptManifest( author = "Foo Man", name = "First Bot", version = 0, description = "Test", category = Category.WOODCUTTING )
    
    public class Main extends AbstractScript {
    
        private final Area treeArea = new Area( 3263,3215,3266,3237 );
    
        public enum Task {
            CHOP,
            BANK,
        }
    
        public Task currentTask;
    
        @Override
        public void onStart() {
            if ( getInventory().isFull() ) {
                currentTask = Task.BANK;
            } else {
                currentTask = Task.CHOP;
            }
        }
    
        @Override
        public int onLoop(){
            int returnValue = 300;
            sleep( 1820, 2862 );
            switch ( currentTask ) {
                case BANK:
                    if ( !getLocalPlayer().isAnimating() && !BankLocation.LUMBRIDGE.getArea( 2 ).contains( getLocalPlayer().getTile() ) ) {
                        getWalking().walk( BankLocation.LUMBRIDGE.getArea( 2 ).getRandomTile() );
                    } else {
                        getBank().openClosest();
                        getBank().depositAllItems();
                        getBank().close();
                        currentTask = Task.CHOP;
                    }
                    break;
                case CHOP:
                    if ( getInventory().isFull() ) {
                        currentTask = Task.BANK;
                        break;
                    }
    
                    if ( !getLocalPlayer().isAnimating() && !treeArea.contains( getLocalPlayer().getTile() ) ) {
                        getWalking().walk( treeArea.getRandomTile() );
                    } else if ( !getLocalPlayer().isAnimating() ){
                        GameObject tree = getGameObjects().closest("Tree" );
                        if ( tree != null ) {
                            tree.interact( "Chop down" );
                            sleep( 500, 1200 );
                        } }
                    break;
            }
            return returnValue;
        }
    
        @Override
        public void onExit(){
    
        }
    
    }

     

    EDIT

    I now feel like this may have been the wrong place to post this. Sorry if that's the case, still getting used to these forums!

    Link to comment
    Share on other sites

    Good job! I can give you some suggestions, take them and use them if you'd like:

    (Read the comments, easier that way)

    private static final int ANIMATION_CHOPPING = 0; // Not sure what the ID is, you should check it yourself and change
    
    	@Override
        public int onLoop(){
     	    //int returnValue = 300;
            // Don't do this, its totally worthless, doesnt do anything besides increase the delays for no reason. 
            // It won't help you with any bans if thats what you are worried about
            //sleep( 1820, 2862 );
      
      
      		// Seperate your current states instead of doing things procedurally and expecting things to follow through all the time (they might not and you can get stuck on something
            // For example, You can seperate the branch where the bank is open or not and act on it, same for inventory, etc
            switch (currentTask) {
                case BANK:
                	if(getBank().isOpen()){
                      	if(getInventory().isEmpty()){
                          	if(getBank().close()){
                        		currentTask = Task.CHOP;
                            }
                        }else{
                          if(getBank().depositAllItems()){
                          	SleepUntil(() -> getInventory().isEmpty(), 1500);
                          }
                        }
                    }else{
                      if(getInventory().isEmpty()){
                        currentTask = Task.CHOP;
                      }else{
                        if (!BankLocation.LUMBRIDGE.getArea(2).contains(getLocalPlayer())) {
    
                          // Try to get into the habit of testing if the operations were successfull
                          if(getWalking().walk(BankLocation.LUMBRIDGE.getArea(2).getRandomTile())){
                            // You can use sleepUntil to sleep or "wait" until a condition is met. 
                            // It isnt a must, but it helps prevent spam clicking and mistakes because of any sudden "lag" you might get
                            MethodProvider.sleepUntil(() -> getWalking().shouldWalk(), 4000);
                          }
                        }else{
                          if(getBank().open()){
                            MethodProvider.sleepUntil(() -> getBank().isOpen(), 3000);
                          }
                        }
                      }
                    }
                    break;
                case CHOP:
                	if(treeArea.contains(getLocalPlayer())){
                      if (getInventory().isFull() ) {
                        currentTask = Task.BANK;
                      }else{
                        if(getLocalPlayer().getAnimation() != CHOPPING_ANIMATION_ID){
                          GameObject tree = getGameObjects().closest("Tree");
                          if (tree != null && tree.interact("Chop down")) {
                            MethodProvider.sleepUntil(() -> getLocalPlayer().getAnimation() == CHOPPING_ANIMATION_ID, 3000);
                          }
                        }
                      }
                    }else{
                      if (getInventory().isFull() ) {
                        currentTask = Task.BANK;
                      }else{
                        if(getWalking().walk(treeArea.getRandomTile())){
                          MethodProvider.sleepUntil(() -> getWalking().shouldWalk(), 4000);
                        }
                      }
                    }
            }
      
            return 500;
        }

     

    Link to comment
    Share on other sites

    You have to consider edge cases and wait timings. There's a sleepUntil method that allows you to do just that, sleep until a particular action returns true.

    For example:

     

    sleepUntil(() -> getBank().isOpen(), 5000);

     

    Does what it says on the tin. It simply sleeps for 5 seconds, or until the bank is open (whichever comes first), before processing the next task.

     

    So to elaborate, if you were to do:

     

    if (getBank().open()) {
    	MethodProvider.sleepUntil(() -> getBank().isOpen(), 5000);
    	}

     

    The above will try to open the bank, then refrain from continuing until either the bank has indeed opened, or 5 seconds have elapsed. Dynamic sleeping is an integral part in smoothness of script execution. Keep at it!

    Link to comment
    Share on other sites

    Thanks for the feedback everyone! I wasn't expecting so many friendly and useful replies so quickly. A great first impression of the community ^_^

    I am aware of the node concept, and I have tried it out. But I want to find more examples of how others are using them in more complex tasks before I start using them the wrong way. I'll be browsing the forums more and checking out some of what's been posted already.

    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.