Jump to content

Attack NPC that isn't under attack


Recommended Posts

Posted (edited)

So I'm trying to make the bot not attack the NPC that is under attack. I tried using !closestMonster.isInteracting() but no luck.

NPC closestMonster = s.getNpcs().closest(Vars.radius, Vars.combatMonster);

        if (closestMonster != null && !closestMonster.isAttackable() && s.getInventory().contains(Vars.combatFood)) {
            s.getWalking().walk(closestMonster);

        } else if (closestMonster != null && !s.myPlayer().isUnderAttack() && s.getInventory().contains(Vars.combatFood)) {
            if (s.myPlayer().isAnimating()) {
                Vars.lastAnimation = System.currentTimeMillis();

            } else if (closestMonster.interact("Attack") && System.currentTimeMillis() > (Vars.lastAnimation + 1000)) {
                Paint.status = "Attacking...";
                Sleep.sleepUntil(() -> s.myPlayer().isUnderAttack(), 5000);
            }
        }

 

Edited by Visty
Posted (edited)

I imagine you don't want to attack an NPC which is under attack, but you want to attack one which is not. Here's how to find that monster:

NPC notUnderAttack = getNpcs().closest(new Filter<NPC>() {
                        @Override
                        public boolean match(NPC npc) {
                            return !npc.isUnderAttack() && !npc.isAnimating();
                        }
                    });

 

Edited by Adept
Posted (edited)
7 minutes ago, Adept said:

I imagine you don't want to attack an NPC which is under attack, but you want to attack one which is not. Here's how to find that monster:


NPC notUnderAttack = getNpcs().closest(new Filter<NPC>() {
                        @Override
                        public boolean match(NPC npc) {
                            return npc.isAttackable() && !npc.isHitBarVisible() && !npc.isAnimating();
                        }
                    });

 

But will that fix the issue with people splashing on NPCs? Because, isUnderAttack() seems to fix the issue with people normally attacking the NPC. But when people splash on NPCs, you can't see their HitBar, so it doesn't work

Edited by Visty
Posted (edited)

Don't quote me on this but I believe NPCs "animate" when under attack (and are being splashed on) so !npc.isAnimating() should take care of that.
Also, I just checked the API and the "!npc.isUnderAttack()" check in the filter I provided earlier is redundant since it's already checked for with "npc.isAttackable()". However since you explicitly stated to check for an NPC which is not under attack and not one which is attackable it makes more sense to keep the former. Edited my reply to reflect that.

Edited by Adept
Posted (edited)
3 hours ago, Eliot said:

I think this will work for you.


NPC npc = getNpcs().closest(npc -> npc.getName().equals(npcName) && npc.getInteracting() == null && !npc.isUnderAttack());

If an NPC is being splashed on #getInteracting will not return null (iirc).

I appreciate your responds. Your piece of code works :)

Also, just noticed that my code above checks useless stuff in the If statements lel :doge:

Edited by Visty
  • 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...