roguehippo Posted May 2, 2016 Share Posted May 2, 2016 hey there guys. im trying to learn how to use filters when defining an npc, mostly to find the closest npc that has a specific name, and is inside of a certain pre-defined area. my current code is: public boolean ShouldAttack() { NPC monster = npcs.closest(n -> ( !n.isUnderAttack() && n.getName().contains(g.name) && (g.monsterArea.contains(n) || g.monsterArea2.contains(n)) )); log("attacking"); if(monster.exists()) { log(monster.getName()); log(monster.getCurrentHealth()); if(g.safespot.contains(myPlayer()) && !myPlayer().isAnimating()) return true; } return false; } and returns a null pointer error whenever i do anything with monster. as im testing this the monsters are in the area and it should be returning true. the g.xxxx are just my global variables. initialized like so. public static class g { static Area safespot = new Area(3154, 9908, 3153, 9908); static Position SafeSpot = new Position(3154,9908,0); static String name = "Moss Giant"; static String ammo = "Iron arrow"; static String food = "Tuna"; static boolean shouldsafespot = true; static Area monsterArea = new Area(3155,9907,3159,9906); static Area monsterArea2= new Area(3157,9905,3160,9901); static String lootlist[] = {"Coins", "Law rune", "Nature rune", "Chaos rune", "Chaos rune" ,"Earth rune", "Air rune"}; } Quote Link to comment Share on other sites More sharing options...
Lone Posted May 2, 2016 Share Posted May 2, 2016 (edited) I recommend formatting it in code tags. I have not made a fighter yet, this is how I would do it. I believe this is what you are talking about. NPC monster = getNpcs().closest("monstername"); if(SAFESPOT.contains(myPlayer())){ if(!myPlayer().isAnimating()) if(monster != null) monster.interact("Attack") Edited May 2, 2016 by Bradf3rd Quote Link to comment Share on other sites More sharing options...
Token Posted May 2, 2016 Share Posted May 2, 2016 (edited) There are generally 2 ways to filter the NPC/Objects/GroundItems collections. 1. Using OSBot Filter interface NPC monster = npcs.closest(new NameFilter<NPC>(NAME), new AreaFilter<NPC>(AREA), new PositionFilter<NPC>(POSITON), new ActionFilter<NPC>(ACTION)); You can add as many filters as you want in the above expression. There are some other classes that inherit Filter and can be used but you can also create your own. Filter<NPC> levelFilter = new Filter<NPC>() { @Override public boolean match(NPC n) { return n.getLevel() < 30; } }; NPC monster = npcs.closest(levelFilter); Which will find only NPCs having a combat level under 30. 2. Using Java 8 streams NPC monster = npcs.getAll().stream().filter(n -> n.getName().equals(NAME) && AREA.contains(n) && n.getLevel() < 30).findFirst().get(); These offer more flexibility but you will have to check the Optional returned by findFirst() otherwise you will get a java.lang.NoSuchElementException, so I advise you to use the OSBot Filter. Edited May 2, 2016 by Token Quote Link to comment Share on other sites More sharing options...
Explv Posted May 2, 2016 Share Posted May 2, 2016 2. Using Java 8 streams NPC monster = npcs.getAll().stream().filter(n -> n.getName().equals(NAME) && AREA.contains(n) && n.getLevel() < 30).findFirst().get(); These offer more flexibility but you will have to check the Optional returned by findFirst() otherwise you will get a java.lang.NoSuchElementException, so I advise you to use the OSBot Filter. To avoid having to check the Optional etc. you could do: NPC monster = npcs.closest(n -> n.getName().equals(NAME) && AREA.contains(n) && n.getLevel() < 30); Quote Link to comment Share on other sites More sharing options...
Psvxe Posted May 2, 2016 Share Posted May 2, 2016 (edited) I get NPE´s to when doing log(method())try log(""+method()); Edited May 2, 2016 by Psvxe Quote Link to comment Share on other sites More sharing options...
Strange_Fk Posted May 2, 2016 Share Posted May 2, 2016 String npcName = "The npcs name" Entity anyEntityName = npcs.closest(new Filter<NPC>() { public boolean match(NPC npc) { return (npc.getName().equals(npcName) && !npc.isUnderAttack() && (npc.getCurrentHealth() != 0 || npc.getMaximumHealth() == 0 && npc.getHealthPercent() != 0)) && !npc.isInteracting(myPlayer()) && killZone.contains(npc.getPosition()) && npc.getInteracting() ==null); } }); anyEntityName.interact("Attack"); sloppy but will do, you can take out or add booleans to define npc as you please. I was toying with it a lot so this example might have a lot of pointless filters. Hope this helps regardless Quote Link to comment Share on other sites More sharing options...
Explv Posted May 3, 2016 Share Posted May 3, 2016 String npcName = "The npcs name" Entity anyEntityName = npcs.closest(new Filter<NPC>() { public boolean match(NPC npc) { return (npc.getName().equals(npcName) && !npc.isUnderAttack() && (npc.getCurrentHealth() != 0 || npc.getMaximumHealth() == 0 && npc.getHealthPercent() != 0)) && !npc.isInteracting(myPlayer()) && killZone.contains(npc.getPosition()) && npc.getInteracting() ==null); } }); anyEntityName.interact("Attack"); sloppy but will do, you can take out or add booleans to define npc as you please. I was toying with it a lot so this example might have a lot of pointless filters. Hope this helps regardless It does indeed have pointless filters, and you will also end up with NullPointerExceptions Quote Link to comment Share on other sites More sharing options...
Strange_Fk Posted May 4, 2016 Share Posted May 4, 2016 It does indeed have pointless filters, and you will also end up with NullPointerExceptions Yeah I forgot to add if (entity !=null) { entity.interact("Attack"); } Quote Link to comment Share on other sites More sharing options...