Chikan Posted December 15, 2017 Share Posted December 15, 2017 (edited) So I'm trying to cache items from the bank and save them for when the bank is closed, I've tried making a HashMap<String, Integer> for it, but what I've been doing isn't working and I'd like some assistance with how I can make it work. HashMap<String, Integer> bankHash = new HashMap<String, Integer>(); //adding values to my bankHash Integer amtOfItemHere = (int)Values.getBank().getAmount("itemNameHere"); bankHash.put("itemNameHere", amtOfItemHere); //method to grab the hashed item public int hashedItemHere() { return bankHash.get("itemNameHere"); //trying to return } I've tried nullchecking them before calling the method, but it won't collect the data that I'm trying to collect. It's likely because the Int I'm trying to use is only usable while the bank is open, but my issue is: how can I do this properly so I can cache items from the bank? What I'm trying to use this for is to calculate the number of items I need to buy from the GE // quantity = default - hashedNum; Edited December 15, 2017 by Chikan Quote Link to comment Share on other sites More sharing options...
Jammer Posted December 15, 2017 Share Posted December 15, 2017 (edited) Make a method which returns a hashmap and create a new hashmap. Use the getItem() method and then an enhanced for loop to add every name and ammount to the hashmap. Then you use this method every time you’ve made changes to your bank. and if you want to check if you need to buy and item you simply check if the keymap is there. Edited December 15, 2017 by Jammer Quote Link to comment Share on other sites More sharing options...
The Undefeated Posted December 15, 2017 Share Posted December 15, 2017 What I do is just create a new class and extend it with ItemContainer. Then make a field with an array of Item. Let getItems() return the array and create a setter for the array. Don't forget to declare the array as empty so when you check if it contains any item it won't NPE. After that you can just call the same methods on it like Inventory and Bank. 1 Quote Link to comment Share on other sites More sharing options...
Team Cape Posted December 15, 2017 Share Posted December 15, 2017 (edited) 4 hours ago, Chikan said: So I'm trying to cache items from the bank and save them for when the bank is closed, I've tried making a HashMap<String, Integer> for it, but what I've been doing isn't working and I'd like some assistance with how I can make it work. HashMap<String, Integer> bankHash = new HashMap<String, Integer>(); //adding values to my bankHash Integer amtOfItemHere = (int)Values.getBank().getAmount("itemNameHere"); bankHash.put("itemNameHere", amtOfItemHere); //method to grab the hashed item public int hashedItemHere() { return bankHash.get("itemNameHere"); //trying to return } I've tried nullchecking them before calling the method, but it won't collect the data that I'm trying to collect. It's likely because the Int I'm trying to use is only usable while the bank is open, but my issue is: how can I do this properly so I can cache items from the bank? What I'm trying to use this for is to calculate the number of items I need to buy from the GE // quantity = default - hashedNum; Item, by default, has a method called 'getAmount()' that will return the amount of the item. Do like @The Undefeated said, but to be honest, it does not need to extend ItemContainer. public class ItemCache { private Item[] items; public ItemCache() { items = new Item[]{}; } public void setItems(Item[] items) { this.items = items; } public Item[] getItems() { return items; } } Wherever you want the items to be saved, define/assign the following: ItemCache cache = new ItemCache(); Wherever you want to update the cache (i.e. when you’re using the bank), call the following: cache.setItems(getBank().getItems()); Whenever you want to check the item cache, do the following: cache.getItems(); If you need any utility methods, you might want to make it extend ItemContainer, or you can design them yourself Edited December 15, 2017 by Team Cape 1 Quote Link to comment Share on other sites More sharing options...
liverare Posted December 17, 2017 Share Posted December 17, 2017 (edited) Map<String, Integer> bankCache; void updateBankCache() { bankCache = Stream.of(bank.getItems()) .collect(Collectors.toMap(Item::getName, Item::getAmount, (a, b) -> a + b)); } { /// ... some bank-ery stuff before here updateBankCache(); bank.close(); // .. some other-ery stuff happens nao int bankedOakLogs = bankCache.get("Oak logs"); } There are items in RS that have the same name, but do not stack on top of each other in the bank, like the Wintertodt supply crates. With such items, there needs to be a merger function that makes sure the final map only contains unique keys. The merger function here "(a, b) -> a + b" is simply adding together the quantities of those unique items. Without a merger function, the script will error when items like the Wintertodt supply crates are found. Edited December 17, 2017 by liverare 1 Quote Link to comment Share on other sites More sharing options...