Mkay lets start with what you have now.
npcs.closest("Cow").interact("Attack");
If there is no cow this will throw a NullPointerException. So what you want to do is NPC cow = ... if(cow != null) cow.interact.... But you're not checking if this particular cow is already under attack, dead or even reachable at all. And what if you're already attacking a cow? I suggest you first write that part of the script, attacking a cow and waiting while in combat, then add looting and finally banking. For banking, use bank.open() and never use a while loop like that. If your current state is banking, you could check the distance to the closest "Stairs". If you want to bank and there is a stairs then "Climb-up" if myPosition().getZ() < 2 else bank.open()