Woody Posted June 1, 2015 Share Posted June 1, 2015 Using this for Woody's Fighter to attack next monster faster. /** * @author Woody * @param entity - The entity which has the chosen option * @param option - The option you want to hover * @return true if hovering over entity option * @throws InterruptedException */ private boolean hoverEntityOption(Entity entity, String option) throws InterruptedException { if(entity != null && menu.isOpen()) { List<Option> options = menu.getMenu(); Rectangle optionRec = null; int index = 0; if(options != null) { for(; index < options.size(); index++) { if(options.get(index).action.equals(option)) { optionRec = menu.getOptionRectangle(index); break; } } } else { return false; } if(optionRec != null) { if(!optionRec.contains(mouse.getPosition())) { int x = menu.getX() + Script.random(10, 160); int y = menu.getY() + 23 + index * 15; Script.sleep(Script.random(200, 400)); return mouse.move(x, y); } } else { return false; } } else if(entity != null && !menu.isOpen()) { EntityDestination ed = new EntityDestination(bot, entity); mouse.click(ed, true); } return false; } How to use: if(myPlayer().getInteracting() != null) { NPC goblin = npcs.closest("Goblin"); if(goblin != null && goblin.isVisible()) { if(hoverEntityOption(goblin, "Attack")) { //the mouse is now hovering attack option and is ready to attack next goblin, when the //fight is over } } } else { if(menu.isOpen()) { if(mouse.click(false)) { for(int i = 0; i < 100 && !myPlayer().isUnderAttack(); i++) { sleep(20, 40); } } } else { if(goblin != null && goblin.isVisible()) { if(goblin.interact("Attack ")) { for(int i = 0; i < 100 && !myPlayer().isUnderAttack(); i++) { sleep(20, 40); } } } else if(goblin != null && !goblin.isVisible()) { camera.toEntity(goblin); } else { log("Goblin gone?!"); } } } Any suggestions/ideas/complains are appreciated. 1 Quote Link to comment Share on other sites More sharing options...
Flamezzz Posted June 1, 2015 Share Posted June 1, 2015 (edited) Nice snippet For moving the mouse inside the rect you might wanna use MouseEvent me = new MoveMouseEvent(new RectangleDestination(bot, menu.getOptionRectangle(idx))); that's what Menu#selectAction does (EDIT: might be better considering the upcoming resizable stuff ) Edited June 1, 2015 by Flamezzz Quote Link to comment Share on other sites More sharing options...
Czar Posted June 1, 2015 Share Posted June 1, 2015 good idea, can even be useful for other skills such as agility Quote Link to comment Share on other sites More sharing options...
Woody Posted June 1, 2015 Author Share Posted June 1, 2015 good idea, can even be useful for other skills such as agility Yes of course. I am using it in my ardy rooftop script to hover next object while performing an animation for faster interaction. Nice snippet For moving the mouse inside the rect you might wanna use MouseEvent me = new MoveMouseEvent(new RectangleDestination(bot, menu.getOptionRectangle(idx))); that's what Menu#selectAction does (EDIT: might be better considering the upcoming resizable stuff ) What are the pros with using MouseEvent? Quote Link to comment Share on other sites More sharing options...
Alek Posted June 1, 2015 Share Posted June 1, 2015 This is a good start and it shows that you know a bit more about scripting than your average Joe, but here are a few suggestions: 1. Try out the new Stream API available in Java 8. 2. Try using events for their intended function. Event e = new Event(), execute(e), e.hasFailed(), e.hasFinished(). Remember that all friendly API methods fall back on events. Mouse.click will fall back on both MoveMouseEvent and ClickMouseEvent. You should look up these events to see how you can fine-tune your own methods. 2 Quote Link to comment Share on other sites More sharing options...
Botre Posted June 1, 2015 Share Posted June 1, 2015 for(; index < options.size(); index++) while loop pls 1 Quote Link to comment Share on other sites More sharing options...
Woody Posted June 1, 2015 Author Share Posted June 1, 2015 This is a good start and it shows that you know a bit more about scripting than your average Joe, but here are a few suggestions: 1. Try out the new Stream API available in Java 8. 2. Try using events for their intended function. Event e = new Event(), execute(e), e.hasFailed(), e.hasFinished(). Remember that all friendly API methods fall back on events. Mouse.click will fall back on both MoveMouseEvent and ClickMouseEvent. You should look up these events to see how you can fine-tune your own methods. Will do! Thank you! for(; index < options.size(); index++) while loop pls The difference? Quote Link to comment Share on other sites More sharing options...
Botre Posted June 1, 2015 Share Posted June 1, 2015 (edited) The difference? While loops are sexy and more readable. //#EYECANCER for(;;) { if(!condition) break; doSomething(); } //#EYECANDY while(condition) { doSomething(); } Edited June 1, 2015 by Botre 1 Quote Link to comment Share on other sites More sharing options...
Woody Posted June 1, 2015 Author Share Posted June 1, 2015 While loops are sexy and more readable. //#EYECANCER for(;;) { if(!condition) break; doSomething(); } //#EYECANDY while(condition) { doSomething(); } Me != you. Jokes. I understand you. 1 Quote Link to comment Share on other sites More sharing options...
fixthissite Posted June 1, 2015 Share Posted June 1, 2015 (edited) Nice, I'm sure it will help out quite a few people. Here's some advice to hopefully improve the readability (and managability) of your code: No need to check if entity != null twice (if statement and else if statement). Simply do if(entity != null) { if(menu.isOpen()) { } else { } } You have quite a few un-needed return statements. Rather than calling return when options == null, format your code to allow the flow of execution "do nothing" until the return statement at the bottom: private boolean hoverEntityOption(Entity entity, String option) throws InterruptedException { if(entity == null) return false; if(menu.isOpen()) { List<Option> options = menu.getMenu(); if(options != null) { Rectangle optionRec = null; for(int index = 0; index < options.size(); index++) { if(options.get(index).action.equals(option)) { optionRec = menu.getOptionRectangle(index); if(optionRec != null) { if(!optionRec.contains(mouse.getPosition())) { int x = menu.getX() + Script.random(10, 160); int y = menu.getY() + 23 + index * 15; Script.sleep(Script.random(200, 400)); return mouse.move(x, y); } } } } } } else { EntityDestination ed = new EntityDestination(bot, entity); mouse.click(ed, true); } return false; } As you can tell, I also fixed up the wierd scoping. If you ever find yourself declaring the tracker variable for a for loop outside of the loop, best believe you have a structuring problem. You were breaking from the loop, just to have another block of code (which depends on the tracker variable; the whole reason you have the for loop) return from the entire method. I would recommend the Stream API to decrease the amount of time it takes to iterate through a collection, as Alek suggested, but it might interfere with how you're specifying your mouse location. Still quite a bit of cognition needed to understand the method. We can lower it through decomposition: private boolean hoverOverEntityOption(Entity entity, String option) throws InterruptedException { if(entity == null) return false; if(menu.isOpen()) return moveMouse(option); else clickEntity(entity); return false; } private boolean moveMouse(String option) { List<Option> options = menu.getMenu(); if(options != null) for(int index = 0; index < options.size(); index++) if(options.get(index).action.equals(option)) { optionRec = menu.getOptionRectangle(index); if(optionRec != null) if(!optionRec.contains(mouse.getPosition())) { int x = menu.getX() + Script.random(10, 160); int y = menu.getY() + 23 + index * 15; Script.sleep(Script.random(200, 400)); return mouse.move(x, y); } } } private void clickEntity(Entity entity) { EntityDestination ed = new EntityDestination(getBot(), entity); mouse.click(ed, true); } Keep up the good work man Edited June 1, 2015 by fixthissite 1 Quote Link to comment Share on other sites More sharing options...
Woody Posted June 1, 2015 Author Share Posted June 1, 2015 Nice, I'm sure it will help out quite a few people. Here's some advice to hopefully improve the readability (and managability) of your code: No need to check if entity != null twice (if statement and else if statement). Simply do if(entity != null) { if(menu.isOpen()) { } else { } } You have quite a few un-needed return statements. Rather than calling return when options == null, format your code to allow the flow of execution "do nothing" until the return statement at the bottom: private boolean hoverEntityOption(Entity entity, String option) throws InterruptedException { if(entity == null) return false; if(menu.isOpen()) { List<Option> options = menu.getMenu(); if(options != null) { Rectangle optionRec = null; for(int index = 0; index < options.size(); index++) { if(options.get(index).action.equals(option)) { optionRec = menu.getOptionRectangle(index); if(optionRec != null) { if(!optionRec.contains(mouse.getPosition())) { int x = menu.getX() + Script.random(10, 160); int y = menu.getY() + 23 + index * 15; Script.sleep(Script.random(200, 400)); return mouse.move(x, y); } } } } } } else { EntityDestination ed = new EntityDestination(bot, entity); mouse.click(ed, true); } return false; } As you can tell, I also fixed up the wierd scoping. If you ever find yourself declaring the tracker variable for a for loop outside of the loop, best believe you have a structuring problem. You were breaking from the loop, just to have another block of code (which depends on the tracker variable; the whole reason you have the for loop) return from the entire method. I would recommend the Stream API to decrease the amount of time it takes to iterate through a collection, as Alek suggested, but it might interfere with how you're specifying your mouse location. Still quite a bit of cognition needed to understand the method. We can lower it through decomposition: private boolean hoverOverEntityOption(Entity entity, String option) throws InterruptedException { if(entity == null) return false; if(menu.isOpen()) return moveMouse(option); else clickEntity(entity); return false; } private boolean moveMouse(String option) { List<Option> options = menu.getMenu(); if(options != null) for(int index = 0; index < options.size(); index++) if(options.get(index).action.equals(option)) { optionRec = menu.getOptionRectangle(index); if(optionRec != null) if(!optionRec.contains(mouse.getPosition())) { int x = menu.getX() + Script.random(10, 160); int y = menu.getY() + 23 + index * 15; Script.sleep(Script.random(200, 400)); return mouse.move(x, y); } } } private void clickEntity(Entity entity) { EntityDestination ed = new EntityDestination(getBot(), entity); mouse.click(ed, true); } Keep up the good work man Is it worth using Stream API in this method? This method is not so much timeconsuming and it goes pretty fast. But there are maybe some other pros with Stream API which I am not aware of. Other than that, I really appreciate your help! Quote Link to comment Share on other sites More sharing options...
fixthissite Posted June 1, 2015 Share Posted June 1, 2015 (edited) Is it worth using Stream API in this method? This method is not so much timeconsuming and it goes pretty fast. But there are maybe some other pros with Stream API which I am not aware of. Other than that, I really appreciate your help! Nope. There are some bonus features, other than parallelism, but none of which you could be taking advantage of in this code Edited June 1, 2015 by fixthissite Quote Link to comment Share on other sites More sharing options...
Joseph Posted June 7, 2015 Share Posted June 7, 2015 I wpuld of use a for loop for those options. It is also better to check and see if the list is empty rather thrn null. Quote Link to comment Share on other sites More sharing options...
Prolax Posted September 22, 2015 Share Posted September 22, 2015 if(myPlayer().getInteracting() != null) { NPC npc = npcs.closest(npc_name); if (npc != null && npc.isVisible()) { if (hoverEntityOption(npc, "Attack")) { if (myPlayer().getInteracting() != npc) { if (npc.isVisible()) { npc.interact("Attack"); log("Attacking " + npc_name); sleep(random(400, 800)); } else { camera.toEntity(npc); } } } } }else { if(menu.isOpen()) { if(mouse.click(false)) { for(int i = 0; i < 100 && !myPlayer().isUnderAttack(); i++) { sleep(random(20,40)); } } } else { if(npc != null && npc.isVisible()) { if(npc.interact("Attack")) { for(int i = 0; i < 100 && !myPlayer().isUnderAttack(); i++) { sleep(random(20,40)); } } } else if(npc != null && !npc.isVisible()) { camera.toEntity(npc); } else { log(npc_name + " gone?"); } } } I'm having trouble implementing your method. What am I doing wrong? The script keeps logging "npc_name gone?" Quote Link to comment Share on other sites More sharing options...
FrostBug Posted September 23, 2015 Share Posted September 23, 2015 if(myPlayer().getInteracting() != null) { NPC npc = npcs.closest(npc_name); if (npc != null && npc.isVisible()) { if (hoverEntityOption(npc, "Attack")) { if (myPlayer().getInteracting() != npc) { if (npc.isVisible()) { npc.interact("Attack"); log("Attacking " + npc_name); sleep(random(400, 800)); } else { camera.toEntity(npc); } } } } }else { if(menu.isOpen()) { if(mouse.click(false)) { for(int i = 0; i < 100 && !myPlayer().isUnderAttack(); i++) { sleep(random(20,40)); } } } else { if(npc != null && npc.isVisible()) { if(npc.interact("Attack")) { for(int i = 0; i < 100 && !myPlayer().isUnderAttack(); i++) { sleep(random(20,40)); } } } else if(npc != null && !npc.isVisible()) { camera.toEntity(npc); } else { log(npc_name + " gone?"); } } } I'm having trouble implementing your method. What am I doing wrong? The script keeps logging "npc_name gone?" Your NPC is null. Quote Link to comment Share on other sites More sharing options...