Jump to content

Attack npc that is not under attack.


Chazler

Recommended Posts

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

 

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
Link to comment
Share on other sites

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

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