Jump to content

How exactly does ConditionalSleep/Sleep and onLoop work together?


Recommended Posts

Posted

Hi all,

So I'm experiencing an issue where my ConditionalSleep isn't blocking the execution of onLoop.

For example this snippet below should sleep until my inventory only contains one kind of item.

log("OnLoop has been run again");
new ConditionalSleep(Integer.MAX_VALUE) {
    public boolean condition() throws InterruptedException {
        return inventory.onlyContains('something');
    }
}.sleep();

But the logger shows that onLoop is being repetitively called.

Am I misunderstanding how ConditionalSleep works here? If so what is the correct way of blocking my script from running.

 

Posted

onLoop is basically a while(true) loop. It's the core of the script. It runs repeatedly until the script is stopped. 

 

ConditionalSleep is used to sleep until an action happens. I know a lot of people use this class for conditional sleeps:

So to drop something, you may do:

	if(inventory.drop(itemId)) {
                Sleep.sleepUntil(() -> !inventory.contains(itemId), 2000);
                Thread.sleep(MethodProvider.random(100, 400));
            }

It really cleans it up.

Posted (edited)

Hmmm interesting is this directly in the onLoop() or are you calling it from somewhere?

Also I noticed that if the inventory is completely empty than inventory.onlyContains() method will return true.

Try this:

new ConditionalSleep(Integer.MAX_VALUE) {
    @Override
    public boolean condition() throws InterruptedException {
        return !getInventory().isEmpty() && getInventory().onlyContains("Something");
    }
}.sleep();
Edited by BravoTaco
  • Like 1
Posted

A few things to note

  • Make sure you calling the ConditionalSleep within the main thread, if you didn't create a new thread you dont have to worry about this.
  • Don't conditional sleep for the max duration, thats how your script gets stuck in the future and never recovers
  • Make sure the condition isnt evaluating to true, you may think it behaves in a certain way but theres every possibility that it doesnt, and you will need to add extra conditions
Posted
13 hours ago, BravoTaco said:

Hmmm interesting is this directly in the onLoop() or are you calling it from somewhere?

Also I noticed that if the inventory is completely empty than inventory.onlyContains() method will return true.

Try this:


new ConditionalSleep(Integer.MAX_VALUE) {
    @Override
    public boolean condition() throws InterruptedException {
        return !getInventory().isEmpty() && getInventory().onlyContains("Something");
    }
}.sleep();

Turns out this was the issue 😅, getInventory.onlyContains was returning True when my inventory was empty. Looks like I didn't look at it thoroughly enough since the api for it does say "This method does not require that all the specified items exist, it determines whether there are no other items in the container".

Thanks for the help guys!

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