Jump to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

how and when to use filters with npc's

Featured Replies

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

 

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

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

 

 

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);

I get NPE´s to when doing

log(method())
try

log(""+method());

Edited by Psvxe

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

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

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

}

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.