Jump to content

Conditional Sleep With Lambda Expressions


Explv

Recommended Posts

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
Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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. ._.

Link to comment
Share on other sites

  • 1 year later...

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
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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