private NPC getBestNpcToAttack(ArrayList<String> ourNames){
java.util.List<NPC> allNPCs= client.getLocalNPCs(); //all local npcs
ArrayList<NPC> goodNPCs = new ArrayList<NPC>(); //all npcs with names we are looking for
ArrayList<NPC> temp = new ArrayList<NPC>(); //A holder
int[] currentBest = {-1,1000}; //Used in returning value
//Find all npcs with names we are looking for
for(int i = 0; i < allNPCs.size(); i++){
if(ourNames.contains(allNPCs.get(i).getName())){
temp.add(allNPCs.get(i));
}
}
//Make sure they exist, are alive, and attackable
for(int i = 0; i < temp.size(); i++){
if((temp.get(i).exists())&&(temp.get(i).getHealth()>0)&&!temp.get(i).isUnderAttack()){
goodNPCs.add(temp.get(i));
}
}
if(goodNPCs.size()<1)
return null; //No npcs you can fight
else{
//Find out which npc is closest
for(int i = 0; i < goodNPCs.size(); i++){
if(goodNPCs.get(i).getPosition().distance(myPlayer().getPosition())<currentBest[1]){
currentBest[0] = i;
currentBest[1] = goodNPCs.get(i).getPosition().distance(myPlayer().getPosition());
}
}
if(currentBest[0]==-1) //Should never happen, just a check.
return null;
else{
return goodNPCs.get(currentBest[0]); //returns the closest npc
}
}
}
Usage:
NPC openNPC = getBestNpcToAttack("NPC_NAME_HERE");
openNPC.interact("Attack");
Please post if you can improve it. I just threw this together really fast and thought it would be a good recourse for people new to scripting that want to make a combat script.