dreameo Posted January 17, 2019 Share Posted January 17, 2019 (edited) Here's a guide on how to make an inventory listener. I don't think the bot offers it in the API, but you will see how simple it can be. The architecture of the following classes can be made better but this is how I've done it on my first try. (There wont be much explanation) Main Class: -Implements Observer -> Interface we made -Exchanges context since we extend MethodProvider -Sets the observer as the Main class Spoiler import org.osbot.rs07.api.model.Item; import org.osbot.rs07.script.Script; import org.osbot.rs07.script.ScriptManifest; @ScriptManifest(version = 1, logo = "", author = "Dream", info = "", name = "Listener") public class Main extends Script implements Observer { private InventoryListener inventoryListener = InventoryListener.getInstance(); @Override public void onStart() throws InterruptedException { inventoryListener.exchangeContext(getBot()); inventoryListener.setObserver(this); new Thread(inventoryListener).start(); } @Override public int onLoop() throws InterruptedException { return 256; } @Override public void inventoryListener(Item item, boolean gained) { log("Item: " + item.getName() + " Gained or Lost: " + gained); } @Override public void onExit() throws InterruptedException { inventoryListener.setRun(false); } } InventoryListener Class Note: I think the code would break if you try running it while logged off. An easy fix would be to set the run state once we are logged in. -Implements runnable, this is a seperate thread that runs to check the status of our inventory -Really simple strategy, we check our current items with a cached set of items. If there are any differences, we know that we either gained/lost an item. Whenever those cases occur, we notify our observer. -The thread runs ever 50 ms = checks for new items 20 times per second. Spoiler import org.osbot.rs07.api.model.Item; import org.osbot.rs07.script.MethodProvider; public class InventoryListener extends MethodProvider implements Runnable { private Observer observer; private volatile boolean run; private boolean initiated; private Item[] items; private static InventoryListener inventoryListener = new InventoryListener(); private InventoryListener(){ run = true; initiated = false; } public static InventoryListener getInstance(){ return inventoryListener; } public void setObserver(Observer observer){ this.observer = observer; } public void setRun(boolean run){ this.run = run; } @Override public void run() { while (run){ if(!initiated){ items = getInventory().getItems(); initiated = true; } else { Item[] newItems = getInventory().getItems(); Item newItem, oldItem; for(int i = 0; i < 27; i++) { oldItem = items[i]; newItem = newItems[i]; if(newItem == null && oldItem != null){ observer.inventoryListener(oldItem, false); } else if(oldItem == null && newItem != null){ observer.inventoryListener(newItem, true); } } items = newItems; } try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } } Observer - Interface Spoiler import org.osbot.rs07.api.model.Item; public interface Observer { void inventoryListener(Item item, boolean gained); } Edited January 18, 2019 by dreameo 4 Quote Link to comment Share on other sites More sharing options...