Jump to content

Help with caching items from the bank


Recommended Posts

Posted (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 by Chikan
Posted (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 by Jammer
Posted (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 by Team Cape
  • Like 1
Posted (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 by liverare
  • Like 1

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...