Jump to content

how and when to use filters with npc's


roguehippo

Recommended Posts

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"};
 
}

 

Link to comment
Share on other sites

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 by Bradf3rd
Link to comment
Share on other sites

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 by Token
Link to comment
Share on other sites

 

 

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);
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...