Jump to content

[Snippet] Sleep


Ayylmao420
 Share

Recommended Posts

import com.epicbot.api.shared.APIContext;
import com.epicbot.api.shared.util.Random;
import com.epicbot.api.shared.util.time.Time;

import java.util.function.BooleanSupplier;

public class Sleep {

    public static boolean sleep(APIContext apiContext, int timeout) {
        long startTime = System.currentTimeMillis();
        while (apiContext.script().isRunning() && !apiContext.script().isPaused()) {
            if (System.currentTimeMillis() - startTime >= timeout) {
                return true;
            }
            Time.sleep(10);
        }
        return true;
    }

    public static boolean sleepUntil(APIContext apiContext, BooleanSupplier breakCondition, int timeout) {
        long startTime = System.currentTimeMillis();
        while (apiContext.script().isRunning() && !apiContext.script().isPaused()) {
            if (breakCondition.getAsBoolean()) {
                return true;
            }
            if (System.currentTimeMillis() - startTime >= timeout) {
                return true;
            }
            Time.sleep(10);
        }
        return true;
    }

    public static boolean sleepUntil(APIContext apiContext, BooleanSupplier breakCondition, BooleanSupplier resetCondition, int timeout) {
        long startTime = System.currentTimeMillis();
        while (apiContext.script().isRunning() && !apiContext.script().isPaused()) {
            if (breakCondition.getAsBoolean()) {
                return true;
            }
            if (resetCondition.getAsBoolean()) {
                startTime = System.currentTimeMillis();
            }
            if (System.currentTimeMillis() - startTime >= timeout) {
                return true;
            }
            Time.sleep(10);
        }
        return true;
    }

}

 

  • Like 1
Link to comment
Share on other sites

12 minutes ago, smile said:

Couldn't you just set 
 

startTime = System.currentTimeMillis();

at the top and just call it into each class?

uhh, not sure, i'm a noob so i really don't know what's the correct way lol.

Link to comment
Share on other sites

@Ayylmao420 I feel like the boolean is a bit redundant as it is always returning true. There is nothing in the code that provides a false return so there's not really a point to check it with an if statement.

This is what I do in my API 

public static void sleepUntil(final BooleanSupplier condition, final long timeout) {
    long start = System.currentTimeMillis();
    while (!condition.getAsBoolean()) {
        if (System.currentTimeMillis() > start + timeout) {
            break;
        }
    }
}

And if you want to add a reset condition can just do this:

public static void sleepUntil(final BooleanSupplier condition, final BooleanSupplier resetCondition, final long timeout) {
    long start = System.currentTimeMillis();
    while (!condition.getAsBoolean()) {
        if (resetCondition.getAsBoolean()) {
            start = System.currentTimeMillis();
        } else if (System.currentTimeMillis() > start + timeout) {
            break;
        }
    }
}

 

There is no need to pass the API context into the methods. The conditions are already provided and if the script ends the thread should be interrupted and everything will stop so you shouldn't be stuck in an infinite loop unless you ran this on a concurrent thread and didn't interrupt it onStop();

Edited by Alias
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.

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.

 Share

×
×
  • Create New...