Jump to content

Checking distance between an NPC and GroundItem?


Annointed

Recommended Posts

Hey guys, I'm trying to check the distance between an NPC and GroundItem, so that whichever one is closer, I'll either attack it or loot it. How would I go upon doing this?

 

Also, what's the most efficient and fastest way of attacking an NPC?

 

Thanks!

 

EDIT: I think I figured it out:

if (myPlayer().getPosition().distance(npc) < myPlayer()
                        .getPosition().distance(grounditem))

Thanks anyways!

Edited by Annointed
Link to comment
Share on other sites

Hey guys, I'm trying to check the distance between an NPC and GroundItem, so that whichever one is closer, I'll either attack it or loot it. How would I go upon doing this?

 

Also, what's the most efficient and fastest way of attacking an NPC?

 

Thanks!

 

You can get the real (walking) distance to the npc / ground item using:

int distance = getMap().realDistance(entity); // entity is the npc or ground item

Or the direct distance:

int distance = myPosition().distance(entity.getPosition());

Then just compare the two distances to find which is less.

 

For attacking the NPC you can just do something like:

NPC cow = getNpcs().closest(npc -> npc.getName().equals("Cow") && npc.isAttackable());

if (cow != null && cow.interact("Attack")) {
   new ConditionalSleep(5000) {
       @ Override
       public boolean condition() throws InterruptedException {
           return !cow.isAttackable();
       }
   }.sleep();
}
Edited by Explv
Link to comment
Share on other sites

 

You can get the real (walking) distance to the npc / ground item using:

int distance = getMap().realDistance(entity); // entity is the npc or ground item

Or the direct distance:

int distance = myPosition().distance(entity.getPosition());

Then just compare the two distances to find which is less.

 

For attacking the NPC you can just do something like:

NPC cow = getNpcs().closest(npc -> npc.getName().equals("Cow") && npc.isAttackable());

if (cow != null && cow.interact("Attack")) {
   new ConditionalSleep(5000) {
       @ Override
       public boolean condition() throws InterruptedException {
           return !cow.isAttackable();
       }
   }.sleep();
}

 

Thank you! Also, what's the difference between the normal sleep method, and ConditionalSleep? When would I use which?

Link to comment
Share on other sites

Thank you! Also, what's the difference between the normal sleep method, and ConditionalSleep? When would I use which?

 

ConditionalSleep will sleep for MAX the amount of time you specify (in this case 5 seconds) OR until the condition is satisfied.

 

So in the example I gave it would sleep for up to 5 seconds or until the NPC is no longer attackable (either it is dead, someone else is attacking it, or we are attacking it)

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...