Jump to content

InventoryMonitor snippet


Recommended Posts

Posted


/**

* Created by Bjorn on 20/06/2015.

*/

public abstract class InventoryMonitor extends Thread {

private final BotreScript script;

/**

* Previous and current copies of the inventory instance.

*/

private final Map<String, Integer> previous, current;

public InventoryMonitor(final BotreScript script) {

this.script = script;

previous = new TreeMap<>();

current = new TreeMap<>();

}

@Override

public void run() {

// Run thread while script is running.

while (script.isRunning()) {

Inventory inventory;

if (script.isOnline() && (inventory = script.getInventory()) != null) {

// Clear previous cache.

previous.clear();

// Copy mappings of the current cache to the previous cache.

previous.putAll(current);

// Clear current cache.

current.clear();

// Rebuild current cache.

for (Item item : inventory.getItems()) {

if (item != null ) {

String name = item.getName();

if (name != null && !current.containsKey(name)) current.put(name, (int) inventory.getAmount(name));

}

}

}

try {

// Call onLoop and sleep for its return value.

Thread.sleep(onLoop());

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

public abstract int onLoop();

public int getDifference(String name) {

if (current.containsKey(name)) return current.get(name) - (previous.containsKey(name) ? previous.get(name) : 0);

else if (previous.containsKey(name)) return 0 - previous.get(name);

else return 0;

}

}

  • Like 3
  • 4 weeks later...
Posted (edited)

for those that need a replacement

   @Override
	public void run() {
        // Run thread while script is running.
    	if (script != null) {
			while (script.getBot().getScriptExecutor().isRunning()) {
	            Inventory inventory;
	            if ((inventory = script.getInventory()) != null) {
	                // Clear previous cache.
					previous.clear();

Edited by josedpay
  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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