/** * @param filter The filtering <code>Filter</code> to accept all the Npcs through. * @return An array of the currently loaded Npcs in the game that are accepted by the provided * filter. */ public static NPC[] getLoaded(final Filter<NPC> filter) { final Client client = Context.client(); final int[] indices = client.getRSNPCIndexArray(); final Set<NPC> npcs = new HashSet<>(); for (final int index : indices) { final Node node = Nodes.lookup((HashTable) client.getRSNPCNC(), index); if (node != null) { NPC npc = null; if (node instanceof RSNPCNode) { npc = new NPC((RSNPC) ((RSNPCNode) node).getRSNPC()); } else if (node instanceof SoftReference) { npc = new NPC( (RSNPC) ((java.lang.ref.SoftReference<?>) ((SoftReference) node).get()).get()); } if (filter.accept(npc)) { npcs.add(npc); } } } return npcs.toArray(new NPC[npcs.size()]); }
/** * @param filter The filtering <code>Filter</code> NPCs have to pass. * @return The nearest Npc passing the filter if any present, else <i>null</i>. */ public static NPC getNearest(final Filter<NPC> filter) { final Client client = Context.client(); final int[] indices = client.getRSNPCIndexArray(); NPC npc = null; double distance = Double.MAX_VALUE; final RegionOffset position = Players.getLocal().getRegionOffset(); for (final int index : indices) { final Node node = Nodes.lookup((HashTable) client.getRSNPCNC(), index); if (node != null && node instanceof RSNPCNode) { final NPC t_npc = new NPC((RSNPC) ((RSNPCNode) node).getRSNPC()); try { if (filter.accept(t_npc)) { final double dist = Calculations.distance(position, t_npc.getRegionOffset()); if (dist < distance) { distance = dist; npc = t_npc; } } } catch (final Exception ignored) { } } } return npc; }