Resource How to setup and use an InventoryListener

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

  1. Cloud

    Cloud The Developer

    Joined:
    Jul 28, 2013
    Messages:
    2,300
    Likes Received:
    729
    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):
    1.  
    2. public final class ListenerTest extends LoopingScript implements InventoryListener {
    3.  
    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):
    1.  
    2. @Override
    3. public void onStart() {
    4.      getEventProcessor().addListener(this);
    5. }
    6.  
    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):
    1.  
    2. @Override
    3. public void onItemAdded(ItemEvent event) {
    4.     System.out.println("Item Added: " + event.getItem() + " (" + event.getQuantityChange() + ")");
    5. }
    6.  
    7. @Override
    8. public void onItemRemoved(ItemEvent event) {
    9.     System.out.println("Item Removed: " + event.getItem() + " (" + event.getQuantityChange() + ")");
    10. }
    11.  
     
  2. Aidden

    Aidden The better executive ;)

    Joined:
    Dec 3, 2013
    Messages:
    3,018
    Likes Received:
    599
    is event.getItem() returning the items name? Nice job by the way :)
     
  3. Cloud

    Cloud The Developer

    Joined:
    Jul 28, 2013
    Messages:
    2,300
    Likes Received:
    729
    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. Aidden

    Aidden The better executive ;)

    Joined:
    Dec 3, 2013
    Messages:
    3,018
    Likes Received:
    599
    Ah okay, it's just the way you were printing it out made it look like it was a value not an object
     
  5. Cloud

    Cloud The Developer

    Joined:
    Jul 28, 2013
    Messages:
    2,300
    Likes Received:
    729
    Well SpriteItem has toString overridden so I figured it was easier.
     
  6. SlashnHax

    SlashnHax The Chosen One

    Joined:
    Dec 10, 2014
    Messages:
    2,783
    Likes Received:
    738
    getEventProcessor() has been renamed getEventDispatcher() I assume?
     
    Baddest Man on Earth likes this.
  7. Baddest Man on Earth

    Joined:
    Nov 26, 2014
    Messages:
    609
    Likes Received:
    241
    Cloud be slacking.
     
    LucasSousa likes this.

Share This Page

Loading...