chardragon Posted August 18, 2023 Share Posted August 18, 2023 Hi as title says I know that it is discouraged to use "while" in the loop, but for the activities mentioned you cant use !myPlayer().isAnimating() due to the character hitting -1 animation during the creation of items. For example, when stringing magic longbows every 5 or so I get a -1 animation and so the bot tries to string again. I currently am getting around this using a while loop with a counter which basically will break the loop when it hits a number which means something isnt working properly for whatver reason but I know this isnt a good way to handle it. I am struggling to see anything in the api to help (although i am not the best at reading apis). I was wondering what the consensus is on how to work around this issue? 1 Quote Link to comment Share on other sites More sharing options...
Khaleesi Posted August 18, 2023 Share Posted August 18, 2023 (edited) Ya do not use a while loop. You can use a Timer and set it to 2-3 seconds and reset it every time u animating and only start interacting or crafting when the timer is not running Edited August 18, 2023 by Khaleesi 1 Quote Link to comment Share on other sites More sharing options...
chardragon Posted August 18, 2023 Author Share Posted August 18, 2023 33 minutes ago, Khaleesi said: Ya do not use a while loop. You can use a Timer and set it to 2-3 seconds and reset it every time u animating and only start interacting or crafting when the timer is not running Hey thanks for the reply is there any chance you can give an example? 1 Quote Link to comment Share on other sites More sharing options...
Khaleesi Posted August 18, 2023 Share Posted August 18, 2023 (edited) 8 minutes ago, chardragon said: Hey thanks for the reply is there any chance you can give an example? public class AnimationCheck extends Script { private final Timer animationTimer = new Timer(3_000); @Override public void onStart() { } @Override public int onLoop() { if(myPlayer().isAnimating()){ animationTimer.reset(); } if(animationTimer.isRunning()){ log("Animating"); }else{ //Start doing whatever you wanted to do } return 50; } } Basic Timer class public class Timer { private long period; private long startTime; public Timer(long period) { this.period = period; startTime = System.currentTimeMillis(); } public boolean isRunning() { return getElapsed() < period; } public long getElapsed() { return System.currentTimeMillis() - startTime; } public long getRemaining() { return period - getElapsed(); } public void reset() { startTime = System.currentTimeMillis(); } public void stop() { period = 0; } public static String formatTime(long ms) { long sec = ms / 1000L; return String.format("%02d:%02d:%02d", Long.valueOf(sec / 3600L), Long.valueOf((sec % 3600L) / 60L), Long.valueOf(sec % 60L)); } } Edited August 18, 2023 by Khaleesi Quote Link to comment Share on other sites More sharing options...
abc3 Posted August 18, 2023 Share Posted August 18, 2023 9 hours ago, chardragon said: Hi as title says I know that it is discouraged to use "while" in the loop, but for the activities mentioned you cant use !myPlayer().isAnimating() due to the character hitting -1 animation during the creation of items. For example, when stringing magic longbows every 5 or so I get a -1 animation and so the bot tries to string again. I currently am getting around this using a while loop with a counter which basically will break the loop when it hits a number which means something isnt working properly for whatver reason but I know this isnt a good way to handle it. I am struggling to see anything in the api to help (although i am not the best at reading apis). I was wondering what the consensus is on how to work around this issue? Look at the ConditionalSleep class, it uses a loop but allows interruption You basically want a nested condition, sleep until you haven't animated for x seconds (sleep until the nested sleep fails) Quote Link to comment Share on other sites More sharing options...
abc3 Posted August 18, 2023 Share Posted August 18, 2023 import org.osbot.rs07.utility.ConditionalSleep; import java.util.function.BooleanSupplier; public class Sleep extends ConditionalSleep { private final BooleanSupplier condition; public Sleep(final BooleanSupplier condition, final int timeout) { super(timeout); this.condition = condition; } public static boolean sleepUntil(final BooleanSupplier condition, final int timeout) { return new Sleep(condition, timeout).sleep(); } @Override public final boolean condition() { return condition.getAsBoolean(); } } Usage Sleep.sleepUntil(() -> !Sleep.sleepUntil(api.myPlayer()::isAnimating, random(4000, 8000)), random(20000, 40000)) Sleep until haven't animated for 4-8 seconds, returns boolean whether condition was met or timed out Quote Link to comment Share on other sites More sharing options...
Alakazizam Posted September 4, 2023 Share Posted September 4, 2023 You could do a conditional sleep when you push through the craft and have the break condition be when your inventory no longer contains the required crafting items. Then after that have a simple sleep with a randomizer after that. 1 Quote Link to comment Share on other sites More sharing options...