Chazler Posted February 22, 2015 Share Posted February 22, 2015 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 Quote Link to comment Share on other sites More sharing options...
Isolate Posted February 22, 2015 Share Posted February 22, 2015 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 4 Quote Link to comment Share on other sites More sharing options...
Khaleesi Posted February 22, 2015 Share Posted February 22, 2015 (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 Edited February 22, 2015 by Khaleesi 3 Quote Link to comment Share on other sites More sharing options...
Chazler Posted February 22, 2015 Author Share Posted February 22, 2015 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 Thanks to the both of you, it really helped me out a bunch! 1 Quote Link to comment Share on other sites More sharing options...
organicjello Posted February 28, 2015 Share Posted February 28, 2015 (edited) 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? Edited February 28, 2015 by organicjello Quote Link to comment Share on other sites More sharing options...
Twin Posted February 28, 2015 Share Posted February 28, 2015 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 Quote Link to comment Share on other sites More sharing options...
Soldtodie Posted February 28, 2015 Share Posted February 28, 2015 Check it with isInteracting or getInteracting but for both for your cow and for every player if they want to attack your cow. Quote Link to comment Share on other sites More sharing options...