Jump to content

How exactly does ConditionalSleep/Sleep and onLoop work together?


ultraswagger

Recommended Posts

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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

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!

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