Jump to content

How to create an Inventory Listener


dreameo

Recommended Posts

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
  • Like 4
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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