Juggles Posted December 14, 2018 Share Posted December 14, 2018 Good morning everyone. Just had a quick question if someone would like to help me out. I am trying to filter my NPC to only attack ones that are in my array of areas. I know how to do it if it was just a single area by doing area.contains(f) but with an Array it is different. Here is my code I have Area[] CONTAINS_NPC_AREA = { CHICKENS,COWS,BARBARIANS,WARRIORS }; NPC npc = getNpcs().closest(f -> f != null && f.getName().equals(name) && map.canReach(f) && f.isAttackable() && f.getHealthPercent() > 0 && f.hasAction("Attack")); I know how to check if myPlayer is in one of the Array of areas with this code. I tried to apply a similar method to my NPC filter, but could not get it working. public boolean myPlayerInsideAreas(Area[] areas) { for(Area a: areas) { if(a.contains(myPlayer())) { return true; } } return false; } Any help would be greatly appreciated Quote Link to comment Share on other sites More sharing options...
progamerz Posted December 14, 2018 Share Posted December 14, 2018 public boolean npcInsideAreas(Area[] areas,NPC n) { for(Area a: areas) { if(a.contains(n)) { return true; } } return false; } Maybe this? 1 Quote Link to comment Share on other sites More sharing options...
NoxMerc Posted December 14, 2018 Share Posted December 14, 2018 (edited) Why not just make an enum for the areas and map the available NPCs to them? You don't need to have a runtime calculation to determine what NPCs are in your area when you have prior knowledge. public enum NpcArea { CHICKENS("Chicken", new Area(x1, y1, x2, y2)), BARBARIANS("Barbarian", new Area(x1, y1, x2, y2)); private final String npcName; private final Area area; NpcArea(String name, Area area) { this.npcName = name; this.area = area; } public static NpcArea fromMyLocation(MethodProvider api) { return Arrays.stream(NpcArea.values()).filter(f -> f.area.contains(api.myPosition())).findFirst().orElse(null); } } And then later on... NpcArea myArea = NpcArea.fromMyLocation(this); // Store this for later use if (myArea == null) { // Walk to area } else { NPC npcToAttack = npcs.closest(myArea.npcName); } Edited December 14, 2018 by NoxMerc 1 Quote Link to comment Share on other sites More sharing options...
Tom Posted December 14, 2018 Share Posted December 14, 2018 (edited) 1 hour ago, progamerz said: public boolean npcInsideAreas(Area[] areas,NPC n) { for(Area a: areas) { if(a.contains(n)) { return true; } } return false; } Maybe this? Better yet public boolean npcInsideAreas(Area[] areas, NPC n) { return Arrays.asList(areas).stream().anyMatch(a -> a.contains(n)); } Should do the trick. @Juggles What you have is right, you just need to substitute Player for the NPC. They are both entities, so I don't see why it wouldn't work. Alternatively you can check contains(npc.getPosition()) Edited December 14, 2018 by Tom 1 Quote Link to comment Share on other sites More sharing options...
liverare Posted December 14, 2018 Share Posted December 14, 2018 (edited) public static boolean isInArea(Entity e, Area... areas) { return Stream.of(areas).anyMatch(area -> area.contains(e)); } Edited December 14, 2018 by liverare 2 Quote Link to comment Share on other sites More sharing options...
Juggles Posted December 14, 2018 Author Share Posted December 14, 2018 Thanks for the help everyone This is why I love this community. Quote Link to comment Share on other sites More sharing options...