Jump to content

Attack npc that is not under attack.


Recommended Posts

Posted

So I'm trying to build my own cow killing script with bank support to familiarize myself with java and scripting.

Currently I use this to attack cows: 

case KILL:
    	Player player = myPlayer();
    	NPC cow = npcs.closest("Cow");
    	if ((!player.isMoving()) && (!player.isUnderAttack()) && (!inventory.isFull()) && (cow != null)) {
    		cow.interact("Attack");
    	}

And it works, the player won't try to attack anohter cow whilest it is fighting a cow yet will try to attack cows that are in combat with another player.

I've tried to look for an answer in the api but I'm probably too obvlious to find a solution.

 

If anyone could help me specify to attack only cows that are not under attack that would be brilliant!

 

Cheers, 

Chazler

Posted

So I'm trying to build my own cow killing script with bank support to familiarize myself with java and scripting.

Currently I use this to attack cows: 

case KILL:
    	Player player = myPlayer();
    	NPC cow = npcs.closest("Cow");
    	if ((!player.isMoving()) && (!player.isUnderAttack()) && (!inventory.isFull()) && (cow != null)) {
    		cow.interact("Attack");
    	}

And it works, the player won't try to attack anohter cow whilest it is fighting a cow yet will try to attack cows that are in combat with another player.

I've tried to look for an answer in the api but I'm probably too obvlious to find a solution.

 

If anyone could help me specify to attack only cows that are not under attack that would be brilliant!

 

Cheers, 

Chazler

Use of a filter would be appropriate here

A filter is a way of it only accepting a entity if it meets all the criteria.

Currently your only criteria is for the entity to be named "Cow" which,

even with the checks underneath will still find any cow at all.

The only reason you dont attack one whilst fighting is because of some of the checks underneath.

Your check in filter form would look like:

 

NPC cow = npcs.closest(new Filter<NPC>() {
                                    @Override
                                    public boolean match(NPC npc) {
                                        return npc != null && npc.getName().equals("Cow");
                                    }
                                });

Which checks would return the same but you only want to find a cow not in combat so you'd need to add more checks

 

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

This check checks for the name, and would allow inclusion of the little cows but excludes dairy cows

after it has identified the fact its a cow it moves on to check:

Is the npc under attack?

is the npc able to be attacked by your player?

is its health > 0? (avoid clicking on cows which are dying)

you can add as many checks to this as you like.

Sorry if my explanation is bad

  • Like 4
Posted (edited)

So I'm trying to build my own cow killing script with bank support to familiarize myself with java and scripting.

Currently I use this to attack cows: 

case KILL:
    	Player player = myPlayer();
    	NPC cow = npcs.closest("Cow");
    	if ((!player.isMoving()) && (!player.isUnderAttack()) && (!inventory.isFull()) && (cow != null)) {
    		cow.interact("Attack");
    	}

And it works, the player won't try to attack anohter cow whilest it is fighting a cow yet will try to attack cows that are in combat with another player.

I've tried to look for an answer in the api but I'm probably too obvlious to find a solution.

 

If anyone could help me specify to attack only cows that are not under attack that would be brilliant!

 

Cheers, 

Chazler

 

 add -> !cow.isUnderAttack()

 

You should be useing a Filter instead, this code willc ause the script to pause it the closest cow in underattack.

 

use this:

NPC cow = script.npcs.closest(new Filter<NPC>() {

	@Override
	public boolean match(NPC npc) {
	return npc != null && (npc.getName().equals("Cow") || pc.getName().equals("Cow calf")) && !npc.isUnderAttack() && npc.getHealth() > 0;
	}
});

You could also check death animation from the cow are check if the cow is over 0 health so it prevents clicking deaths cows.

 

Goodluck smile.png

Edited by Khaleesi
  • Like 3
Posted

 

Sorry if my explanation is bad

 

Not at all!

 

 add -> !cow.isUnderAttack()

 

You should be useing a Filter instead, this code willc ause the script to pause it the closest cow in underattack.

 

use this:

NPC cow = script.npcs.closest(new Filter<NPC>() {

	@Override
	public boolean match(NPC npc) {
	return npc != null && (npc.getName().equals("Cow") || pc.getName().equals("Cow calf")) && !npc.isUnderAttack() && npc.getHealth() > 0;
	}
});

You could also check death animation from the cow are check if the cow is over 0 health so it prevents clicking deaths cows.

 

Goodluck smile.png

 

Thanks to the both of you, it really helped me out a bunch!

  • Like 1
Posted

Is there a better check than isUnderAttack? I use it, but occasionally, it returns false even when an NPC is under attack. Does getAnimation work better even when between the breaks between attack animations?

 

You could maybe get the health of the NPC instead? Like

cowHP= NPC.getHealth("Cow");

if(cowHP<8)

return State.ATTACK; or something like that.

not 100% sure if thats how you do it but something like that might work

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