Guest User

Untitled

a guest
Jun 21st, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. package com.kbot2.scriptable.methods.data;
  2.  
  3. import com.kbot2.bot.BotEnvironment;
  4. import com.kbot2.scriptable.methods.wrappers.NPC;
  5. import com.kbot2.scriptable.methods.wrappers.Tile;
  6.  
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. /**
  11. * Created by IntelliJ IDEA.
  12. * User: Jan Ove / Kosaki
  13. * Date: 11.apr.2009
  14. * Time: 15:26:33
  15. */
  16. public class NPCs extends Data {
  17. public NPCs(BotEnvironment botEnv) {
  18. super(botEnv);
  19. }
  20.  
  21.  
  22. /**
  23. * Gets an array of all NPCs in clients range.
  24. *
  25. * @return array of all NPCs in range
  26. */
  27. public NPC[] getNPCs() {
  28. com.kbot2.accessors.NPC[] NPCs = botEnv.botInstance.getClient().getNPCs();
  29. List<NPC> out = new ArrayList<NPC>();
  30. for (com.kbot2.accessors.NPC npc : NPCs) {
  31. if (npc == null)
  32. continue;
  33. out.add(new NPC(npc, botEnv));
  34. }
  35. return out.toArray(new NPC[1]);
  36. }
  37.  
  38. /**
  39. * Gets closest NPC in given range by given IDs
  40. *
  41. * @param range Range to search in
  42. * @param ids IDs to search for
  43. * @return If NPC is found; NPC otherwise; null
  44. * @author Alowaniak
  45. */
  46. public NPC getClosest(int range, int... ids) {
  47. Tile myLoc = players.getMyPlayer().getLocation();
  48. double closestDist = 256;
  49. NPC[] allNPCs = getNPCs();
  50. NPC closestNPC = null;
  51. try {
  52. mainLoop: for(NPC tempNPC : allNPCs) {
  53. for(int i : ids) {
  54. int tempDist = tempNPC.getLocation().distanceTo(myLoc);
  55. if(i == tempNPC.getID() && tempDist <= range && tempDist < closestDist) {
  56. closestNPC = tempNPC;
  57. closestDist = tempDist;
  58. continue mainLoop;
  59. }
  60. }
  61. }
  62. } catch (Exception betterSafeThanSorry) {
  63. return null;
  64. }
  65. return closestNPC;
  66. }
  67.  
  68. /**
  69. * Gets closest NPC in given range by given names
  70. *
  71. * @param range Range to search in
  72. * @param names Names to search for
  73. * @return If NPC is found; NPC otherwise; null
  74. * @author Alowaniak
  75. */
  76. public NPC getClosest(int range, String... names) {
  77. Tile myLoc = players.getMyPlayer().getLocation();
  78. double closestDist = 256;
  79. NPC[] allNPCs = getNPCs();
  80. NPC closestNPC = null;
  81. try {
  82. mainLoop: for(NPC tempNPC : allNPCs) {
  83. for(String s : names) {
  84. int tempDist = tempNPC.getLocation().distanceTo(myLoc);
  85. if(tempNPC.getName().equalsIgnoreCase(s) && tempDist <= range) {
  86. closestNPC = tempNPC;
  87. closestDist = tempDist;
  88. continue mainLoop;
  89. }
  90. }
  91. }
  92. } catch (Exception betterSafeThanSorry) {
  93. return closestNPC;
  94. }
  95. return closestNPC;
  96. }
  97. }
Add Comment
Please, Sign In to add comment