Jump to content

Fast Question


Snowydell

Recommended Posts

Here's the code

 

NPC cow = npcs.closest("Cow", "Cow calf");

 

if(!inventory.isFull() && COW_AREA.contains(myPlayer()) && !myPlayer().isUnderAttack()  && !cow.isUnderAttack() && cow != null)

{

cow.interact("Attack");

}

 

Now basically what happens is it will target the closest cow even if it's in combat so it will just wait until the cow is dead to target a new cow. How would I make it do something like: npc.closest("Cow", "Cow calf").!isUnderAttack

 

Sorry for all the questions, but this script i'm working on i'll put in the local script section so hopefully if it works

Edited by Snowydell
Link to comment
Share on other sites

 

Here's the code
 
NPC cow = npcs.closest("Cow", "Cow calf");
 
if(!inventory.isFull() && COW_AREA.contains(myPlayer()) && !myPlayer().isUnderAttack()  && !cow.isUnderAttack() && cow != null)
{
cow.interact("Attack");
}
 
Now basically what happens is it will target the closest cow even if it's in combat so it will just wait until the cow is dead to target a new cow. How would I make it do something like: npc.closest("Cow", "Cow calf").!isUnderAttack
 
Sorry for all the questions, but this script i'm working on i'll put in the local script section so hopefully if it works

 

you could always try

 NPC cow = npcs.closest(new Filter<NPC>() {
             @Override
             public boolean match(NPC npc) {
                 return npc.getName().contains("Cow") && !npc.getName().contains("Dairy") && npc != null
                         && npc.getHealth() >0 && npc.isAttackable() && !npc.isUnderAttack();
             }
         });
if(!inventory.isFull() && COW_AREA.contains(myPlayer()) && !myPlayer().isUnderAttack() && cow != null)
{
cow.interact("Attack");
}

 
 
Edited by Isolate
  • Like 2
Link to comment
Share on other sites

why are you checking for if the pointer is null after your already using the instance

 

 

Because I like to be sure wink.png long time no doge

 

He means why are you doing this:

npc.getName().contains("Cow") && !npc.getName().contains("Dairy") && npc != null

null checking npc after you call npc.getName is pointless. If there is an NPE you will get it on npc.getName, and thus null checking after you call a method from that object is pointless. The NPE will not be avoided at all.

Link to comment
Share on other sites

He means why are you doing this:

npc.getName().contains("Cow") && !npc.getName().contains("Dairy") && npc != null

null checking npc after you call npc.getName is pointless. If there is an NPE you will get it on npc.getName, and thus null checking after you call a method from that object is pointless. The NPE will not be avoided at all.

move it to the start then.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

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