What I would do is start by structuring your script so that if an event fails at anytime, it will simply retry that action only, not the whole loop again (as @Apaec suggested):
if (!inventory.isItemSelected()) {
// Use first item
// Sleep until inventory.isSelected()
} else {
// Use first item with second item
// Sleep until finished making pie shell
}
Here is a full example taken from my AIO Herblore script:
if (!script.inventory.isItemSelected()) {
Item pestle = script.inventory.getItem("Pestle and mortar");
if (pestle != null) {
if (pestle.interact("Use")) {
Sleep.sleepUntil(() -> script.inventory.isItemSelected(), 5000);
}
}
} else {
int clickSlot = script.inventory.getSlot(itemToGrind);
InventorySlotDestination itemSlot = new InventorySlotDestination(script.getBot(), clickSlot);
script.getMouse().move(itemSlot);
if (script.getMouse().click(false)) {
Sleep.sleepUntil(() -> !script.inventory.contains(itemToGrind) || script.getDialogues().isPendingContinuation(), 120000);
}
}