Ayylmao420 Posted January 4, 2021 Share Posted January 4, 2021 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; } } 1 Quote Link to comment Share on other sites More sharing options...
smile Posted January 4, 2021 Share Posted January 4, 2021 Couldn't you just set startTime = System.currentTimeMillis(); at the top and just call it into each class? Quote Link to comment Share on other sites More sharing options...
Ayylmao420 Posted January 4, 2021 Author Share Posted January 4, 2021 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. Quote Link to comment Share on other sites More sharing options...
smile Posted January 4, 2021 Share Posted January 4, 2021 1 minute ago, Ayylmao420 said: uhh, not sure, i'm a noob so i really don't know what's the correct way lol. Gotcha Quote Link to comment Share on other sites More sharing options...
Proto Posted January 4, 2021 Share Posted January 4, 2021 Nice code! The API already has conditional sleeps! Time.sleep(int timeout, booleanSupplier) @Ayylmao420 Quote Link to comment Share on other sites More sharing options...
Ayylmao420 Posted January 4, 2021 Author Share Posted January 4, 2021 7 minutes ago, Proto said: Nice code! The API already has conditional sleeps! Time.sleep(int timeout, booleanSupplier) @Ayylmao420 No reset condition though 😬 Quote Link to comment Share on other sites More sharing options...
Proto Posted January 4, 2021 Share Posted January 4, 2021 47 minutes ago, Ayylmao420 said: No reset condition though 😬 I'll suggest for it to be added to the API 😄 1 Quote Link to comment Share on other sites More sharing options...
Alias Posted January 5, 2021 Share Posted January 5, 2021 (edited) @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 January 5, 2021 by Alias 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.