Temsei Posted December 26, 2016 Share Posted December 26, 2016 Hey, I have a few preset areas where the script will power chop trees. I don't want the bot to walk out of the preset area and currently have a simple check for cutting trees within the area: if (LEVELING_AREA1.contains(tree) { tree.interact("Chop down"); } This creates a small problem. If the tree isn't within LEVELING_AREA1, the script will still hover over the tree and proceed to get stuck as it can't chop it down. How could I make it so it doesn't even attempt to chop the trees down that aren't in the area? Thanks in advance! Quote Link to comment Share on other sites More sharing options...
Precise Posted December 26, 2016 Share Posted December 26, 2016 (edited) Hey, I have a few preset areas where the script will power chop trees. I don't want the bot to walk out of the preset area and currently have a simple check for cutting trees within the area: if (LEVELING_AREA1.contains(tree) { tree.interact("Chop down"); } This creates a small problem. If the tree isn't within LEVELING_AREA1, the script will still hover over the tree and proceed to get stuck as it can't chop it down. How could I make it so it doesn't even attempt to chop the trees down that aren't in the area? Thanks in advance! when attempting to find the closest and most appropriate tree in that area, i would recommend usng a filter like so: RS2Object tree = script.getObjects().closest(t -> t != null && t.getName().equals("TREE_NAME_HERE") && AREA_NAME_HERE.contains(t)); this will only find trees in that area. Precise. Edited December 26, 2016 by Precise 3 Quote Link to comment Share on other sites More sharing options...
Temsei Posted December 26, 2016 Author Share Posted December 26, 2016 (edited) when attempting to find the closest and most appropriate tree in that area, i would recommend usng a filter like so: RS2Object tree = script.getObjects().closest(t -> t != null && t.getName().equals("TREE_NAME_HERE") && AREA_NAME_HERE.contains(t)); this will only find trees in that area. Precise. Thanks a lot! Seems to be working well. Would you mind explaining the steps your code goes through? I'm not fully comprehending it. More specifically this little part here: (t -> t != null And why doesn't this Entity tree = objects.closest("Tree"); if (LEVELING_AREA1.contains(tree) { } accomplish the same thing? Edited December 26, 2016 by Temsei Quote Link to comment Share on other sites More sharing options...
Precise Posted December 26, 2016 Share Posted December 26, 2016 (edited) Thanks a lot! Seems to be working well. Would you mind explaining the steps your code goes through? I'm not fully comprehending it. More specifically this little part here: (t -> t != null And why doesn't this Entity tree = objects.closest("Tree"); if (LEVELING_AREA1.contains(tree) { } accomplish the same thing? Ok so, when you call: Entity tree = objects.closest("Tree"); You're getting the closest object which matches the name "Tree" and that only. So you could get a tree which is closest to you, but isn't in the area, and so it won't click it. The code i posted finds the closest object using a filter and only gets objects which have the name "Tree" AND are in the area you specified. let me know if i wasn't clear. Edit: here is another example of how you can write it, might be easier to understand: RS2Object tree = getObjects().closest(new Filter<RS2Object>() { @[member=Override] public boolean match(RS2Object o) { if(o != null && o.getName().equals("TREE_NAME") && AREA_HERE.contains(o)) { return true; } return false; } }); Precise. Edited December 26, 2016 by Precise 2 Quote Link to comment Share on other sites More sharing options...
Temsei Posted December 26, 2016 Author Share Posted December 26, 2016 Ok so, when you call: Entity tree = objects.closest("Tree"); You're getting the closest object which matches the name "Tree" and that only. So you could get a tree which is closest to you, but isn't in the area, and so it won't click it. The code i posted finds the closest object using a filter and only gets objects which have the name "Tree" AND are in the area you specified. let me know if i wasn't clear. Precise. Ahhh, of course. I got it, thanks a lot buddy! Quote Link to comment Share on other sites More sharing options...
Precise Posted December 26, 2016 Share Posted December 26, 2016 Ahhh, of course. I got it, thanks a lot buddy! No problem Quote Link to comment Share on other sites More sharing options...
Team Cape Posted December 27, 2016 Share Posted December 27, 2016 Just going to add in that the API does have a method for this RS2Object tree = getObjects().closest(AREA_NAME, "Tree"); //finds the closest tree in that area It's the same as what Precise said, but it's always beneficial to have a solid knowledge of the API as well 2 Quote Link to comment Share on other sites More sharing options...