Zoubiey Posted May 14, 2015 Share Posted May 14, 2015 (edited) Hey, kinda new to this bot but thought I could give scripting a try. Started with a chicken killer and it works pretty good, some minor tweaks for attacking and finding "free" chickens is still needed tho. My biggest issue is that it doesn't loot at all and also clicks the chicken while attacking, can also click another chicken even if the first one isnt dead yet, been searching a few threads for some answers and stuff I could try to fix it but with no success. My code so far. private enum State { PICK_UP, KILL, WAIT }; private State getState() { GroundItem feathers = groundItems.closest("Feathers"); if ((feathers !=null && !myPlayer().isUnderAttack())) return State.PICK_UP; if (!inventory.isFull()) return State.KILL; return State.WAIT; } @Override public int onLoop() throws InterruptedException { switch (getState()) { case PICK_UP: GroundItem feathers = groundItems.closest("Feathers"); if ((!inventory.isFull() && !myPlayer().isUnderAttack())) { feathers.interact("Take"); wait(100,300); } break; case KILL: Player player = myPlayer(); NPC chicken = npcs.closest("Chicken"); if((!player.isMoving()) && (!player.isUnderAttack()) && (chicken.isAttackable()) && (chicken.isVisible()) && (chicken.getHealth()>0) && (chicken.getName().contains("Chicken")) && (chicken != null)) { chicken.interact("Attack"); sleep(random(200, 350)); } else { camera.toEntity(chicken); } break; case WAIT: sleep(random(500, 700)); break; } return random(200,300); //The amount of time in milliseconds before the loop starts over } What I'm trying to do is a chicken killer which loots feathers from only those I kill. That's my first goal, will be going for bury bones and such when I get this to work. So please tell me if you see something that's need to be changed/removed/added. Zoubiey Edited May 14, 2015 by Zoubiey Quote Link to comment Share on other sites More sharing options...
FrostBug Posted May 14, 2015 Share Posted May 14, 2015 wait(100,300); ^ this will sleep the current thread until notified by another thread, or until 100 milliseconds and 300 nanoseconds has passed. I don't think that's what you want, so use sleep instead. (!player.isMoving()) && (!player.isUnderAttack()) && (chicken.isAttackable()) && (chicken.isVisible()) && (chicken.getHealth()>0) && (chicken.getName().contains("Chicken")) && (chicken != null) First of all, you should probably make your own filter to supply to the 'closest' method if you're gonna have this many checks. Second; having a nullcheck that late is pointless, the NullPointerException would have already been thrown at this point if there were no chickens. You may want to check whether your player is interacting with a chicken, rather than whether the chicken is attacking you (player isUnderAttack), since it takes a while for a chicken to retaliate, and the player may start attacking another chicken in that time. Furthermore, there's no need to check if the chickens name contains chicken. It would not have been found by the 'closest' method with String arguments in the first place if it did not. Quote Link to comment Share on other sites More sharing options...
Zoubiey Posted May 14, 2015 Author Share Posted May 14, 2015 wait(100,300); ^ this will sleep the current thread until notified by another thread, or until 100 milliseconds and 300 nanoseconds has passed. I don't think that's what you want, so use sleep instead. (!player.isMoving()) && (!player.isUnderAttack()) && (chicken.isAttackable()) && (chicken.isVisible()) && (chicken.getHealth()>0) && (chicken.getName().contains("Chicken")) && (chicken != null) First of all, you should probably make your own filter to supply to the 'closest' method if you're gonna have this many checks. Second; having a nullcheck that late is pointless, the NullPointerException would have already been thrown at this point if there were no chickens. You may want to check whether your player is interacting with a chicken, rather than whether the chicken is attacking you (player isUnderAttack), since it takes a while for a chicken to retaliate, and the player may start attacking another chicken in that time. Furthermore, there's no need to check if the chickens name contains chicken. It would not have been found by the 'closest' method with String arguments in the first place if it did not. Thanks, gonna try everything you just said! Quote Link to comment Share on other sites More sharing options...
Zoubiey Posted May 14, 2015 Author Share Posted May 14, 2015 wait(100,300); ^ this will sleep the current thread until notified by another thread, or until 100 milliseconds and 300 nanoseconds has passed. I don't think that's what you want, so use sleep instead. (!player.isMoving()) && (!player.isUnderAttack()) && (chicken.isAttackable()) && (chicken.isVisible()) && (chicken.getHealth()>0) && (chicken.getName().contains("Chicken")) && (chicken != null) First of all, you should probably make your own filter to supply to the 'closest' method if you're gonna have this many checks. Second; having a nullcheck that late is pointless, the NullPointerException would have already been thrown at this point if there were no chickens. You may want to check whether your player is interacting with a chicken, rather than whether the chicken is attacking you (player isUnderAttack), since it takes a while for a chicken to retaliate, and the player may start attacking another chicken in that time. Furthermore, there's no need to check if the chickens name contains chicken. It would not have been found by the 'closest' method with String arguments in the first place if it did not. Changed some stuff but still acts a bit wierd. From time to time it's clicking works great but sometimes it starts attacking another chicken while already in a fight. I think I used the right check, but can be wrong. (!player.isInteracting(chicken)) Also the looting part doesn't work at all, not sure what the problem is on that part. Mind explaining how the filter works? Sounds interesting. Quote Link to comment Share on other sites More sharing options...
FrostBug Posted May 14, 2015 Share Posted May 14, 2015 Changed some stuff but still acts a bit wierd. From time to time it's clicking works great but sometimes it starts attacking another chicken while already in a fight. I think I used the right check, but can be wrong. (!player.isInteracting(chicken)) Also the looting part doesn't work at all, not sure what the problem is on that part. Mind explaining how the filter works? Sounds interesting. That's not really it, that expression only evaluates to true if you are not attacking a specific chicken. What I reckon you want to do is check whether you're attacking ANY chicken like so: if(myPlayer().getInteracting() == null) { //If not attacking/interacting with anything //Attack code } A filter can be used by creating a class that implements the Filter<NPC> interface. The interface has 1 method that will let you filter an NPC by whatever conditions you see fit. You can then create an instance of this filter of yours and pass to getNpcs().closest(yourFilter) Also, your pickup doesn't work becuase the item is called "Feather" and not "Feathers" Quote Link to comment Share on other sites More sharing options...
Zoubiey Posted May 14, 2015 Author Share Posted May 14, 2015 That's not really it, that expression only evaluates to true if you are not attacking a specific chicken. What I reckon you want to do is check whether you're attacking ANY chicken like so: if(myPlayer().getInteracting() == null) { //If not attacking/interacting with anything //Attack code } A filter can be used by creating a class that implements the Filter<NPC> interface. The interface has 1 method that will let you filter an NPC by whatever conditions you see fit. You can then create an instance of this filter of yours and pass to getNpcs().closest(yourFilter) Also, your pickup doesn't work becuase the item is called "Feather" and not "Feathers" The combat is working great now! Gonna see now if I can get the looting to be as I want it now. Thanks for your help! Quote Link to comment Share on other sites More sharing options...