Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[SOLVED] Upgrading from IInventory to Capabilities - IInventoryChangedListener?


logalu
 Share

Recommended Posts

I've been refactoring and upgrading one of my mods, and one of the changes I wanted to make was to move from the old IInventory to the newer capabilities system. I must admit that I've been having a hard time grasping the new concepts, but I think I've managed to at least get the basics related to my use-case. That is, I have an entity which has an associated inventory, that can be accesed through a GUI when right-clicking such entity. Given this simple goal, I decided to use the provided ItemStackHandler implementation of IItemHandler, and I've sucessfully done the required changes for the core things/GUI to work (overridenhasCapability/getCapability, changed Slot to SlotItemHandler in the Container, persistency with NBT, ...).

 

However, I'm now facing a problem. The entity's inventory is not a generic storage, but more of a group of specific slots. That is, slot # can only hold one kind of item, and the entity must update some of its variables depending on the slot being filled or not. In the IInventory version, I had the Entity class implement the IInventoryChangedListener, and then I performed the corresponding checks and updates in the onInventoryChanged() method whenever the IInventory was modified. Now with capabilities I'm unsure of the approach to take. The ItemStackHandler class does contain a onContentsChanged() method that gets called on inventory update, but overriding it doesn't make sense right now as I'd need to access the entity's update methods from the ItemStackHandler class, which does not have access to the entity (or does it somehow?)

 

The possible solution I see right now would be to create my own implementation of IItemHandler, very similar to ItemStackHandler but taking the entity as an input parameter so I can then use it to call the entitiy's update methods in the onContentsChanged() call. But I feel this ain't the right approach, or is it? Is there any listener similar to IInventoryChangedListener in the new capability system, or am I missing anything else in general?

 

Thanks in advance!

Edited by logalu
Marked as solved
Link to comment
Share on other sites

6 minutes ago, logalu said:

The possible solution I see right now would be to create my own implementation of IItemHandler, very similar to ItemStackHandler but taking the entity as an input parameter so I can then use it to call the entitiy's update methods in the onContentsChanged() call.

You can just subclass ItemStackHandler. Alternatively you can make your own extension of ItemStackHandler that maintains a list of listeners and register your entity as a listener.

But in general the idea is to just make an anonymous subclass in your entity:

class MyEntity {

  private IItemHandler handler = new ItemStackHandler(4) {
    @Override
    protected void onContentsChanged(int slot) {
      MyEntity.this.onChanged(slot)
    }
  }
  
  private void onChanged(int slot) {
   // do stuff 
  }

}

 

  • Thanks 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.   Restore formatting

  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.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.