Decided I would share my solutions for Banking to help beginners as well as to receive critiques to improve my code.
Firstly, I typically do two things before I start banking. I generate a list of items that I DON'T want to be deposited (Banking Exceptions) & a list of items that my character will need to withdraw from the bank (Needed Supplies).
Here is my method to generate the Deposit Exceptions:
public LinkedList<String> getDepositExceptions() {
LinkedList<String> neededItems = new LinkedList<String>();
if (Config.enableAttPot){
neededItems.add("Attack potion(4)");
}
if (Config.enableStrPot){
neededItems.add("Strength potion(4)");
}
if (Config.enableSupAttPot){
neededItems.add("Super attack(4)");
}
if (Config.enableSupStrPot){
neededItems.add("Super strength(4)");
}
if (Config.enableCombatPot){
neededItems.add("Combat potion(4)");
}
neededItems.add("Lobster");
return neededItems;
}
Explained:
So I'm creating a list of items which I do not want to deposit into my bank. This list will be used later when I want to deposit all of the items in my inventory (except for those found in this list).
I'm using if statements for some items because the items may not be relevant for all users. This is handy if you have a GUI for your script where not everyone will have the same banking exceptions. Then for items which will be universal for your script (in this example, Lobster) you can simply add them to the list.
My method to Deposit All items (with the exception of those found in the getDepositExceptions() method above):
public void depositUnwanted() throws InterruptedException{
for (Item i : S.getInventory().getItems()) {
if (i != null && !getDepositExceptions().contains(i.getName())) {
S.log("Banking: " + i.getName());
i.interact("Deposit-All");
Script.sleep(Script.random(350,500));
}
}
}
Explained:
This will simply create a for loop which will look through all the items found in your inventory. If the item isn't an item found in the list generated by getDepositExceptions, it will deposit all of that item. May be beneficial to use a conditional sleep after the deposit instead of my way.
My method to generate a list of Needed Supplies:
public Entry<String, Integer> getNeededSupplies() {
LinkedHashMap<String, Integer> neededItems = new LinkedHashMap<String, Integer>();
if (Config.enableAttPot && (!S.inventory.contains("Attack potion(4)") || (S.getInventory().getAmount("Attack potion(4)") < Config.attAmt) )){
neededItems.put(Constants.ATTACK_B[0], (Config.attAmt - (int) S.getInventory().getAmount("Attack potion(4)")));
}
if (Config.enableStrPot && (!S.inventory.contains("Strength potion(4)") || (S.getInventory().getAmount("Strength potion(4)") < Config.strAmt))){
neededItems.put(Constants.STRENGTH_B[0], (Config.strAmt - (int) S.getInventory().getAmount("Strength potion(4)")));
}
if (Config.enableSupAttPot && (!S.inventory.contains("Super attack(4)") || (S.getInventory().getAmount("Super attack(4)") < Config.supAttAmt))){
neededItems.put(Constants.SUPER_ATTACK_B[0], (Config.supAttAmt - (int) S.getInventory().getAmount("Super attack(4)")));
}
if (Config.enableSupStrPot && (!S.inventory.contains("Super strength(4)") || (S.getInventory().getAmount("Super strength(4)") < Config.supStrAmt))){
neededItems.put(Constants.SUPER_STRENGTH_B[0], (Config.supStrAmt - (int) S.getInventory().getAmount("Super strength(4)")));
}
if (Config.enableCombatPot && (!S.inventory.contains("Combat potion(4)") || (S.getInventory().getAmount("Combat potion(4)") < Config.combatAmt))){
neededItems.put(Constants.COMBAT_B[0], (Config.combatAmt - (int) S.getInventory().getAmount("Combat potion(4)")));
}
if (S.getInventory().getAmount("Lobster") < Config.foodAmt){
neededItems.put(Config.foodName, (Config.foodAmt - (int) S.getInventory().getAmount("Lobster")));
}
final Set<Entry<String, Integer>> values = neededItems.entrySet();
final int maplength = values.size();
final Entry<String, Integer>[] test = new Entry[maplength];
values.toArray(test);
if (test.length > 0){
return test[0];
}
else return null;
}
Explained:
So here I am creating a Linked Hash Map (From my understanding, this is similar to a List). I've done this so that I can store the Item name & the amount that should be withdrawn in the same grouping to be used for later. This time, it is best to use an if statement for EVERY item because we need to check if your inventory doesn't already contain the item.
We're also doing some math to determine the correct amount to withdraw by subtracting the current amount in inventory from the maximum amount you should have. For me, I store the maximum in a Config class which grabs the data from my GUI (IE. config.attAmt)
My method for withdrawing item(s):
private void withdraw(String itemName, int amt) throws InterruptedException {
Item item = this.S.bank.getItem(itemName);
if (S.getBank().contains(itemName)) {
S.getBank().withdraw(item.getName(), amt);
Script.sleep(Script.random(350, 600));
} else {
S.log("Ran out of " + itemName + ", stopping.");
S.stop();
}
}
Explained:
A simple method with 2 parameters, the name of the item, and the amount to be withdrawn. If the bank contains your item, it will withdraw the amount given. If the bank does not contain your item, it will print into the Logger that you have run out of the item name, and will end your script. Again, it may be useful to add a conditional sleep instead of this random integer sleep.
My method to open the nearest bank:
private void openBank() throws InterruptedException {
S.getBank().open();
new ConditionalSleep(5000) {
@Override
public boolean condition() throws InterruptedException {
return S.getBank().isOpen();
}
}.sleep();
S.log("Banking");
}
Explained:
Will simply open the nearest bank, and have a 5-second conditional sleep which will wait 5 seconds if the bank is not open, or will cut the sleep off short when it sees that the bank is, in fact, open.
Putting it all together:
if (S.getBank() != null) {
if (!S.getBank().isOpen())
openBank();
else {
//Deposits all items except bank exceptions
for (Item i : S.getInventory().getItems()) {
if (i != null && !getDepositExceptions().contains(i.getName())) {
S.log("Banking: " + i.getName());
i.interact("Deposit-All");
Script.sleep(Script.random(350,500));
}
}
if (getNeededSupplies() != null){
S.log("Need to withdraw: " + getNeededSupplies().getKey() + ", " + getNeededSupplies().getValue() );
//.getKey() will return our LinkedHashMap String / itemName
//& .getValue() will return our Integer / Amount to withdraw
withdraw(getNeededSupplies().getKey(), getNeededSupplies().getValue());
}
}
}
Explained:
This is essentially a fully working Banking Class now. It will open the nearest bank if it's not already open. Then it will deposit all the items found in the inventory which aren't needed / desired. Then it will withdraw all of the items / supplies which will be needed for the task.
Hopefully, this is useful to you guys. I'm looking to improve my knowledge as well so if you see anything in this thread that can be optimized / improved, I would love to hear it!