NotoriousPP Posted May 30, 2014 Share Posted May 30, 2014 (edited) Well I thought today I would release some of my private collection of different snippets to hopefully help new writers learn a little bit, and to maybe see a rise in quality of scripts throughout this forum. My second release is my inventory monitor, which will keep track of items in your inventory changes. This came from the same framework as the Painter class, though this needed major changes in order to work for OSBot correctly, and along the help of a few people I got this running. This will create a item cache will you can then use to see when a item is removed from inventory, or when on has been added, and how many of them were updated. This is extremely usefull for keeping track of loot, or even in some cases use it to help keep track of your scripts progress. Original author: Coma - 06/29/13Revision author: NotoriousPP - 02/12/14 What does this class actually do? Keep track of crap in your inventory. How does is work?In the example below, I will show a sample code, and the output. **MUST USE ITEM IDs, NOT THEIR NAME!** private InventoryMonitor monitor; @Override public void onStart() { monitor = new InventoryMonitor(this) { @Override public void onChange() { for (Item i : getChanges()) { log("Updated Item ID: " + i.getId() + " by " + i.getAmount()); } update(); } }; } @Override public int onLoop() throws InterruptedException { if (monitor != null && !client.getBank().isOpen() && monitor.hasChanged()) { monitor.onChange(); } return gRandom(800, 300); } Output: Finally, where the magic happens:The source code to the InventoryMonitor class! import org.osbot.script.Script; import org.osbot.script.rs2.model.Item; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; /** * Original * @author Coma * Date: 6/29/13 * Time: 11:41 PM * -------------- * Updated * @author NotoriousPP * Date: 2/12/14 * Time: 4:57 PM */ public abstract class InventoryMonitor { private Item[] cache = new Item[0]; private Script ctx; /** * * @param ctx Script context. */ public InventoryMonitor(Script ctx) { updateContext(ctx); update(); } /** * This is needed in OSBot to keep the script context refreshed, * if this isn't called, the inventory will never update after the first tick. * @param ctx Script context. */ public void updateContext(Script ctx){ this.ctx = ctx; } /** * The method we use to handle when our inventory has been updated. */ public abstract void onChange(); /** * * @return has the inventory been updated/changed recently */ public boolean hasChanged() { return getChanges().length > 0; } /** * Update item cache. */ public void update() { cache = new Item[(int)ctx.client.getInventory().getTotalItemsAmount()]; int i = 0; for (Item item : ctx.client.getInventory().getItems()) { if(item != null){ cache[i++] = item; } } } /** * * @return array of items that has been recently updated. */ public Item[] getChanges() { int count = (int)ctx.client.getInventory().getTotalItemsAmount(); List<Item> items = new ArrayList<>(count); Item[] ci = new Item[count]; int changes = 0; Collections.addAll(items, ctx.client.getInventory().getItems()); for (Item item : items) { if(item != null){ int id = item.getId(); int c1 = (int)ctx.client.getInventory().getAmount(id), c2 = cached(id); if (c1 != c2 && !contains(ci, id)) { ci[changes++] = new Item(id, c1 - c2); } } } return Arrays.copyOf(ci, changes); } /** * * @param list Item array you wish to search * @param id ID of the item your searching for. * @return true if array contains item, otherwise false. */ private boolean contains(Item[] list, int id) { for (Item i : list) { if (i == null) continue; if (i.getId() == id) { return true; } } return false; } /** * * @param id Item of the item you searching for. * @return return amount of item in cached array. */ private int cached(int id) { int count = 0; for (Item i : cache) { if (i != null && i.getId() == id) { count += i.getAmount(); } } return count; } } Questions/Comments?:If you see anything I messed up on, or should be improved, please let me know, but be respectful about it, we have too many keyboard warriors thinking their hot shit, yet do nothing but bash others and never give any useful resources.Even if you have a question, free to ask me, just please refrain from asking me blatant obvious questions, or ones you did little to no research on before asking, I'm not here to spoon feed you, though I am willing to help someone is trying. Edited May 30, 2014 by NotoriousPP 1 Link to comment Share on other sites More sharing options...
PolishCivil Posted May 30, 2014 Share Posted May 30, 2014 Wait... @Override public int onLoop() throws InterruptedException { new Thread(new Runnable() { @Override public void run() { if (monitor != null && !client.getBank().isOpen() && monitor.hasChanged()) { monitor.onChange(); } } }).start(); return gRandom(800, 300); } ] new Thread(new Runnable() { @Override public void run() { if (monitor != null && !client.getBank().isOpen() && monitor.hasChanged()) { monitor.onChange(); } } }).start(); public void run() { if (monitor != null && !client.getBank().isOpen() && monitor.hasChanged()) { monitor.onChange(); } } What? Link to comment Share on other sites More sharing options...
NotoriousPP Posted May 30, 2014 Author Share Posted May 30, 2014 Wait... @Override public int onLoop() throws InterruptedException { new Thread(new Runnable() { @Override public void run() { if (monitor != null && !client.getBank().isOpen() && monitor.hasChanged()) { monitor.onChange(); } } }).start(); return gRandom(800, 300); } ] new Thread(new Runnable() { @Override public void run() { if (monitor != null && !client.getBank().isOpen() && monitor.hasChanged()) { monitor.onChange(); } } }).start(); public void run() { if (monitor != null && !client.getBank().isOpen() && monitor.hasChanged()) { monitor.onChange(); } } What? Fuck me... Sorry less than 6 hours of sleep, and too much on my mind. I just copy/pasted that shit in my onLoop just for a preview, but I'll go back and fix it right now. Link to comment Share on other sites More sharing options...
PolishCivil Posted May 30, 2014 Share Posted May 30, 2014 (edited) Fuck me... Sorry less than 6 hours of sleep, and too much on my mind. I just copy/pasted that shit in my onLoop just for a preview, but I'll go back and fix it right now. Know that feel i havent sleep for 50h ;s When i code i have hallucinations about type parameters smoking weed with me. Edited May 30, 2014 by PolishCivil 3 Link to comment Share on other sites More sharing options...