1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Resource How to setup and use an InventoryListener

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

  1. Cloud

    Cloud Engineer

    Joined:
    Jul 28, 2013
    Messages:
    2,777
    Likes Received:
    1,124
    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 Author of MaxiBots

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

    Cloud Engineer

    Joined:
    Jul 28, 2013
    Messages:
    2,777
    Likes Received:
    1,124
    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 Author of MaxiBots

    Joined:
    Dec 3, 2013
    Messages:
    6,482
    Likes Received:
    990
    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 Engineer

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

    Joined:
    Dec 10, 2014
    Messages:
    3,198
    Likes Received:
    1,041
    getEventProcessor() has been renamed getEventDispatcher() I assume?
     
    Baddest Man on Earth likes this.
  7. Baddest Man on Earth

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

Share This Page

Loading...