Hi, I've just recently decided to try my hand at some basic scripting. I've been following a few tutorials on the site but I can't get the Conditional Sleep to work no matter where I place it. It's an extremely basic woodcutter, here's the code:
import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.utility.ConditionalSleep;
import org.osbot.rs07.script.ScriptManifest;
import java.util.function.BooleanSupplier;
@ScriptManifest(author = "whoknowshonestly", name = "Trial Woodcutter", info = "First script - will chop trees and drop the logs", version = 0.1, logo = "https://i.imgur.com/7m1RYof.png")
public final class Woodcutter extends Script {
private final Area oakChoppingArea = new Area(3129, 3329, 3148, 3317);
@Override
public final int onLoop() throws InterruptedException {
if (!oakChoppingArea.contains(myPosition())){
getWalking().webWalk(oakChoppingArea);}
if (canChopTree()) {
chopTree();
} else {
dropWood();
}
return random(1000, 2000);
}
private boolean canChopTree(){
Entity tree = getObjects().closest("Oak");
if (tree != null && tree.interact("Chop down") && !myPlayer().isAnimating()) {
return true;
}
return false;
}
private boolean isCurrentlyChopping(){
return myPlayer().isAnimating();
}
private boolean isInventoryEmpty() {
return getInventory().contains("");
}
private boolean inventoryContainsLogs(){
return getInventory().contains("Logs", "Oak logs", "Willow logs");
}
private void chopTree(){
if (isInventoryEmpty() && !isCurrentlyChopping()){
if (getObjects().closest("Oak").interact("Chop down")) {
new ConditionalSleep(10000, 10000) {
@Override
public boolean condition() {
return inventoryContainsLogs();
}
}.sleep();
}
}
}
private void dropWood(){
inventory.dropAll();
}
}
class Sleep extends ConditionalSleep {
private final BooleanSupplier condition;
public Sleep(final BooleanSupplier condition, final int timeout) {
super(timeout);
this.condition = condition;
}
@Override
public final boolean condition() throws InterruptedException {
return condition.getAsBoolean();
}
public static boolean sleepUntil(final BooleanSupplier condition, final int timeout) {
return new Sleep(condition, timeout).sleep();
}
}
I have tried placing the ConditionalSleep into the OnLoop(), and moved the conditions with it but it still will always click when the random timeout expires at the end of the OnLoop(). I could get the regular sleep method to work but obviously the conditional sleep is preferable and so would appreciate it if someone could point me in the right direction.