Jump to content

Help with caching items from the bank


Chikan

Recommended Posts

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

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

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.

  • Like 1
Link to comment
Share on other sites

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

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