Use of a filter would be appropriate here
A filter is a way of it only accepting a entity if it meets all the criteria.
Currently your only criteria is for the entity to be named "Cow" which,
even with the checks underneath will still find any cow at all.
The only reason you dont attack one whilst fighting is because of some of the checks underneath.
Your check in filter form would look like:
NPC cow = npcs.closest(new Filter<NPC>() {
@Override
public boolean match(NPC npc) {
return npc != null && npc.getName().equals("Cow");
}
});
Which checks would return the same but you only want to find a cow not in combat so you'd need to add more checks
NPC cow = npcs.closest(new Filter<NPC>() {
@Override
public boolean match(NPC npc) {
return npc != null && npc.getName().contains("Cow") && !npc.getName().contains("Dairy")
&& !npc.isUnderAttack() && npc.isAttackable() && npc.getHealth() > 0;
}
});
This check checks for the name, and would allow inclusion of the little cows but excludes dairy cows
after it has identified the fact its a cow it moves on to check:
Is the npc under attack?
is the npc able to be attacked by your player?
is its health > 0? (avoid clicking on cows which are dying)
you can add as many checks to this as you like.
Sorry if my explanation is bad