liverare Posted December 31, 2018 Share Posted December 31, 2018 15 hours ago, NoxMerc said: Thought I'd add a comment here instead of a new thread. Made a smol addition to the Sleep class that saves some lines of code down the road. public static <k> k untilNotNull(final Supplier<k> locator, final int timeout, final int interval) { Sleep.until(() -> locator.get() != null, timeout, interval); return locator.get(); } Using this, you can essentially wait for an item to be not-null, and it'll return it. Like so: RS2Object coal = Sleep.untilNotNull(() -> ctx.getObjects().closest(ent -> MiningEntity.COAL.hasOre(ent)), 15_000, 750); if (coal == null) //ABORT I could save an additional call to the locator by wrapping the <k> in a mutable final object, but meh. I wouldn't sleep until the ore is regenerated, because you risk blocking your entire script for an unspecified amount of time. That's risky, especially if you're surrounded by hostile foes. Consider leveraging the script loop instead. Each loop cycle is a brand new chance for you to assess and re-assess where you are, what you're doing, then to figure out what you're going to do next. That way, if you're waiting on ore but you're suddenly attacked, your script can go into a completely different state of behaviour where it focuses on mitigating the danger before resuming its mining task. Quote Link to comment Share on other sites More sharing options...
NoxMerc Posted December 31, 2018 Share Posted December 31, 2018 (edited) It's fine for my use case and simplifies my code a bit. I purposefully did it so that I wasn't constantly executing through the script. I see where you are coming from though. Edited December 31, 2018 by NoxMerc Quote Link to comment Share on other sites More sharing options...
maxStart90 Posted April 7, 2019 Share Posted April 7, 2019 This just saved ton of my shit ... holy the built in ones are either aweful or I cant use them , thanks man ! Quote Link to comment Share on other sites More sharing options...
Glaciation96 Posted January 11, 2020 Share Posted January 11, 2020 (edited) Hello folks, Could I get some help to understand what is going on in the Sleep class please? I understand that it allows your script to use lambdas when sleeping, and you'll just be calling the static sleepUntil method, which fills out the Sleep constructor and returns its own object method, but my question is; When does the mandate override boolean method condition() get called? It returns the functional method from BooleanSupplier, but I can't seem to follow the code from around this point. Is there perhaps a link between the member variable 'condition' and the override method condition()? It's not just there because it extends ConditionalSleep is it? I'm guessing it's actually doing something.. Any help would be greatly appreciated. I've been stumped by this for around a week now so I thought I had to ask. Many thanks! Edited January 11, 2020 by Glaciation96 Clarfiying Quote Link to comment Share on other sites More sharing options...
Camaro Posted January 12, 2020 Share Posted January 12, 2020 8 hours ago, Glaciation96 said: Hello folks, Could I get some help to understand what is going on in the Sleep class please? I understand that it allows your script to use lambdas when sleeping, and you'll just be calling the static sleepUntil method, which fills out the Sleep constructor and returns its own object method, but my question is; When does the mandate override boolean method condition() get called? It returns the functional method from BooleanSupplier, but I can't seem to follow the code from around this point. Is there perhaps a link between the member variable 'condition' and the override method condition()? It's not just there because it extends ConditionalSleep is it? I'm guessing it's actually doing something.. Any help would be greatly appreciated. I've been stumped by this for around a week now so I thought I had to ask. Many thanks! The condition() method gets called in the super class. It stops sleeping as soon as condition() return true. basically; while (condition() == false) { sleep(sleepTime) if (System.currentTimeMillis() > startTime + maxSleepTime) { break; } } And that's coming from the ConditionalSleep class's sleep() method in the decompiled osbot jar 1 Quote Link to comment Share on other sites More sharing options...
chikaij Posted July 9, 2022 Share Posted July 9, 2022 (edited) The conditional sleep doesnt work properly, Here you have my code package com.company; import org.osbot.rs07.script.MethodProvider; import org.osbot.rs07.utility.ConditionalSleep; import java.lang.reflect.Method; import java.util.function.BooleanSupplier; public final class Sleep extends ConditionalSleep { private final BooleanSupplier condition; public Sleep(final BooleanSupplier condition, final int timeout) { super(timeout); this.condition = condition; } public Sleep(final BooleanSupplier condition, final int timeout, final int interval) { super(timeout, interval); 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(); } public static boolean sleepUntil(final BooleanSupplier condition, final int timeout, final int interval) { return new Sleep(condition, timeout, interval).sleep(); } } My ide says somtehing about sleepUntil not returning a value. How can this be Edited July 9, 2022 by chikaij Quote Link to comment Share on other sites More sharing options...
Gunman Posted July 9, 2022 Share Posted July 9, 2022 55 minutes ago, chikaij said: My ide says somtehing about sleepUntil not returning a value. How can this be Works for me @Override public int onLoop() throws InterruptedException { Sleep.sleepUntil(()-> getInventory().contains("Bones"), 5_000); Sleep.sleepUntil(()-> getInventory().contains("Bones"), 5_000, 600); return 600; } import org.osbot.rs07.utility.ConditionalSleep; import java.util.function.BooleanSupplier; public final class Sleep extends ConditionalSleep { private final BooleanSupplier condition; public Sleep(final BooleanSupplier condition, final int timeout) { super(timeout); this.condition = condition; } public Sleep(final BooleanSupplier condition, final int timeout, final int interval) { super(timeout, interval); 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(); } public static boolean sleepUntil(final BooleanSupplier condition, final int timeout, final int interval) { return new Sleep(condition, timeout, interval).sleep(); } } Quote Link to comment Share on other sites More sharing options...