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

Inventory Listener

Featured Replies

I wasn't happy with existing Inventory Listener snippets here so I converted one over from elsewhere. 

import org.osbot.rs07.api.Client;
import org.osbot.rs07.api.model.Item;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.utility.Condition;

import java.util.ArrayList;
import java.util.HashMap;

public class InventoryObserver extends Thread {
    private ArrayList<InventoryListener> listeners;
    private Condition condition;

    private Script theScript;
    private Client client;
    public InventoryObserver(Condition condition, Script script) {
        this.listeners = new ArrayList<>();
        this.condition = condition;
        this.theScript = script;
        this.client = script.client;
    }

    @[member=Override]
    public void run() {
        while (!client.isLoggedIn()) {
            try {
                theScript.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        HashMap<Integer, Integer> map = inventoryHashMap();
        while (true) {
            try {
                theScript.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (!client.isLoggedIn())
                continue;
            if (!condition.evaluate()) {
                map = inventoryHashMap();
                continue;
            }
            HashMap<Integer, Integer> updatedMap = inventoryHashMap();
            for (Integer i : updatedMap.keySet()) {
                int countInitial = map.containsKey(i) ? map.get(i) : 0, countFinal = updatedMap.get(i);
                if (countFinal > countInitial) {
                    addTrigger(i, countFinal - countInitial);
                } else if (countFinal < countInitial) {
                    subtractedTrigger(i, countInitial - countFinal);
                }
                map.remove(i);
            }
            for (Integer i : map.keySet())
                if (!updatedMap.containsKey(i))
                    subtractedTrigger(i, map.get(i));
            map = updatedMap;
        }
    }

    public HashMap<Integer, Integer> inventoryHashMap() {
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (Item item : theScript.inventory.getItems()) {
            if(item != null) {
                map.put(item.getId(), Integer.valueOf((int) theScript.inventory.getAmount(item.getId())));
            }
        }
        return map;
    }

    public void addListener(InventoryListener inventoryListener) {
        listeners.add(inventoryListener);
    }

    public void addTrigger(int id, int count) {
        for (InventoryListener l : listeners)
            l.inventoryItemGained(id, count);
    }

    public void subtractedTrigger(int id, int count) {
        for (InventoryListener l : listeners)
            l.inventoryItemLost(id, count);
    }
}

public interface InventoryListener {
    void inventoryItemGained(int id, int count);

    void inventoryItemLost(int id, int count);
}

Usage: Implement InventoryListener in your main class, and implement the methods your IDE suggested. In your onStart method, declare a new InventoryObserver object, including the condition under which you want the observer to run (i.e. make it not run while a bank is open). From there, call addListener(this) on your object and then call .start() on it. From there it should be fairly straight-forward to use.

Edited by Night

  • Author
while (true) {

Really??

 

Just don't do that.

 

Feel free to modify it.

Feel free to modify it.

 

Okay. Gimme a few minutes

I don't want to spoon feed everything, but this should be a good basis.

Also note that this class extends the API so you wont have to use script.client or script.inventory.

 

The thread will automatically stop after a script has stopped. 

import org.osbot.rs07.Bot;
import org.osbot.rs07.script.API;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Xerion on 16-1-2017.
 */
public class InventoryObserver extends API {

    private List<InventoryListener> listeners = new ArrayList<>();    

    /**
     *
     * @param bot
     */
    public InventoryObserver(Bot bot){
        this.exchangeContext(bot);
        new Thread(() -> {
            logger.debug("Started: InventoryWatcher");
            while (isRunning()) {
                try {
                    sleep(loop());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            logger.debug("Stopped: InventoryWatcher");
        }).start();
    }

    @[member=Override]
    public void initializeModule() {

    }

    /**
     *
     * @[member=Return]
     */
    private int loop(){
        /**
         * Add your code here to check if item is added/removed
         */
        return 100;
    }

    /**
     *
     * @[member=Return] True if a script is running
     */
    private boolean isRunning(){
        return bot.getScriptExecutor().getCurrent() != null;
    }
    
    /**
     *
     * @param listener
     */
    public void addListener(InventoryListener listener) {
        listeners.add(listener);
    }

    /**
     *
     * @param listener
     */
    public void removeListener(InventoryListener listener) {
        if(listener != null && listeners.contains(listener)) {
            listeners.remove(listener);
        }
    }
}
 

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

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.