Jump to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Conditional Sleep With Lambda Expressions

Featured Replies

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.

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 by NoxMerc

  • 3 months later...

This just saved ton of my shit ... holy the built in ones are either aweful or I cant use them , thanks man ! 

  • 9 months later...

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 by Glaciation96
Clarfiying

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

  • 2 years later...

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 by chikaij

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();
    }
}

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.