Advertisement
Guest User

Farmer Bot

a guest
Dec 20th, 2018
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.23 KB | None | 0 0
  1. //Main
  2. import org.dreambot.api.script.AbstractScript;
  3. import org.dreambot.api.script.Category;
  4. import org.dreambot.api.script.ScriptManifest;
  5. import org.dreambot.api.Client;
  6. import org.dreambot.api.methods.map.Area;
  7. import java.util.ArrayList;
  8. import java.util.Arrays;
  9. import java.util.List;
  10.  
  11. import org.dreambot.api.wrappers.interactive.GameObject;
  12. import org.dreambot.api.wrappers.widgets.message.Message;
  13. import org.dreambot.api.script.listener.MessageListener;
  14. import org.dreambot.api.wrappers.widgets.chatbox.ChatboxMessage;
  15.  
  16. import TreePatch.TreePatch;
  17.  
  18. @ScriptManifest(
  19.         author = "Liondedan",
  20.         description = "Completes a farming tree run",
  21.         category = Category.FARMING,
  22.         version = 1.0,
  23.         name = "Farming: Tree Run"
  24. )
  25.  
  26. // TODO: (priority high --> low)
  27. //  - Loop over farming tree locations and initiate TreePatch package
  28. //  - Implement OnMessage within TreePatch package to fix inspecing of patch
  29. //  - Navigate between patches using house teleport (no teleport needed between Falador & Taverly)
  30. //  - Get all required equipment from bank for tree run
  31. //  - Paint GUI
  32. //  - Support selection of specific trees and locations
  33.  
  34. public class Main extends AbstractScript {
  35.  
  36.     // trees
  37.     String tree = "Willow Tree";
  38.  
  39.     List<TreePatch> locations = Arrays.asList(
  40.         new TreePatch(this, tree, "Treznor", "Varrock", new Area (3226, 3461,3231, 3456)),
  41.         new TreePatch(this, tree,"Alain", "Taverly", new Area (2933, 3441,2939, 3435)),
  42.         new TreePatch(this, tree,"Heskel", "Falador",  new Area (3001, 3376,3007, 3370)),
  43.         new TreePatch(this, tree,"Prissy Scilla", "Stronghold", new Area (2433, 3418,2438, 3411)),
  44.         new TreePatch(this, tree,"Fayeth", "Lumbridge", new Area (3190, 3233,3196, 3229)),
  45.         new TreePatch(this, tree,"Heskel", "Falador",  new Area (3001, 3376,3007, 3370))
  46.     );
  47.  
  48.     // TODO: Need to implement within TreePatch package
  49. //    @Override
  50. //    public void onMessage(Message message) {
  51. //        super.onMessage(message);
  52. //
  53. //        // inspected an empty patch
  54. //        if(message.getMessage() != null && (message.getMessage().toLowerCase().contains("patch is empty"))){
  55. //            log("The patch is empty");
  56. //            locationPlot.setTreePlanted(false);
  57. //        }
  58. //
  59. //        // Inspected a growing tree that has not been treated
  60. //        if(message.getMessage() != null && message.getMessage().toLowerCase().contains("patch has something growing") &  message.getMessage().toLowerCase().contains("not been treated")){
  61. //            log("Tree is growing but needs to be looked after");
  62. //            locationPlot.setTreePlanted(true);
  63. //            locationPlot.setFarmerPaid(false);
  64. //        }
  65. //    }
  66.  
  67.     public boolean isBusy() {
  68.         if(getLocalPlayer().isAnimating() || getLocalPlayer().isMoving()) {
  69.             return true;
  70.         } else {
  71.             return false;
  72.         }
  73.     }
  74.  
  75.     @Override
  76.     public int onLoop() {
  77.  
  78.         locations.forEach(location -> {
  79.             if (isBusy() == false) {
  80.                 location.getToLocation();
  81.             if (location.isCompleted()) {
  82.             } else {
  83.                 if (location.isInLocation() == false) {
  84.                     location.getToLocation();
  85.                 } else {
  86.                     if (location.hasBeenHealthChecked() == false) {
  87.                         location.checkHealth();
  88.                     } else if (location.hasBeenChopped() == false) {
  89.                         location.chopTree();
  90.                     } else if (location.isTreePlanted() == false) {
  91.                         location.plantTree();
  92.                     } else if (location.isFarmerPaid() == false) {
  93.                         location.payFarmer();
  94.                     }
  95.                 }
  96.             }
  97.             }
  98.         });
  99.         return 1000;
  100.     }
  101. }
  102.  
  103.  
  104. //TreePatch
  105. package TreePatch;
  106.  
  107. import org.dreambot.api.methods.MethodContext;
  108. import org.dreambot.api.methods.map.Area;
  109. import org.dreambot.api.wrappers.interactive.GameObject;
  110. import org.dreambot.api.wrappers.interactive.NPC;
  111.  
  112. public class TreePatch {
  113.     private static MethodContext mc;
  114.     String tree;
  115.     String farmer;
  116.     String city;
  117.     Area location;
  118.  
  119.     Boolean atLocation = false;
  120.     Boolean healthChecked = false;
  121.     Boolean isChopped = false;
  122.     Boolean treePlanted = false;
  123.     Boolean farmerPaid = false;
  124.     Boolean isComplete = false;
  125.  
  126.  
  127.     // This is the constructor of the class Employee
  128.     public TreePatch(MethodContext mc, String tree, String farmer, String city, Area location) {
  129.         this.mc = mc;
  130.         this.location = location;
  131.         this.tree = tree;
  132.         this.farmer = farmer;
  133.         this.city = city;
  134.     }
  135.  
  136.     public void getToLocation() {
  137.         if (location.contains(mc.getLocalPlayer())) {
  138.             mc.log("Already at " + city + " " + tree + " location");
  139.             atLocation = true;
  140.  
  141.         } else {
  142.             mc.log("Walking to " + city + " " + tree + " location");
  143.             mc.getWalking().walk(location.getRandomTile());
  144.         }
  145.     }
  146.  
  147.     public void checkHealth() {
  148.         if (!healthChecked == true) {
  149.             mc.log(city + " " + tree + ": Needs Health Check");
  150.             GameObject treeLocation = mc.getGameObjects().closest(tree);
  151.  
  152.             // If there is a tree nearby
  153.             if (treeLocation != null && treeLocation.distance(mc.getPlayers().localPlayer().getTile()) <= 5) {
  154.  
  155.                 // And it hasn't been chopped down
  156.                 if(treeLocation.hasAction("Chop down")) {
  157.                     healthChecked = true;
  158.                 } else if (treeLocation.hasAction("Check-health")) {
  159.                     if (treeLocation.interact("Check-health")) {
  160.                         mc.log("Checked health");
  161.                         mc.sleep(2000);
  162.                         healthChecked = true;
  163.                     }
  164.                 } else {
  165.                     isChopped = true;
  166.                     healthChecked = true;
  167.                 }
  168.             } else {
  169.                 isChopped = true;
  170.                 healthChecked = true;
  171.             }
  172.         }
  173.     }
  174.  
  175.     public void chopTree() {
  176.         mc.log("Chopping " + tree + " ...");
  177.  
  178.         GameObject treePlot = mc.getGameObjects().closest(tree);
  179.         // If tree exists
  180.         if (treePlot != null && treePlot.distance(mc.getPlayers().localPlayer().getTile()) <= 5) {
  181.             // And is able to be chopped down
  182.             if (treePlot.hasAction("Chop down")) {
  183.                 // And there is an farmer nearby
  184.                 NPC npc = mc.getNpcs().closest(farmer);
  185.                 // And the farmer is able to be paid
  186.                 if (npc != null && npc.hasAction("Pay")) {
  187.                     if (npc.interact("Pay")) {
  188.                         mc.sleep(500);
  189.                         if(mc.getDialogues().inDialogue()) {
  190.                             mc.sleep(1000);
  191.                             mc.getDialogues().chooseOption(1);
  192.                             mc.sleep(1000);
  193.                             mc.getDialogues().spaceToContinue();
  194.                             mc.log("Paid " + farmer + " to chop down " + tree);
  195.                             isChopped = true;
  196.                         }
  197.                     }
  198.                 }
  199.             }
  200.         }
  201.     }
  202.  
  203.     public void inspectPlot() {
  204.         GameObject treePatch = mc.getGameObjects().closest("Tree patch");
  205.         GameObject treePlot = mc.getGameObjects().closest(tree);
  206.  
  207.         // If there is a treePatch
  208.         if (treePatch != null && treePatch.distance(mc.getPlayers().localPlayer().getTile()) <= 5) {
  209.             mc.log("Need to inspect tree patch...");
  210.             // And it doesn't need to be raked and can be inspected
  211.             if (!treePatch.hasAction("Rake") & treePatch.hasAction("Inspect")) {
  212.                 if (treePatch.interact("Inspect")) {
  213.                     mc.log("Inspecting Plot...");
  214.                     mc.sleep(2000);
  215.                 }
  216.             }
  217.         }
  218.  
  219.         if (treePlot != null && treePlot.distance(mc.getPlayers().localPlayer().getTile()) <= 5) {
  220.             mc.log("Need to inspect tree plot...");
  221.             // And it can be inspected
  222.             if (treePlot.hasAction("Inspect")) {
  223.                 if (treePlot.interact("Inspect")) {
  224.                     mc.log("Inspecting Plot...");
  225.                     mc.sleep(2000);
  226.                 }
  227.             }
  228.         }
  229.     }
  230.  
  231.     public void plantTree() {
  232.         inspectPlot();
  233.         mc.log("Need to plant a tree");
  234.         GameObject treePatch = mc.getGameObjects().closest("Tree patch");
  235.         GameObject treePlot = mc.getGameObjects().closest(tree);
  236.  
  237.         // If there is a tree
  238.         if (treePatch != null && treePatch.distance(mc.getPlayers().localPlayer().getTile()) <= 5) {
  239.             // And is it needs to be raked
  240.             if (treePatch.hasAction("Rake")) {
  241.                 treePatch.interact("Rake");
  242.                 mc.sleep(2000);
  243.                 mc.log("Ranked " + tree + " patch");
  244.             } else {
  245.                 mc.getInventory().get(5371).useOn(treePatch);
  246.                 mc.log("Planted " + tree );
  247.                 mc.sleep(3000);
  248.                 treePlanted = true;
  249.             }
  250.         } else if (treePlot != null && treePlot.distance(mc.getPlayers().localPlayer().getTile()) <= 5) {
  251.             mc.log("There is still a tree here - needs to be chopped or health checked");
  252.             healthChecked = false;
  253.             isChopped = false;
  254.         }
  255.     }
  256.  
  257.     public void payFarmer() {
  258.         mc.log("Paying the farmer");
  259.         NPC npc = mc.getNpcs().closest(farmer);
  260.         if (npc != null) {
  261.  
  262.             // If farmer can be paid
  263.             if (npc != null && npc.hasAction("Pay")) {
  264.                 if (npc.interact("Pay")) {
  265.                     mc.sleep(3000);
  266.                     if(mc.getDialogues().inDialogue()) {
  267.                         mc.sleep(3000);
  268.  
  269.                         mc.getDialogues().chooseOption(1);
  270.                         mc.sleep(3000);
  271.                         mc.getDialogues().spaceToContinue();
  272.                         mc.log("Paid " + farmer + " to look after " + tree);
  273.                         farmerPaid = true;
  274.                         isComplete = true;
  275.                     }
  276.                 }
  277.             }
  278.         }
  279.     }
  280.  
  281.     // Setters
  282.     public void setTreePlanted(Boolean s){
  283.         this.treePlanted = s;
  284.     }
  285.  
  286.     public void setFarmerPaid(Boolean s){
  287.         this.farmerPaid = s;
  288.     }
  289.  
  290.     // Getters
  291.     public boolean hasBeenChopped() {
  292.         if (isChopped == true) {
  293.             MethodContext.log("isChopped == true");
  294.             return true;
  295.         } else if(isChopped == false) {
  296.             mc.log("isChopped == false");
  297.             return false;
  298.         }
  299.  
  300.         return false;
  301.     }
  302.  
  303.     public boolean isInLocation() {
  304.         if (atLocation == true) {
  305.             MethodContext.log("atLocation == true");
  306.             return true;
  307.         } else if(atLocation == false) {
  308.             mc.log("atLocation == false");
  309.             return false;
  310.         } else {
  311.             return false;
  312.         }
  313.     }
  314.  
  315.     public boolean isTreePlanted() {
  316.         if (treePlanted == true) {
  317.             MethodContext.log("treePlanted == true");
  318.             return true;
  319.         } else if(treePlanted == false) {
  320.             mc.log("treePlanted == false");
  321.             return false;
  322.         } else {
  323.             return false;
  324.         }
  325.     }
  326.  
  327.     public boolean isFarmerPaid() {
  328.         if (farmerPaid == true) {
  329.             mc.log("farmerPaid == true");
  330.             return true;
  331.         } else if(farmerPaid == false) {
  332.             mc.log("farmerPaid == false");
  333.             return false;
  334.         } else {
  335.             return false;
  336.         }
  337.     }
  338.  
  339.     public boolean hasBeenHealthChecked() {
  340.         if(healthChecked == true) {
  341.             mc.log("healthChecked == true");
  342.             return true;
  343.         } else {
  344.             mc.log("healthChecked == false");
  345.             return false;
  346.         }
  347.     }
  348.  
  349.     public boolean isCompleted() {
  350.         if(isComplete == true) {
  351.             mc.log("isComplete == true");
  352.             return true;
  353.         } else {
  354.             mc.log("isComplete == false");
  355.             return false;
  356.         }
  357.     }
  358. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement