Jump to content

Every once in a while my player will attack another npc while already under attack.. why?


Recommended Posts

Posted
 NPC cow = main.getNpcs().closest(npc -> npc.getName().startsWith("cow") && npc.isAttackable());
            if (cow != null) {
                main.log("cow not null");
                if (!main.myPlayer().isInteracting(cow) && !cow.isInteracting(main.myPlayer()) && !main.myPlayer().isUnderAttack()) {
                    InteractionEvent e = new InteractionEvent(cow, "Attack");
                    e.setHover(false);
                    e.setMaximumAttempts(1);
                    e.setOperateCamera(true);
                    e.setWalkTo(false);
                    main.execute(e);
                    Util.Sleep.sleepUntil(() -> e.hasFailed() || main.myPlayer().isInteracting(cow) || cow.isInteracting(main.myPlayer()), 3000);
                }

95 percent of the time it can run for ages no problem, but every once in a while it will try to attack another npc and then get stuck trying to attack it while being attacked. Also this process only runs if !getCombat.isfighting, so theres another check. 

 

Just dont know what else i can do.

 

Insight welcome pls :)

Posted (edited)

 

44 minutes ago, saintpaul1 said:
 NPC cow = main.getNpcs().closest(npc -> npc.getName().startsWith("cow") && npc.isAttackable());
            if (cow != null) {
                main.log("cow not null");
                if (!main.myPlayer().isInteracting(cow) && !cow.isInteracting(main.myPlayer()) && !main.myPlayer().isUnderAttack()) {
                    InteractionEvent e = new InteractionEvent(cow, "Attack");
                    e.setHover(false);
                    e.setMaximumAttempts(1);
                    e.setOperateCamera(true);
                    e.setWalkTo(false);
                    main.execute(e);
                    Util.Sleep.sleepUntil(() -> e.hasFailed() || main.myPlayer().isInteracting(cow) || cow.isInteracting(main.myPlayer()), 3000);
                }

95 percent of the time it can run for ages no problem, but every once in a while it will try to attack another npc and then get stuck trying to attack it while being attacked. Also this process only runs if !getCombat.isfighting, so theres another check. 

 

Just dont know what else i can do.

 

Insight welcome pls :)

Have your script sleep 5-7+ seconds after you get hit OR hit a cow so that it accounts for connection lag. Seems like that's what might be breaking your script. 

Edited by 07ETHFarmer
Posted
11 hours ago, saintpaul1 said:
 NPC cow = main.getNpcs().closest(npc -> npc.getName().startsWith("cow") && npc.isAttackable());
            if (cow != null) {
                main.log("cow not null");
                if (!main.myPlayer().isInteracting(cow) && !cow.isInteracting(main.myPlayer()) && !main.myPlayer().isUnderAttack()) {
                    InteractionEvent e = new InteractionEvent(cow, "Attack");
                    e.setHover(false);
                    e.setMaximumAttempts(1);
                    e.setOperateCamera(true);
                    e.setWalkTo(false);
                    main.execute(e);
                    Util.Sleep.sleepUntil(() -> e.hasFailed() || main.myPlayer().isInteracting(cow) || cow.isInteracting(main.myPlayer()), 3000);
                }

95 percent of the time it can run for ages no problem, but every once in a while it will try to attack another npc and then get stuck trying to attack it while being attacked. Also this process only runs if !getCombat.isfighting, so theres another check. 

 

Just dont know what else i can do.

 

Insight welcome pls :)

Because you are loading the cloest cow with this code, that won't always be the cow you are fighting if another ones get into the same range as the one you are already fighting.

 NPC cow = main.getNpcs().closest(npc -> npc.getName().startsWith("cow") && npc.isAttackable());


What you can do is check if you are under attack by a cow, if so don't do anything, else attack a new one

NPC npcAttackingMe = script.getNpcs().closest(npc -> npc.isInteracting(script.myPlayer()) && npc.hasAction("Attack") && npc.getHealthPercent() > 0 && script.getMap().canReach(npc));

 

  • Like 2
Posted
2 hours ago, Khaleesi said:

Because you are loading the cloest cow with this code, that won't always be the cow you are fighting if another ones get into the same range as the one you are already fighting.

 NPC cow = main.getNpcs().closest(npc -> npc.getName().startsWith("cow") && npc.isAttackable());


What you can do is check if you are under attack by a cow, if so don't do anything, else attack a new one

NPC npcAttackingMe = script.getNpcs().closest(npc -> npc.isInteracting(script.myPlayer()) && npc.hasAction("Attack") && npc.getHealthPercent() > 0 && script.getMap().canReach(npc));

 

Thanks khal thats exactly my problem! Thanks for explaining! :>)

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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