Jump to content

Withdraw x amounts of charges/doses


The Undefeated

Recommended Posts

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
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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