There are a number of structural bad practices here which are likely causing the issue, or unreliability in the script as a whole. If you fix these, you're more likely to fix your original problem!
You're writing the script in such a way that you're expecting each line to successfully execute every time. This is not (and can never be) the case as you're interacting with a live game so there is a whole middle layer entirely out of your control (latency fluctuation, game errors, resource throttling, ...). Don't do this! Instead, endeavor to make your script stateless, i.e it is not relying on any instruction to execute successfully before immediately proceeding to another instruction.
You can achieve this by only ever having one interaction call per onLoop traversal (the interaction to execute would be determined by the current global game state). For example, a common mistake is as follows with banking:
private void withdrawTenEggs() { // An example of how NOT to withdraw 10 eggs
getBank().open();
getBank().withdraw("Eggs", 10);
getBank().close();
}
As you can see, there are three consecutive instructions. What if one of them were to fail - for example, the first 'getBank().open()' ? In this situation, the withdraw would have all sorts of problems as you cannot withdraw items if the bank is not open. This will likely throw horrible errors and potentially crash the script. Also, it is worth noting that jamming static sleeps between each of these calls will not fix the problem.
So we've seen how not to do it, but how should you do it? Take a look:
private void withdrawTenEggsProperly() { // How to properly withdraw 10 eggs
if (getInventory().contains("Eggs")) {
if (getBank().isOpen()) {
getBank().close();
}
} else {
if (getBank().isOpen()) {
getBank().withdraw("Eggs", 10);
} else {
getBank().open();
}
}
}
As you can see, the above example will only ever try to do one game interaction each time it is called. Arguably this is poor design, as after calling the method you are not guaranteed to be in the state you need to be in, but luckily this is less relevant in the context of a system where the state is encapsulated externally. The benefits this approach provides far outweigh the negatives, namely that this method is much less error prone as there are no linear dependencies and will always converge to an end goal. Furthermore, this methodology capitalises on the ever-looping nature of onLoop.
Hopefully that made sense - it's important to understand why your solution is unreliable rather than to patch up the code and settle for sub-par reliability
Good luck, let me know if you have any further questions
Apa