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 Script - Java Patterns


    liondedan

    Recommended Posts

    Hello,

    First script - just spaghetti code at the moment.

    Wanted to learn some Java and get to know the dreambot api. Hoping to work on this script and simply allow it to do a farming tree run. 

    Just a question on how to refactor the following into something a lot better - I'm from a web dev background so unaware of the Java patterns.

    If I were to initialize each location object - would I then be able to loop over each object to process each location in turn?

    I'm also struggling with the best way to handle 'Area' - at the very basic I'd like to perhaps do a switch statement against the 'name' and return an area but struggling to pass the area values around.

    Thanks

    Dan

     

    import org.dreambot.api.script.AbstractScript;
    import org.dreambot.api.script.Category;
    import org.dreambot.api.script.ScriptManifest;
    
    import TreePatch.TreePatch;
    
    @ScriptManifest(
            author = "LiDan",
            description = "Does stuff",
            category = Category.Farming,
            version = 0.01,
            name = "WIP: Does a tree run"
    )
    
    public class Main extends AbstractScript {
    
        // trees
        // willow: 5371
        String tree = "Willow Tree";
    
        //varrock
        TreePatch location = new TreePatch(this, tree, "Treznor", "Varrock");
    
        //taverly
    //    TreePatch location = new TreePatch(this, tree,"Alain", "Taverly");
    
        //gnome stronghold
    //    TreePatch location = new TreePatch(this, tree,"Prissy Scilla", "Stronghold");
    
        //lumbridge
    //    TreePatch location = new TreePatch(this, tree,"Fayeth", "Lumbridge");
    
        //faladar
    //    TreePatch location = new TreePatch(this, tree,"Heskel", "Falador");
    
        //Gnome Stronghold Fruit
    //    TreePatch location = new TreePatch(this, tree,"Bolongo", "Gnome Stronghold Fruit");
    
        @Override
        public int onLoop() {
    
            location.getToLocation();
    
            if (location.checkStatus()) {
                log("P: Location is complete: need to change to next");
            } else {
                log("P: Location is not yet complete");
                location.treeInspect();
    
                if (location.isInLocation() == false) {
                    log("P: Need to walk to location");
                    location.getToLocation();
                } else {
                    log("P: At the location already");
                    if (location.hasBeenHealthChecked() == false) {
                        log("P: Need to do a health check");
                        location.checkHealth();
                    } else if (location.hasBeenChopped() == false) {
                        log("P: Need to chop trees");
                        location.chopTree();
                    } else if(location.isTreePlanted() == false) {
                        log("P: Need to do a plant trees");
                        location.plantTree();
                    } else if (location.isFarmerPaid() == false) {
                        log("P: Need to pay farmer");
                        location.payFarmer();
                    }
                }
            }
            return 100;
        }
    }
    // TreePatch.java
    package TreePatch;
    
    import org.dreambot.api.methods.MethodContext;
    import org.dreambot.api.methods.map.Area;
    import org.dreambot.api.wrappers.interactive.GameObject;
    import org.dreambot.api.wrappers.interactive.NPC;
    
    
    
    public class TreePatch {
        private static MethodContext mc;
        String tree;
        String farmer;
        String city;
    
        Boolean atLocation = false;
        Boolean healthChecked = false;
        Boolean isChopped = false;
        Boolean treePlanted = false;
        Boolean farmerPaid = false;
        Boolean isComplete = false;
    
        // This is the constructor of the class Employee
        public TreePatch(MethodContext mc, String tree, String farmer, String city) {
            this.mc = mc;
            this.tree = tree;
            this.farmer = farmer;
            this.city = city;
        }
    
    //    private void getArea() {
    //        Area location;
    //        if (city == "Stronghold") {
    //            location = new Area (2433, 3418,2438, 3411);
    //        }
    //
    //        return location;
    //    }
    
        public void getToLocation() {
            // varrock
    //        Area location = new Area (3226, 3461,3231, 3456);
    
            // taverly
    //        Area location = new Area (2933, 3441,2939, 3435);
    
            // stronghold
            Area location = new Area (2433, 3418,2438, 3411);
    
            // gnome stronghold fruit
    //        Area location = new Area (2473, 3448,2478, 3444);
    
            //lumbridge
    //        Area location = new Area (3187, 3235,3196, 3228);
    
            //falador
    //        Area location = new Area (3001, 3376,3007, 3370);
    
            if (location.contains(mc.getLocalPlayer())) {
                mc.log("Already at " + city + " " + tree + " location");
                atLocation = true;
    
            } else {
                mc.log("Walking to " + city + " " + tree + " location");
                mc.getWalking().walk(location.getRandomTile());
            }
    
        }
    
        public void checkHealth() {
            if (!healthChecked == true) {
                mc.log(city + " " + tree + ": Needs Health Check");
                GameObject treePlot = mc.getGameObjects().closest(tree);
    
                // If there is a tree nearby
                if (tree != null && treePlot.distance(mc.getPlayers().localPlayer().getTile()) <= 5) {
    
                    // And it hasn't been chopped down
                    if(treePlot.hasAction("Chop down")) {
                        healthChecked = true;
                    }
    
                    if(treePlot.hasAction("Check-health")) {
                        if (treePlot.interact("Check-health")) {
                            mc.log("Checked health");
                            mc.sleep(2000);
                            healthChecked = true;
                        }
                    }
                }
            }
        }
    
        public void chopTree() {
            mc.log("Chopping " + tree + " ...");
    
            GameObject treePlot = mc.getGameObjects().closest(tree);
            // If tree exists
            if (treePlot != null && treePlot.distance(mc.getPlayers().localPlayer().getTile()) <= 5) {
                // And is able to be chopped down
                if (treePlot.hasAction("Chop down")) {
                    // And there is an farmer nearby
                    NPC npc = mc.getNpcs().closest(farmer);
                    // And the farmer is able to be paid
                    if (npc != null && npc.hasAction("Pay")) {
                        if (npc.interact("Pay")) {
                            mc.sleep(500);
                            if(mc.getDialogues().inDialogue()) {
                                mc.sleep(1000);
                                mc.getDialogues().chooseOption(1);
                                mc.sleep(1000);
                                mc.getDialogues().spaceToContinue();
                                mc.log("Paid " + farmer + " to chop down " + tree);
                                isChopped = true;
                            }
                        }
                    }
                }
            }
        }
    
        public void treeInspect() {
            GameObject treePlot = mc.getGameObjects().closest(tree);
            if (treePlot != null && treePlot.distance(mc.getPlayers().localPlayer().getTile()) <= 5) {
                // And is able to be chopped down
                if (treePlot.hasAction("Inspect") && !treePlot.hasAction("Check-health") && !treePlot.hasAction("Chop down")) {
                    healthChecked = true;
                    isChopped = true;
                    treePlanted = true;
                    mc.sleep(5000);
                    mc.log("Health Checked, Chopped and Planted");
                }
            }
        }
    
        public void plantTree() {
            mc.log("Planting da Tree");
            GameObject treePatch = mc.getGameObjects().closest("Tree patch");
    
            // If there is a tree
            if (treePatch != null && treePatch.distance(mc.getPlayers().localPlayer().getTile()) <= 5) {
                // And is it needs to be raked
                if (treePatch.hasAction("Rake")) {
                    treePatch.interact("Rake");
                    mc.sleep(2000);
                    mc.log("Ranked " + tree + " patch");
                } else {
                    mc.getInventory().get(5371).useOn(treePatch);
                    mc.log("Planted " + tree );
                    mc.sleep(2000);
                    treePlanted = true;
                }
            }
        }
    
        public void payFarmer() {
            mc.log("Paying the farmer");
            NPC npc = mc.getNpcs().closest(farmer);
            if (npc != null) {
    
                // If farmer can be paid
                if (npc != null && npc.hasAction("Pay")) {
                    if (npc.interact("Pay")) {
                        mc.sleep(500);
                        if(mc.getDialogues().inDialogue()) {
    
                            if(mc.getDialogues().canContinue()) {
                                farmerPaid = true;
                                isComplete = true;
                            } else {
                                mc.sleep(1000);
                                mc.getDialogues().chooseOption(1);
                                mc.sleep(1000);
                                mc.getDialogues().spaceToContinue();
                                mc.log("Paid " + farmer + " to look after " + tree);
                                farmerPaid = true;
                                isComplete = true;
                            }
                        }
                    }
                } else {
                    farmerPaid = true;
                    isComplete = true;
                }
            }
        }
    
        public boolean hasBeenChopped() {
            if (isChopped == true) {
                MethodContext.log("isChopped == true");
                return true;
            } else if(isChopped == false) {
                mc.log("isChopped == false");
                return false;
            }
    
            return false;
        }
    
        public boolean isInLocation() {
            if (atLocation == true) {
                MethodContext.log("atLocation == true");
                return true;
            } else if(atLocation == false) {
                mc.log("atLocation == false");
                return false;
            } else {
                return false;
            }
        }
    
        public boolean isTreePlanted() {
            if (treePlanted == true) {
                MethodContext.log("treePlanted == true");
                return true;
            } else if(treePlanted == false) {
                mc.log("treePlanted == false");
                return false;
            } else {
                return false;
            }
        }
    
        public boolean isFarmerPaid() {
            if (farmerPaid == true) {
                mc.log("farmerPaid == true");
                return true;
            } else if(farmerPaid == false) {
                mc.log("farmerPaid == false");
                return false;
            } else {
                return false;
            }
        }
    
        public boolean hasBeenHealthChecked() {
            if(healthChecked == true) {
                mc.log("healthChecked == true");
                return true;
            } else {
                mc.log("healthChecked == false");
                return false;
            }
        }
    
        public boolean isBusy() {
            if(!mc.getLocalPlayer().isAnimating() && !mc.getLocalPlayer().isMoving()) {
                return false;
            } else {
                return true;
            }
        }
    
        public boolean checkStatus() {
            return isComplete;
        }
    
    }


     

    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.