How to Attack NPC's
- You'll need an NPC's name or ID.
- You're going to need to find that NPC, make sure it exists, it isn't in combat with another player or anything else and then interact with the NPC.
If you'd like to find the NPC with a name.
NPC cow = NPCs.closest("Cow"); //This will find the closest NPC that has the name "cow"
if(cow != null && !cow.isInCombat()) { //Make sure the cow isn't null and isn't in combat
cow.interact("Attack"); //Interact in this example we want to attack
}
If you'd like to find the NPC with an ID
NPC cow = NPCs.closest(2790); //This will find the closest NPC that has the ID 2790
if(cow != null && !cow.isInCombat()) { //Make sure the cow isn't null and isn't in combat
cow.interact("Attack"); //Interact in this example we want to attack
}
You are able to put the above checks directly into a Lambda expression.
NPC cow = NPCs.closest(c -> "cow".equalsIgnoreCase(c.getName()) && !c.isInCombat());
NPC cow = NPCs.closest(c -> c.getID() == 2790 && !c.isInCombat());