Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

Sign up now!

Resource How to setup and use an InventoryListener

Engineer
Joined
Jul 28, 2013
Messages
2,776
An InventoryListener is a utility used for monitoring inventory changes, such as for detecting when you catch a fish, get a log from cutting a tree, or set a log on the ground for starting a fire.

The first step to using it is to make your script implement InventoryListener
Code:
public final class ListenerTest extends LoopingScript implements InventoryListener {
After that, you need to ensure that you submit your InventoryListener to the event processor so it's notified when inventory related events happen
Code:
@Override
public void onStart() {
     getEventProcessor().addListener(this);
}
Finally, you need to override two methods in your script, one for handling when an item is added and one for handling when an item is removed.

Note: You need to override both even if you only need one.
Code:
@Override
public void onItemAdded(ItemEvent event) {
    System.out.println("Item Added: " + event.getItem() + " (" + event.getQuantityChange() + ")");
}

@Override
public void onItemRemoved(ItemEvent event) {
    System.out.println("Item Removed: " + event.getItem() + " (" + event.getQuantityChange() + ")");
}
 
Author of MaxiBots
Joined
Dec 3, 2013
Messages
6,774
is event.getItem() returning the items name? Nice job by the way :)
 
Engineer
Joined
Jul 28, 2013
Messages
2,776
is event.getItem() returning the items name? Nice job by the way :)
It's returning a SpriteItem instance, which you could use for highlighting the inventory spot, getting the id, getting the name, or anything else you can do with a SpriteItem.
 
Author of MaxiBots
Joined
Dec 3, 2013
Messages
6,774
It's returning a SpriteItem instance, which you could use for highlighting the inventory spot, getting the id, getting the name, or anything else you can do with a SpriteItem.
Ah okay, it's just the way you were printing it out made it look like it was a value not an object
 
Joined
Dec 10, 2014
Messages
3,255
An InventoryListener is a utility used for monitoring inventory changes, such as for detecting when you catch a fish, get a log from cutting a tree, or set a log on the ground for starting a fire.

The first step to using it is to make your script implement InventoryListener
Code:
public final class ListenerTest extends LoopingScript implements InventoryListener {
After that, you need to ensure that you submit your InventoryListener to the event processor so it's notified when inventory related events happen
Code:
@Override
public void onStart() {
     getEventProcessor().addListener(this);
}
Finally, you need to override two methods in your script, one for handling when an item is added and one for handling when an item is removed.

Note: You need to override both even if you only need one.
Code:
@Override
public void onItemAdded(ItemEvent event) {
    System.out.println("Item Added: " + event.getItem() + " (" + event.getQuantityChange() + ")");
}

@Override
public void onItemRemoved(ItemEvent event) {
    System.out.println("Item Removed: " + event.getItem() + " (" + event.getQuantityChange() + ")");
}
getEventProcessor() has been renamed getEventDispatcher() I assume?
 
Top