Jump to content

Hovering entity's option


Woody

Recommended Posts

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.

  • Like 1
Link to comment
Share on other sites

Nice snippet smile.png

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 happy.png )

Edited by Flamezzz
Link to comment
Share on other sites

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 smile.png

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 happy.png )

What are the pros with using MouseEvent?

Link to comment
Share on other sites

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.

 

 

 

  • Like 2
Link to comment
Share on other sites

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 sad.png

 

The difference?

Link to comment
Share on other sites

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 smile.png

Edited by fixthissite
  • Like 1
Link to comment
Share on other sites

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 smile.png

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!

Link to comment
Share on other sites

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 by fixthissite
Link to comment
Share on other sites

  • 3 months later...
                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?"

 

 

Link to comment
Share on other sites

                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.

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...