Botre Posted March 16, 2015 Share Posted March 16, 2015 (edited) A quick example of a ConditionalSleep class.The API one is great and I suggest you use that one, this is more for learning purposes. package org.bjornkrols.conditional; import org.bjornkrols.imported.MilliTimer; /** * @author Bjorn Krols (Botre) * @version 0.1 * @since March 16, 2015 */ public abstract class ConditionalSleep { /** * The time in milliseconds to sleep for per loop. */ private final int sleep; /** * The maximum (exclusive) time of sleep, in milliseconds, after which the sleep loop will be broken. */ private final int timeout; /** * @param sleep The time in milliseconds to sleep for per loop. * @param timeout The maximum (exclusive) time of sleep in milliseconds after which the sleep loop will be broken. */ public ConditionalSleep(final int sleep, final int timeout) { this.sleep = sleep; this.timeout = timeout; } /** * @return Whether to continue to sleep. */ public abstract boolean condition(); /** * Sleeps until the condition returns false or the timeout is reached. */ public void sleep() { MilliTimer timer = new MilliTimer(); while (condition() && timer.getElapsedMilliSeconds() < timeout) { try { Thread.sleep(sleep); } catch (InterruptedException e) { e.printStackTrace(); } } } } Implementation example: // Wait while moving or making fire. new ConditionalSleep(300, 5000) { @Override public boolean condition() { return script.myPlayer().isMoving() || script.myPlayer().getAnimation() == FIREMAKING_ANIMATION; } }.sleep(); Edited March 26, 2015 by Botre 3 Quote Link to comment Share on other sites More sharing options...
Tom Posted March 16, 2015 Share Posted March 16, 2015 I really don't know why I havent been using conditional sleeps all this time. Quote Link to comment Share on other sites More sharing options...
Botre Posted March 26, 2015 Author Share Posted March 26, 2015 UPDATED Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.