I wrote up a quick example script. Not everything is implemented I left the buyItem() method blank so that you can implement that.
It uses an Array of strings for the different items to alch, and a HashMap<String, Integer> that holds the alched count for each item.
If you have any questions let me know.
Also this code is untested. So it may be broken
public class UCAlcher extends Script {
private BotState botState;
// This is used to keep track of how many of each item that can be alched, has been alched.
private final HashMap<String, Integer> alchsMap = new HashMap<>();
// The different items you want to alch.
private final String[] itemNames = new String[]{"Rune 2h Sword", "Rune platebody", "Rune platelegs"};
// This value represents how many items will be alched/bought for each item.
private int alchLimitForEachItem = 2;
private String currentAlchItemName;
@Override
public void onStart() throws InterruptedException {
// Initialize the HashMap with the values from the itemNames array.
for (String s : itemNames) {
alchsMap.put(s, 0);
}
}
@Override
public int onLoop() throws InterruptedException {
botState = setBotState();
if (botState != null) {
switch (botState) {
case ALCH_ITEM: {
alchItem();
}
case BUY_ITEM: {
buyItem();
}
}
} else {
warn("The botState is null.");
stop(false);
}
return random(800, 3600);
}
private BotState setBotState() {
currentAlchItemName = null;
//
for (Item item : getInventory().getItems()) {
if (item != null && alchsMap.containsKey(item.getName()) && alchsMap.get(item.getName()) < alchLimitForEachItem) {
currentAlchItemName = item.getName();
return BotState.ALCH_ITEM;
}
}
// If the currentAlchItemName is null here than that means we need to possibly buy a new item to alch
// or we need to reset the alch count on each item.
if (currentAlchItemName == null) {
for (String s : alchsMap.keySet()) {
if (alchsMap.get(s) < alchLimitForEachItem) {
currentAlchItemName = s;
return BotState.BUY_ITEM;
}
}
}
// If the currentAlchItemName is still null than that means we have bought and alched all items.
// So now we have to reset the alch count on each item and set the currentAlchItemName to the first index in the itemNames array.
if (currentAlchItemName == null) {
for (String s : alchsMap.keySet()) {
alchsMap.replace(s, 0);
}
currentAlchItemName = itemNames[0];
return BotState.BUY_ITEM;
}
return null;
}
private void buyItem() {
// Implement the buying of the item here.
}
private void alchItem() throws InterruptedException {
Item alchItem = getInventory().getItem(currentAlchItemName);
if (alchItem != null) {
if (getMagic().canCast(Spells.NormalSpells.HIGH_LEVEL_ALCHEMY)) {
if (getMagic().castSpell(Spells.NormalSpells.HIGH_LEVEL_ALCHEMY)) {
if (sleepUntil(() -> alchItem.interact("cast"), 5000, 20)) {
alchsMap.replace(currentAlchItemName, alchsMap.get(currentAlchItemName) + 1);
} else {
warn("Unable to alch item.");
}
}
} else {
warn("Unable to cast high level alchemy. Stopping script.");
stop(false);
}
} else {
warn("The inventory does not contain the required item to alch. Something must have gone wrong when calculating the botState.");
}
}
private boolean sleepUntil(BooleanSupplier condition, int maxTime, int checkInterval) throws InterruptedException {
long startTime = System.currentTimeMillis();
while (System.currentTimeMillis() - startTime < maxTime) {
if (condition.getAsBoolean()) {
return true;
}
Thread.sleep(checkInterval);
}
return false;
}
private enum BotState {
BUY_ITEM,
ALCH_ITEM
}
}