Jump to content

Conditional Sleep With Lambda Expressions


Recommended Posts

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

 

Usage:

Sleep sleep = new Sleep(() -> myPlayer().isAnimating(), 5000);
sleep.sleep();

Or:

Sleep.sleepUntil(() -> myPlayer().isAnimating(), 5000);

 

Edited by Explv
  • Like 3
  • Heart 1
  • Mald 1
Posted
public class CustomSleep extends ConditionalSleep{
	
	private BooleanSupplier condition;

	public CustomSleep(BooleanSupplier condition, int timeout) {
		super(timeout);
		this.condition = condition;
	}

	@Override
	public boolean condition() throws InterruptedException {
		return condition.getAsBoolean();
	}

}

Example usage

new CustomSleep(() -> myPlayer().isAnimating(), 2000).sleep();

 

Posted
1 minute ago, Tom said:

public class CustomSleep extends ConditionalSleep{
	
	private BooleanSupplier condition;

	public CustomSleep(BooleanSupplier condition, int timeout) {
		super(timeout);
		this.condition = condition;
	}

	@Override
	public boolean condition() throws InterruptedException {
		return condition.getAsBoolean();
	}

}

Example usage


new CustomSleep(() -> myPlayer().isAnimating(), 2000).sleep();

 

He just has an interval parameter to change how often to check I believe, otherwise it's the same. ._.

  • 1 year later...
Posted (edited)

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.

 

Edited by NoxMerc

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...