December 14, 20187 yr 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
December 14, 20187 yr public boolean npcInsideAreas(Area[] areas,NPC n) { for(Area a: areas) { if(a.contains(n)) { return true; } } return false; } Maybe this?
December 14, 20187 yr 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, 20187 yr by NoxMerc
December 14, 20187 yr 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, 20187 yr by Tom
December 14, 20187 yr public static boolean isInArea(Entity e, Area... areas) { return Stream.of(areas).anyMatch(area -> area.contains(e)); } Edited December 14, 20187 yr by liverare
Create an account or sign in to comment