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.

How to create an Inventory Listener

Featured Replies

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 by dreameo

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.