Jump to content

Method to define closest NPC that isn't in combat?


Recommended Posts

Posted (edited)

Probably super simple I'm just not sure. Been a while since I've been on here. Also my script keeps getting stuck saying there are arrows to be picked up but there aren't. Any ideas why?

 

73ef1fc6c7bdf3b5072d822f2752bf53.png

	public boolean arrowsPresent() {
		if (getGroundItems().closest(arrowType) != null) {
			return true;
		}
		return false;
	}
Edited by Paradox68
Posted (edited)

Probably super simple I'm just not sure. Been a while since I've been on here.

I would use a filter like this:

Filter<NPC> monsterFilter = new Filter<NPC>() {
    @Override
    public boolean match(NPC n) {
        if (n.getId() != monsterID)
            return false;
        if(!n.getName().equals(monsterName))
            return false;
        if(n.isUnderAttack())
            return false;
        return true;
    }
};

then you can just say: 

NPC monster = getNpcs().closest(monsterFilter);

Did that help?

Edited by AresScripts
Posted (edited)

I would use a filter like this:

Filter<NPC> monsterFilter = new Filter<NPC>() {
    @Override
    public boolean match(NPC n) {
        if (n.getId() != monsterID)
            return false;
        if(!n.getName().equals(monsterName))
            return false;
        if(n.isUnderAttack())
            return false;
        return true;
    }
};

then you can just say: 

NPC monster = getNpcs().closest(monsterFilter);

Did that help?

 

 

A lot actually, thanks dude. Not to be mr. stupid but what's this about?

9c66c2a47fc882b785b2994d47763f00.png

Edited by Paradox68
Posted (edited)

I would use a filter like this:

Filter<NPC> monsterFilter = new Filter<NPC>() {
    @Override
    public boolean match(NPC n) {
        if (n.getId() != monsterID)
            return false;
        if(!n.getName().equals(monsterName))
            return false;
        if(n.isUnderAttack())
            return false;
        return true;
    }
};

then you can just say: 

NPC monster = getNpcs().closest(monsterFilter);

Did that help?

 

 

Or you could just do:

NPC monster = getNpcs().closest(npc -> npc.getName().equals("Cow") && !npc.isUnderAttack());

A lot actually, thanks dude. Not to be mr. stupid but what's this about?

9c66c2a47fc882b785b2994d47763f00.png

 

Just suppress it, the warning is for an operation that is potentially unsafe, but in this case we know it is fine.

@SuppressWarnings("unchecked") 
NPC monster = getNpcs().closest(npc -> npc.getName().equals("Cow") && !npc.isUnderAttack());

Also with regards to your arrow method, it can be simplified to:

public boolean arrowsPresent() {
    
    return getGroundItems().closest(arrowType) != null;
}

And it should work fine, it is probably an issue somewhere else in your code.

Edited by Explv
  • Like 1
Posted

Or you could just do:

NPC monster = getNpcs().closest(npc -> npc.getName().equals("Cow") && !npc.isUnderAttack());

Just suppress it, the warning is for an operation that is potentially unsafe, but in this case we know it is fine.

@SuppressWarnings("unchecked") 
NPC monster = getNpcs().closest(npc -> npc.getName().equals("Cow") && !npc.isUnderAttack());

Also with regards to your arrow method, it can be simplified to:

public boolean arrowsPresent() {
    
    return getGroundItems().closest(arrowType) != null;
}

And it should work fine, it is probably an issue somewhere else in your code.

Ah. I need to study up on lambda expressions :o

Posted (edited)

Ah. I need to study up on lambda expressions ohmy.png

 

 

I found your Filter to be written a little oddly as well tongue.png

 

Why would you do:

Filter<NPC> monsterFilter = new Filter<NPC>() {
    @Override
    public boolean match(NPC n) {
        if (n.getId() != monsterID)
            return false;
        if(!n.getName().equals(monsterName))
            return false;
        if(n.isUnderAttack())
            return false;
        return true;
    }
};

 when you could just do:

Filter<NPC> monsterFilter = new Filter<NPC>() {
    @Override
    public boolean match(NPC n) {
        return n.getName().equals("Cow") && !n.isUnderAttack();
    }
};

But yeah, lambdas make everything more simple anyway ^_^

Edited by Explv
Posted

I found your Filter to be written a little oddly as well tongue.png

 

Why would you do:

Filter<NPC> monsterFilter = new Filter<NPC>() {
    @Override
    public boolean match(NPC n) {
        if (n.getId() != monsterID)
            return false;
        if(!n.getName().equals(monsterName))
            return false;
        if(n.isUnderAttack())
            return false;
        return true;
    }
};

 when you could just do:

Filter<NPC> monsterFilter = new Filter<NPC>() {
    @Override
    public boolean match(NPC n) {
        return n.getName().equals("Cow") && !n.isUnderAttack();
    }
};

But yeah, lambdas make everything more simple anyway happy.png

Even though that takes up less lines of code, I like to only do one "filter" per if statement because its easier to debug for me.

  • Like 1

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