dontbuzz Posted August 4, 2016 Share Posted August 4, 2016 (edited) Am learning to script, this is my first script. I thought this line would stop my player from attacking another chicken that is already in combat but it still seems to sometimes. (and not just when another player beats me to the chicken). if (chicken != null && chicken.isVisible() && !chicken.isUnderAttack() && chicken.getHealthPercent() > 0 && map.canReach(chicken)) { Edit: Script sometimes tries to switch chickens mid combat, anyone know why that could be aswell? plz halp. Full Code: import org.osbot.rs07.api.Combat; import org.osbot.rs07.api.filter.Filter; import org.osbot.rs07.api.map.Area; import org.osbot.rs07.api.model.Entity; import org.osbot.rs07.api.model.NPC; import org.osbot.rs07.api.model.RS2Object; import org.osbot.rs07.script.Script; import org.osbot.rs07.script.ScriptManifest; import org.osbot.rs07.utility.ConditionalSleep; import java.awt.*; @ScriptManifest(name = "dontbuzz Fried Chicken", author = "dontbuzz", version = 1.0, info = "Kills chickens in Lumbridge", logo = "") public class ChickenKiller extends Script { Area chickenArea = new Area(3183, 3290, 3173, 3306); @[member='Override'] public void onStart() { log("Welcome to dontbuzz Chicken Killer"); } @[member='Override'] public void onExit() { //Code here will execute after the script ends } @[member='Override'] public int onLoop() throws InterruptedException { checkArea(); attack(); dropJunk(); return random(600, 4000); } // The amount of time in milliseconds before the loop starts over @[member='Override'] public void onPaint(Graphics2D g) { //This is where you will put your code for paint(s) } public void attack() { if (!myPlayer().isUnderAttack() && !myPlayer().isMoving() && !getCombat().isFighting()) { NPC chicken = npcs.closest("Chicken"); if (chicken != null && chicken.isVisible() && !chicken.isUnderAttack() && chicken.getHealthPercent() > 0 && map.canReach(chicken)) { chicken.interact("Attack"); new ConditionalSleep(2000) { // Sleep until the player is out of combat, or for 2 seconds @[member='Override'] public boolean condition() throws InterruptedException { return !myPlayer().isUnderAttack() && !myPlayer().isMoving() && !getCombat().isFighting(); } }.sleep(); } } } public void checkArea() { NPC chicken = npcs.closest(chickenArea, "Chicken"); if (!chickenArea.contains(myPlayer()) && !map.canReach(chicken)) { RS2Object chickenGate = objects.closest(1558, 1560); chickenGate.interact("Open"); new ConditionalSleep(3000) { // Sleep until the player is out of combat, or for 5 seconds @[member='Override'] public boolean condition() throws InterruptedException { return map.canReach(chicken); } }.sleep(); getWalking().webWalk(chickenArea); } } public void dropJunk() { if (!inventory.isEmpty()) { inventory.dropAll(); } } } Edited August 4, 2016 by dontbuzz Quote Link to comment Share on other sites More sharing options...
Chris Posted August 4, 2016 Share Posted August 4, 2016 (edited) Am learning to script, this is my first script. I thought this line would stop my player from attacking another chicken that is already in combat but it still seems to sometimes. (and not just when another player beats me to the chicken). if (chicken != null && chicken.isVisible() && !chicken.isUnderAttack() && chicken.getHealthPercent() > 0 && map.canReach(chicken)) { Edit: Script sometimes tries to switch chickens mid combat, anyone know why that could be aswell? plz halp. try this fam Edited August 4, 2016 by Christopher 1 Quote Link to comment Share on other sites More sharing options...
Khaleesi Posted August 4, 2016 Share Posted August 4, 2016 Use a filter to get a proper chicken: NPC chicken = getNpcs().closest(new Filter<NPC>() { @[member=Override] public boolean match(NPC npc) { return npc != null && npc.getName().equals("Chicken") && !npc.isUnderAttack() && npc.getHealthPercent() > 0; } }); 1 Quote Link to comment Share on other sites More sharing options...
Prolax Posted August 4, 2016 Share Posted August 4, 2016 (edited) Is using methods a better idea then using states in your onLoop method? Is it a good idea to use a static value (2000 ms) for a conditional sleep? Or better to use rand(1500,2500)? Edited August 4, 2016 by Prolax Quote Link to comment Share on other sites More sharing options...
Explv Posted August 4, 2016 Share Posted August 4, 2016 (edited) Am learning to script, this is my first script. I thought this line would stop my player from attacking another chicken that is already in combat but it still seems to sometimes. (and not just when another player beats me to the chicken). Edit: Script sometimes tries to switch chickens mid combat, anyone know why that could be aswell? plz halp. Instead of checking if your player is under attack (the chicken might not be fighting back), instead you can check if your player is interacting with a Chicken. Also, in your ConditionalSleep, considering it takes time for your player to walk to the chicken, and another player might attack it first, you should check in your sleep condition if the chicken is under attack, or dead etc. Consider this short example (untested): import org.osbot.rs07.api.filter.NameFilter; import org.osbot.rs07.api.model.NPC; import org.osbot.rs07.script.Script; import org.osbot.rs07.script.ScriptManifest; import org.osbot.rs07.utility.ConditionalSleep; @ScriptManifest(name = "Chicken Killer", info = "Kills Chickens...", author = "Explv", version = 0.1, logo = "") public final class ChickenKiller extends Script { private NPC currentChicken; @[member='Override'] public final int onLoop() throws InterruptedException { if(!isAttackingChicken()) attackChicken(); return random(150, 200); } private boolean isAttackingChicken() { return currentChicken != null && currentChicken.exists() && currentChicken.getHealthPercent() > 0 && myPlayer().isInteracting(currentChicken) && currentChicken.isUnderAttack(); } private void attackChicken() { currentChicken = getClosestChicken(); if(currentChicken != null && currentChicken.interact("Attack")) { new ConditionalSleep(5000) { @[member='Override'] public boolean condition() throws InterruptedException { return myPlayer().isInteracting(currentChicken) || !isAttackableTarget(currentChicken); } }.sleep(); } } private NPC getClosestChicken() { return getNpcs().closest( new NameFilter<>("Chicken"), this::isAttackableTarget ); } private boolean isAttackableTarget(final NPC target) { return target != null && target.exists() && target.getHealthPercent() > 0 && !target.isUnderAttack(); } } Edited August 4, 2016 by Explv Quote Link to comment Share on other sites More sharing options...