Jump to content

Withdraw x amounts of charges/doses


Recommended Posts

Posted (edited)

Small but very useful snippet. This will prioritize items with the lowest dose. It returns null if not enough doses are in the bank.

Code:

Spoiler

    private HashMap<String,Integer> withdrawDoses(String item, int doses) {
        int i = 1;
        HashMap<String,Integer> map = new HashMap<>();
        while (doses > 0 && i <= 8) {
            String itemName = item + "(" + i + ")";
            int j = 1;
            while (doses > 0 && j <= getBank().getAmount(itemName)) {
                doses = doses - i;
                if (map.containsKey(itemName)) {
                    map.put(itemName,map.get(itemName) + 1);
                } else {
                    map.put(itemName,1);
                }
                j++;
            }
            i++;
        }
        if (doses <= 0) {
            return map;
        }
        return null;
        
    }

    private int getCurrentDoses(String item) {
        int i = 1;
        int doses = 0;
        while (i <= 8) {
            String itemName = item + "(" + i + ")";
            int curAmount = (int) (i * getInventory().getAmount(itemName));
            doses = doses + curAmount;
            i++;
        }
        return doses;
    }

 

Usage:

Spoiler

getBank().withdraw(withdrawDoses("Stamina potion",10));

 

 

Edited by The Undefeated
  • Like 6

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...