Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.kbot2.scriptable.methods.data;
- import com.kbot2.bot.BotEnvironment;
- import com.kbot2.scriptable.methods.wrappers.NPC;
- import com.kbot2.scriptable.methods.wrappers.Tile;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * Created by IntelliJ IDEA.
- * User: Jan Ove / Kosaki
- * Date: 11.apr.2009
- * Time: 15:26:33
- */
- public class NPCs extends Data {
- public NPCs(BotEnvironment botEnv) {
- super(botEnv);
- }
- /**
- * Gets an array of all NPCs in clients range.
- *
- * @return array of all NPCs in range
- */
- public NPC[] getNPCs() {
- com.kbot2.accessors.NPC[] NPCs = botEnv.botInstance.getClient().getNPCs();
- List<NPC> out = new ArrayList<NPC>();
- for (com.kbot2.accessors.NPC npc : NPCs) {
- if (npc == null)
- continue;
- out.add(new NPC(npc, botEnv));
- }
- return out.toArray(new NPC[1]);
- }
- /**
- * Gets closest NPC in given range by given IDs
- *
- * @param range Range to search in
- * @param ids IDs to search for
- * @return If NPC is found; NPC otherwise; null
- * @author Alowaniak
- */
- public NPC getClosest(int range, int... ids) {
- Tile myLoc = players.getMyPlayer().getLocation();
- double closestDist = 256;
- NPC[] allNPCs = getNPCs();
- NPC closestNPC = null;
- try {
- mainLoop: for(NPC tempNPC : allNPCs) {
- for(int i : ids) {
- int tempDist = tempNPC.getLocation().distanceTo(myLoc);
- if(i == tempNPC.getID() && tempDist <= range && tempDist < closestDist) {
- closestNPC = tempNPC;
- closestDist = tempDist;
- continue mainLoop;
- }
- }
- }
- } catch (Exception betterSafeThanSorry) {
- return null;
- }
- return closestNPC;
- }
- /**
- * Gets closest NPC in given range by given names
- *
- * @param range Range to search in
- * @param names Names to search for
- * @return If NPC is found; NPC otherwise; null
- * @author Alowaniak
- */
- public NPC getClosest(int range, String... names) {
- Tile myLoc = players.getMyPlayer().getLocation();
- double closestDist = 256;
- NPC[] allNPCs = getNPCs();
- NPC closestNPC = null;
- try {
- mainLoop: for(NPC tempNPC : allNPCs) {
- for(String s : names) {
- int tempDist = tempNPC.getLocation().distanceTo(myLoc);
- if(tempNPC.getName().equalsIgnoreCase(s) && tempDist <= range) {
- closestNPC = tempNPC;
- closestDist = tempDist;
- continue mainLoop;
- }
- }
- }
- } catch (Exception betterSafeThanSorry) {
- return closestNPC;
- }
- return closestNPC;
- }
- }
Add Comment
Please, Sign In to add comment