I thought it would be a good exercise to create a task based script that kills and loots chaos druids. However, my attack task seems to always be bugging out on me. Occasionally ending the conditional sleep when it is not complete. The script is overall me learning some java and programming through many of the discussion posts on here.
package tasks;
import org.osbot.rs07.api.model.NPC;
import org.osbot.rs07.script.MethodProvider;
import utils.Sleep;
import utils.Vals;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import static org.osbot.rs07.script.MethodProvider.random;
public class Attack extends Task {
public Attack(MethodProvider api) {
super(api);
}
NPC currentTarget;
@Override
public boolean canProcess() {
return Vals.combatReady && !Vals.inCombat;
}
@Override
public void process() throws InterruptedException {
Vals.combatReady = false;
// Check if any npc is targeting me
List<NPC> druidsFocusingMe = api.npcs.getAll().stream()
.filter(npc -> api.myPlayer().equals(npc.getInteracting()))
.filter(npc -> npc.getName().equals("Chaos druid"))
.sorted(Comparator.comparingInt(npc -> api.getMap().realDistance(npc)))
.collect(Collectors.toList());
if (!druidsFocusingMe.isEmpty()){
//There is a druid targeting me
currentTarget = druidsFocusingMe.get(0);
api.log("COMBAT STATE 1: NPC is attempting to attack");
Sleep.sleepUntil(() -> !api.myPlayer().isUnderAttack(), 3000);
api.sleep(random(500,1000));
// If the druid could not reach me go attack it
if (!api.getCombat().isFighting() && !((api.myPlayer().isInteracting(currentTarget) && currentTarget.isInteracting(api.myPlayer())))) {
if (currentTarget.interact("Attack")){
api.log("NPC stuck, attacking NPC targetting me");
Sleep.sleepUntil(() -> (api.myPlayer().isInteracting(currentTarget) && currentTarget.isInteracting(api.myPlayer())), 5000);
if (api.getCombat().isFighting()){
api.log("(1) Attack success, in combat");
Vals.combatReady = false;
Vals.inCombat = true;
}
//AN error has occured
else{
api.log("(1) Was unable to get into combat, retrying");
api.sleep(random(1000,2000));
Vals.combatReady = true;
}
}
}
else {
api.log("(0) Attack success, in combat");
Vals.inCombat = true;
Vals.combatReady = false;
}
}
//Else if still not in combat
else if (!Vals.inCombat) {
NPC chaosDruid = api.getNpcs().closest(Vals.CHAOS_DRUID);
api.log("COMBAT STATE2: Attacking nearest NPC");
currentTarget = chaosDruid;
if (chaosDruid.interact("Attack")){
Sleep.sleepUntil(() -> (api.myPlayer().isInteracting(chaosDruid) && chaosDruid.isInteracting(api.myPlayer())), 5000);
if (api.getCombat().isFighting()) {
api.log("(2) Attack success, in combat");
Vals.inCombat = true;
Vals.combatReady = false;
}
//An error has occured
else{
api.log("(2) Was unable to get into combat, retrying");
api.sleep(random(1000,2000));
Vals.combatReady = true;
}
}
}
if (Vals.inCombat){
Sleep.sleepUntil(() -> (!api.myPlayer().isUnderAttack() && !api.getCombat().isFighting() && !api.myPlayer().isAnimating() && currentTarget.getHealthPercent()==0 && !currentTarget.exists() && currentTarget == null), 5000);
api.sleep(random(250,550));
api.log("No longer in combat");
Vals.inCombat = false;
Vals.readyToLoot = true;
}
}
}
I was too lazy to start a GUI so all of my error checking is done through the terminal/logger. I would love some feedback, I can also post the rest of the code if anyone is interested, but it is fairly basic banking and looting.