Jump to content

Inventory listener


Joseph

Recommended Posts

The class is named InventoryTrackers (with an s), the interface name is InventoryTracker (without the s, found statically in the class.)

package dependencies.trackers;

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

import org.osbot.rs07.api.model.Item;
import org.osbot.rs07.script.Script;

@SuppressWarnings("deprecation")
public class InventoryTrackers implements Runnable	{

	public static interface InventoryTracker	{
		public void handleAdded(Item item);	
		public void handleRemoved(Item item);
	}
	
 	private List<Item> current = new ArrayList<Item>();
 	private InventoryTracker event;
 	private boolean isRunning = true;
	private Script s;
	
	public InventoryTrackers(Script script, InventoryTracker e) {
		this.event = e;
		this.s = script;
		updateItems();
	}

	public void run() {
		while (isRunning)	{
			process(event);
			try {
				Thread.sleep(1200);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void stop()	{
		this.isRunning = false;
	}
	
	public void updateItems()	{
		current.clear();
		
		if (s.inventory.getItems() != null)	{
			for (Item item: s.inventory.getItems())	{
				if (item != null) {
					current.add(item);
				}else{
					current.add(new Item(-1, -1));
				}
			}			
		}
	}

	public void process(InventoryTracker e)	{	
		List<Item> ourItems = current;
		List<Item> newItems = getNewList();

		for (int i = 0; i < ourItems.size(); i++)	{
			Item ourItem = ourItems.get(i);
			Item newItem = newItems.get(i);
			
			if (ourItem.getId() != newItem.getId())	{
				e.handleRemoved(ourItem);
				e.handleAdded(newItem);
			}
			else if (ourItem.getAmount() != newItem.getAmount())	{
				if (ourItem.getAmount() < newItem.getAmount())	{
					e.handleAdded(newItem);
				}else{
					e.handleRemoved(ourItem);
				}
			}			
		}
		updateItems();
	}
	
	private List<Item> getNewList()	{
		List<Item> list = new ArrayList<Item>();
		
		if (s.inventory.getItems() != null)	{
			for (Item item: s.inventory.getItems())	{
				if (item != null) {
					list.add(item);
				}else{
					list.add(new Item(-1, -1));
				}
			}			
		}
		return list;
	}
}

 

How do you use it, you must create a new thread and initialize it. You must also end the thread once the script is done. Dont for get to implement InventoryTracker.

 

Example:

public class ScriptName extends Script implements InventoryTracker	{
     private InventoryTrackers invTracker; //create a field
}

Next create a new thread, and initialize the tracker within the onStart

Override
public void onStart() throws InterruptedException {
	this.invTracker = new InventoryTrackers(this, this);
	new Thread(invTracker).start();

}

Now within your script you should have the two method overrided. Keep the condition so it doesn't affect your script once its paused or not logged in.

	@Override
	public void handleAdded(Item item) {
		if (!bot.getScriptExecutor().isPaused() && client.isLoggedIn())	{  }
	}

	@Override
	public void handleRemoved(Item item) {
		if (!bot.getScriptExecutor().isPaused() && client.isLoggedIn())	{  }
	}		
	

Kill the thread once the script is over.

@Override
public void onExit() throws InterruptedException {	invTracker.stop();	}

if you have any question feel free to ask.  if im missing something let me know.

  • Like 1
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

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