Jump to content

InventoryMonitor snippet


Botre

Recommended Posts


/**

* 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
Link to comment
Share on other sites

  • 4 weeks later...

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