Resource How to setup and use an InventoryListener

Discussion in 'Tutorials & Resources' started by Cloud, May 4, 2014.

  1. 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 bot implement InventoryListener
    Code (Text):
    2. 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 (Text):
    2. @Override
    3. public void onStart() {
    4.      getEventProcessor().addListener(this);
    5. }
    Finally, you need to override two methods in your script bot, 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 (Text):
    2. @Override
    3. public void onItemAdded(ItemEvent event) {
    4.     System.out.println("Item Added: " + event.getItem() + " (" + event.getQuantityChange() + ")");
    5. }
    7. @Override
    8. public void onItemRemoved(ItemEvent event) {
    9.     System.out.println("Item Removed: " + event.getItem() + " (" + event.getQuantityChange() + ")");
    10. }
  2. is event.getItem() returning the items name? Nice job by the way :)
  3. 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.
  4. Ah okay, it's just the way you were printing it out made it look like it was a value not an object
  5. Well SpriteItem has toString overridden so I figured it was easier.
  6. getEventProcessor() has been renamed getEventDispatcher() I assume?
    Baddest Man on Earth likes this.
  7. Cloud be slacking.
    LucasSousa likes this.

Share This Page