Skip to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Help with caching items from the bank

Featured Replies

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

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

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.

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

	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

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.