Hey I'm making my second script which basically just farms cows for xp. I've been using static sleeps everywhere and was wondering how to implement conditional sleeps? I've tried checking the API but for whatever reason it doesn't let me access it saying there's a server error.
Was wondering if someone could give me a quick example and some sample code on how to implement them?
My code is here if anyone wants to have a look:
import org.osbot.rs07.api.filter.Filter;
import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.model.NPC;
import org.osbot.rs07.api.ui.Skill;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import java.awt.*;
@ScriptManifest(name = "Cow Killer", author = "Tank", version = 1.0, info = "", logo = "")
public class TanksCowKiller extends Script {
private final Area cowField = new Area(3043,3313,3021,3298);
private long startTime;
private enum State {
ATTACK, WAIT
};
private State getState(){
if(myPlayer().isMoving() || combat.isFighting() || myPlayer().isUnderAttack())
return State.WAIT;
else
return State.ATTACK;
}
@SuppressWarnings("unchecked")
private NPC getTarget(){
NPC targetCow = npcs.closest(new Filter<NPC>(){
public boolean match(NPC theCow){
return theCow != null && (theCow.getName().equals("Cow") || theCow.getName().equals("Cow calf")) &&
theCow.isAttackable() && (theCow.getHealth() != 0) && cowField.contains(theCow);
}
});
return targetCow;
}
@[member=Override]
public void onStart() {
startTime = System.currentTimeMillis();
for(final Skill skill : new Skill[]{Skill.ATTACK, Skill.STRENGTH, Skill.DEFENCE, Skill.HITPOINTS}) {
getExperienceTracker().start(skill);
}
}
@[member=Override]
public int onLoop() throws InterruptedException{
switch(getState()) {
case ATTACK:
NPC cowToKill = getTarget();
if(cowToKill != null && !cowToKill.isOnScreen())
camera.toEntity(cowToKill);
cowToKill.interact("Attack");
sleep(random(600,1000));
while(cowToKill.isInteracting(myPlayer())) {
sleep(random(1500,3000));
}
break;
case WAIT:
sleep(random(400,600));
break;
}
return 100;
}
@[member=Override]
public void onExit() {
}
//method taken from Explv's paint tutorial -> osbot.org/forum/topic/87697-explvs-dank-paint-tutorial/
public final String formatTime(final long ms){
long s = ms / 1000, m = s / 60, h = m / 60;
s %= 60; m %= 60; h %= 24;
return String.format("%02d:%02d:%02d", h, m, s);
}
@[member=Override]
public void onPaint(Graphics2D g) {
double attackXP = getExperienceTracker().getGainedXP(Skill.ATTACK);
double strengthXP = getExperienceTracker().getGainedXP(Skill.STRENGTH);
double defenceXP = getExperienceTracker().getGainedXP(Skill.DEFENCE);
double hitpointsXP = getExperienceTracker().getGainedXP(Skill.HITPOINTS);
int attackLvls = getExperienceTracker().getGainedLevels(Skill.ATTACK);
int strengthLvls = getExperienceTracker().getGainedLevels(Skill.STRENGTH);
int defenceLvls = getExperienceTracker().getGainedLevels(Skill.DEFENCE);
int hitpointsLvls = getExperienceTracker().getGainedLevels(Skill.HITPOINTS);
g.drawString("Attack XP gained: " + attackXP, 25, 100);
g.drawString("Strength XP gained: " + strengthXP, 25, 120);
g.drawString("Defence XP gained: " + defenceXP, 25, 140);
g.drawString("HP XP gained: " + hitpointsXP, 25, 160);
g.drawString("---", 25, 180);
g.drawString("Attack Lvls gained: " + attackLvls, 25, 200);
g.drawString("Strength Lvls gained: " + strengthLvls, 25, 220);
g.drawString("Defence Lvls gained: " + defenceLvls, 25, 240);
g.drawString("HP Lvls gained: " + hitpointsLvls, 25, 260);
g.drawString("---", 25, 280);
g.drawString("Runtime: " + formatTime((System.currentTimeMillis() - startTime)), 25, 300);
}
}
Cheers