Jump to content

Conditional Sleep With Lambda Expressions


Explv

Recommended Posts

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.

Link to comment
Share on other sites

  • 3 months later...
  • 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
Link to comment
Share on other sites

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

  • Like 1
Link to comment
Share on other sites

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

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