Jump to content

Couple Sleep Methods


BravoTaco

Recommended Posts

Code:

Spoiler

Conditional Sleep For New Threads:
Spoiler


private boolean conditionalSleep(BooleanSupplier condition, long maxTime, long checkTime) {
    long startTime = System.currentTimeMillis();
    while (!condition.getAsBoolean() && (System.currentTimeMillis() - startTime) < maxTime) {
        try {
            Thread.sleep(checkTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    return condition.getAsBoolean();
}

Usage:



if (conditionalSleep(() -> script.myPlayer().isAnimating(), 10000, 500)) {
    script.log("Player Is Animating!");
    if (conditionalSleep(() -> !script.myPlayer().isAnimating(), 10000, 500)) {
        script.log("Player is No Longer Animating");
    }
}

Animation Delay Sleep:
Spoiler


private boolean sleepWithAnimationDelay(int maxTimeToWaitForAnimationStart, int maxTimeForAnimationDelay) throws InterruptedException {
    new ConditionalSleep(maxTimeToWaitForAnimationStart, 20) {
        @Override
        public boolean condition() throws InterruptedException {
            return myPlayer().isAnimating();
        }
    }.sleep();
    long startTime = System.currentTimeMillis();
    if (myPlayer().isAnimating()) {
        while (System.currentTimeMillis() - startTime < maxTimeForAnimationDelay) {
            if (myPlayer().isAnimating()) {
                startTime = System.currentTimeMillis();
                sleep(20);
            }
        }
    } else {
        return false;
    }
    return true;
}

Usage:



if (sleepWithAnimationDelay(10000, 3000)) {
    log("Animation Delay Over!");
}

 

 

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