import org.osbot.rs07.api.filter.Filter;
import org.osbot.rs07.api.model.NPC;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
@ScriptManifest(info = "", name = "Chickens", logo = "", version = 1.00, author = "Foulwerp" )
public class Chickens extends Script {
public int onLoop() {
if (myPlayer().getInteracting() != null) {
//This check at the start saves resources, because you don't do useless things like find new npcs while in combat
return random(500, 1000);
}
NPC chicken = npcs.closest(new Filter<NPC>() {
public boolean match(NPC npc) {
//Multiple checks using a filter to find the best npc to attack. I guess, optimizing this to do the least
//resource intensive checks first, leading up the the most intensive. If one of the first checks returns
//false, you didn't waste a bunch of resources. If you check if the npc is reachable first you would waste a lot
//of resources on an A* algorithm on a mob you may not even want to attack. This is because you are polling
//all the mobs till one of these returns true to all checks.
return npc.getName().equals("Chicken") && npc.getHealth() > 0 && npc.isAttackable() && map.canReach(npc);
}
});
if (chicken == null) {
//If chicken is null just return so you wait for one to spawn
return random(500, 1000);
}
if (!chicken.isOnScreen() {
//Try to get the chicken on the screen by turning to it
camera.toEntity(chicken);
//Returning after doing an action
return (500, 1000)
}
//Attack the chicken then return net iteration should see that I'm interacting which is the first check
chicken.interact("Attack");
//Default return but again returned after I completed an action
return random(500, 1000);
}
}
I know how hard it can be to learn when you have no reference so I decided to give you one, and a few tips.
Once you learn how to use filters life will be a lot easier. You can use multiple checks to find the perfect npc to attack, and not just the closest one to you. Filters can be applied to not only npcs but items, ground items, etc.
Instead of checking if your player is moving or animating check if it is interacting with something. In most cases if it is your player is in combat.
Try to write your code logically so that you don't have to use sleep. If you return it's better and what you return is how long in milliseconds it waits till it enters the loop again. When your script runs through the loop you want it to do 0 to 1 action and return. So every loop iteration should either have no actions done meaning the default is returned, or do 1 action in which you return a specified amount. Not action sleep another action sleep then return.
In a sense returning is sleeping, but a much better way allowing the bot to do what it needs to before you enter the loop again. Randoms aren't a big deal on oldschool but returning allows the bot to check for them and do other background things, sleeping doesn't.