Jump to content

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


Darek855
 Share

Recommended Posts

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

 

Link to comment
Share on other sites

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

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

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.

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.

 Share

  • Recently Browsing   0 members

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