Jump to content

Attack NPC that isn't under attack


Viston

Recommended Posts

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

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

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

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

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