Jump to content
Botre

Need help with InventoryListener

Recommended Posts

I seem to be having some trouble with caching the inventory amounts.

Halp :x

import java.util.Map;
import java.util.TreeMap;

import org.osbot.rs07.api.model.Item;

import dependencies.script.BotreScript;

/**
 * @author 		Bjorn Krols (Botre)
 * @version		0.0
 * @since		April 20, 2015
 */

public class InventoryListener extends Thread {

	private final BotreScript script;
	
	private Map<String, Integer> oldCache, newCache;
	
	public InventoryListener(final BotreScript script) {
		this.script = script;
		oldCache = new TreeMap<String, Integer>();
		newCache = new TreeMap<String, Integer>();
	}
	
	@Override
	public void run() {
		while (script.isRunning()) {
			if (script.isOnline()) {
				oldCache = newCache;
				newCache.clear();
				for (Item item : script.getInventory().getItems()) if (item != null && !newCache.containsKey(item.getName())) newCache.put(item.getName(), (int) script.getInventory().getAmount(item.getName()));
				onUpdate();
			}
			try {
				Thread.sleep(3000); // SLOW FOR READABILITY
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void onUpdate() { }
	
	public int getDifference(String name) {
		if (oldCache.containsKey(name) && newCache.containsKey(name)) {
			script.log("old " + oldCache.get(name));
			script.log("new" + newCache.get(name));
			return newCache.get(name) - oldCache.get(name);
		}
		else return 0;
	}

} 

How it currently logs:

[INFO][Bot #1][04/21 02:55:09 AM]: old 7
[INFO][Bot #1][04/21 02:55:09 AM]: new7
[INFO][Bot #1][04/21 02:55:12 AM]: old 9
[INFO][Bot #1][04/21 02:55:12 AM]: new9
[INFO][Bot #1][04/21 02:55:15 AM]: old 10
[INFO][Bot #1][04/21 02:55:15 AM]: new10
[INFO][Bot #1][04/21 02:55:18 AM]: old 12
[INFO][Bot #1][04/21 02:55:18 AM]: new12

How it's supposed to log:

[INFO][Bot #1][04/21 02:55:09 AM]: old 5
[INFO][Bot #1][04/21 02:55:09 AM]: new7
[INFO][Bot #1][04/21 02:55:12 AM]: old 7
[INFO][Bot #1][04/21 02:55:12 AM]: new9
[INFO][Bot #1][04/21 02:55:15 AM]: old 9
[INFO][Bot #1][04/21 02:55:15 AM]: new10
[INFO][Bot #1][04/21 02:55:18 AM]: old 10
[INFO][Bot #1][04/21 02:55:18 AM]: new12



Solved:

	if (script.isOnline()) {
				oldCache.clear();
				oldCache.putAll(newCache);
				newCache.clear();
				for (Item item : script.getInventory().getItems()) if (item != null && !newCache.containsKey(item.getName())) newCache.put(item.getName(), (int) script.getInventory().getAmount(item.getName()));
				onUpdate();
			}
Edited by Botre
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.


  • Recently Browsing   0 members

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