Annointed Posted October 30, 2016 Share Posted October 30, 2016 (edited) 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 October 30, 2016 by Annointed Quote Link to comment Share on other sites More sharing options...
Explv Posted October 30, 2016 Share Posted October 30, 2016 (edited) 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 October 30, 2016 by Explv Quote Link to comment Share on other sites More sharing options...
Annointed Posted October 30, 2016 Author Share Posted October 30, 2016 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? Quote Link to comment Share on other sites More sharing options...
Explv Posted October 30, 2016 Share Posted October 30, 2016 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) Quote Link to comment Share on other sites More sharing options...