Jump to content

Trying to see if there is an npc in 1 tile radius around my player


Recommended Posts

Posted

Why does this not work? i am trying to see if there is an npc in 1 tile radius around my player.

Not getting any errors on compiling but it wont run. (npc id is not correct in this example but it is correct in my code)

	NPC goblin = npcs.closest(123);
	@Override
	public int onLoop(){
		if (myPlayer().getArea(1).contains(goblin)) {
			log("Goblin next to me");
		}
		return random(200, 300);
	}

 

Posted (edited)

Try looking into filters too:

Filter<NPC> goblinFilter = npc -> npc.getName().equalsIgnoreCase("Goblin") &&
                                        myPlayer().getArea(1).contains(npc) &&
                                        !npc.isHitBarVisible() &&
                                        npc.isAttackable() &&
                                        !npc.isUnderAttack() &&
                                        npc.isVisible();
  .
  .
  .
  .
  NPC goblin = getNpcs().closest(goblinFilter);

Using them will result in cleaner code.

EDIT:

Don't just copy and paste this, i just threw a couple of methods of the top of my head.

Edited by HunterRS
Posted
2 hours ago, HunterRS said:

Try looking into filters too:


Filter<NPC> goblinFilter = npc -> npc.getName().equalsIgnoreCase("Goblin") &&
                                        myPlayer().getArea(1).contains(npc) &&
                                        !npc.isHitBarVisible() &&
                                        npc.isAttackable() &&
                                        !npc.isUnderAttack() &&
                                        npc.isVisible();
  .
  .
  .
  .
  NPC goblin = getNpcs().closest(goblinFilter);

Using them will result in cleaner code.

EDIT:

Don't just copy and paste this, i just threw a couple of methods of the top of my head.

Filters looks super useful,  i was also wondering how to make something like this shorter and cleaner

if(getInventory().contains("Prayer potion(4)") && getInventoy().contains("Prayer potion(3)")) 

Would be nice to know for the future as many things has x amount of charges or doses etc

Posted
2 minutes ago, Darek855 said:

Filters looks super useful,  i was also wondering how to make something like this shorter and cleaner


if(getInventory().contains("Prayer potion(4)") && getInventoy().contains("Prayer potion(3)")) 

Would be nice to know for the future as many things has x amount of charges or doses etc

Filter<Item> prayerPotFilter = item -> item.getName().contains("Prayer potion(");
  .
  .
  .
  .
  if (getInventory().contains(prayerPotFilter)) {
  	log("We have a prayer potion");
  }

This should work, play around with filters and the API, super usefull stuff.

Posted
38 minutes ago, HunterRS said:

Filter<Item> prayerPotFilter = item -> item.getName().contains("Prayer potion(");
  .
  .
  .
  .
  if (getInventory().contains(prayerPotFilter)) {
  	log("We have a prayer potion");
  }

This should work, play around with filters and the API, super usefull stuff.

Right you can use filters for more than just npcs

My next bot that i write will be so much more compact and clean. recently wrote a bot that kills 1 creature over and over and banks, and that is 1090 rows...

of course it includes tons of checks so it never dies or gets stuck also tons of anti-pattern things even tho i am not sure how much that helps with banrates. wont hurt the GP hour so whatever.

anyways, thanks for the tips!

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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