Botre Posted March 16, 2015 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
Tom Posted March 16, 2015 Posted March 16, 2015 I really don't know why I havent been using conditional sleeps all this time.