Automatic Food Banking



  • The number of scripts that are missing any basic food banking is frustrating.
    I wrote this class for everyone. No setup.
    When out of food it will

    • walk to the bank
    • withdraw the same food as what you start with
    • walk back to the same spot

    Hope it helps people advance their scripts.

    //Make this a property of your main class
    private TaskBank banker = new TaskBank();
    
    //If you dont use a task loop, call this at the beginning of each loop
    if (banker.validate())
        return banker.execute();
    
    // Put this in its own file called TaskBank
    import org.rspeer.runetek.adapter.component.Item;
    import org.rspeer.runetek.api.Game;
    import org.rspeer.runetek.api.commons.Time;
    import org.rspeer.runetek.api.commons.math.Random;
    import org.rspeer.runetek.api.component.Bank;
    import org.rspeer.runetek.api.component.tab.Inventory;
    import org.rspeer.runetek.api.movement.Movement;
    import org.rspeer.runetek.api.movement.path.Path;
    import org.rspeer.runetek.api.movement.position.Position;
    import org.rspeer.runetek.api.scene.Players;
    import org.rspeer.script.task.Task;
    import org.rspeer.ui.Log;
    import java.lang.reflect.Array;
    import java.util.Arrays;
    import java.util.HashMap;
    public class TaskBank extends Task {
    
        HashMap<String, Integer> startInventory = null;
    
        Position returnPos = null;
        Path returnPath = null;
        int startRun = Random.nextInt(40, 60);
    
        @Override
        public int execute() {
    
            if (Movement.getRunEnergy() > startRun && !Movement.isRunEnabled()) {
                Movement.toggleRun(true); //Turn on run
                startRun = Random.nextInt(36, 55); //Randomize when we next turn on run
            }
            if (returnPos == null)
                returnPos = Players.getLocal().getPosition();
            if (returnPath != null) {
                if(returnPos.distance(Players.getLocal().getPosition()) < 2)
                    returnPath = null;
                    returnPos = null;
                else {
                    returnPath = tryGetPath(returnPos, 15);//Must update continuously
                    returnPath.walk();
                }
            }else {
                if (!Bank.isOpen()) {
                    Bank.open();//This will activate the path walker
                    return Random.nextInt(1000, 2000);
                } else {
                    Log.fine("Banking");
                    //Bank.depositInventory(); //Optional. Maybe not?
                    Time.sleep(200, 400);
                    for (String key : startInventory.keySet()) {
                        Bank.withdraw(key, startInventory.get(key));
                        Time.sleep(200, 400);
                    }
                    returnPath = tryGetPath(returnPos, 15);
                    if (returnPath == null) {
                        while (Game.isLoggedIn()) {
                            Game.logout();
                            Time.sleep(300, 650);
                        }
                        //Set your script to stop here!
                        //Set your script to stop here!
                        //Set your script to stop here!
                        //mainScript.publicStaticInstance.setStopping(true);
                    }
                }
            }
            return Random.nextInt(200, 600);
        }
    
        @Override
        public boolean validate() {
            if (startInventory == null) {
                CollectFoodInInventory();
            }
            if (returnPath != null) return true;//We are walking, so yes, run this
            if (startInventory.size() == 0) return false; //We never found any food on start, so we'll never run
            Item[] remainingFood = Inventory.getItems((i) -> startInventory.containsKey(i.getName()));
            return (remainingFood.length == 0);//We have no matching food, lets bank!
        }
        
        void CollectFoodInInventory(){
            startInventory = new HashMap<>(0);//Hashmap is like a dictionary, you can only have one of something.
            for (Item item : Inventory.getItems()) {
                String name = item.getName();
                if (startInventory.containsKey(name))
                    startInventory.put(name, startInventory.get(name) + 1);
                else {
                    if (canWeEatThis(item)) {
                        startInventory.put(name, 1);
                    }
                }
            }
        }
        
        boolean canWeEatThis(Item item){
            return Arrays.stream(item.getActions()).anyMatch((s) -> s.startsWith("Eat"));
        }
    
        public static Path tryGetPath(Position destination, int tries) {
            Path p = Movement.buildPath(destination);
            int attempts = 0;
            while (p == null) {
                if (attempts > tries) {
                    break;
                }
                p = Movement.buildPath(destination);
                if (p != null) {
                    break;
                }
                attempts++;
                Time.sleep(500, 1000);
            }
            return p;
        }
    }

 

83
Online

8.6k
Users

904
Topics

11.7k
Posts