You can attach an inventory to an item through the capability system. You'll need an ICapabilityProvider implementation to store and provide the inventory, you can then create and return an instance of this from Item#initCapabilities.
Rollback Post to RevisionRollBack
Chisel Facades: For all your decorative pipe-hiding needs.
Please don't PM me to ask for help or to join your mod development team. Asking your question in a public thread preserves it for people who are having the same problem in the future. I'm not interested in developing mods with people.
You can attach an inventory to an item through the capability system. You'll need an ICapabilityProvider implementation to store and provide the inventory, you can then create and return an instance of this from Item#initCapabilities.
This is exactly what I'm trying for-- my problem is, I don't know how to set that up. The videos above explain the method for adding it to a tileentity, and this also works for a mob, but items do not share the same NBT data initialization methods. I'm not sure how to initialize the new capabilities-- for an entity you have to add the handler to the writeEntityToNBT (compound.setTag("ItemStackHandler", this.handler.serializeNBT());) and readEntityFromNBT (this.handler.deserializeNBT(compound.getCompoundTag("ItemStackHandler"));)methods, but those methods do not exist for items from what I can see. What I'm stuck on is where that code needs to go, since the methods are completely different. Is initCapabilities the only place I need to initialize it? Does it automatically read and write to NBT from there?
@everyone else: I'm basically trying to apply the method in the video (originally used for a tile entity) to an item, but as I mentioned above, items don't share the same NBT methods as tile entities and entities.
You need to read up more about capabilities. Only the capability itself is read from and written to nbt. In the item you want to get it's capability and retrieve the required data from the capability. All this is described in the link I provided. If you're having problems with capabilities I can help you.
You need to read up more about capabilities. Only the capability itself is read from and written to nbt. In the item you want to get it's capability and retrieve the required data from the capability. All this is described in the link I provided. If you're having problems with capabilities I can help you.
So am I understanding correctly in that the only place I need to assign the capability is from the initCapabilities? That capabilities are inherently written into items, so you don't have to use any sort of read/write NBT methods for items?
I think that you're correct about assigning the capability from item.initCapabilites. However, it seems to me that you still don't quite understand what capabilities do. Capabilities just store data. You'll need to access the capability from your item's code then do what you want with it. In your case it would probably be retrieving the inventory from the capability when needed and saving/adding to the inventory in the capability when an item is stored or whatever you need.
I think that you're correct about assigning the capability from item.initCapabilites. However, it seems to me that you still don't quite understand what capabilities do. Capabilities just store data. You'll need to access the capability from your item's code then do what you want with it. In your case it would probably be retrieving the inventory from the capability when needed and saving/adding to the inventory in the capability when an item is stored or whatever you need.
Yeah, I do get that-- I'm just a little confused because there was additional setup that went into getting capabilities working for the entity.
To load capability data from and save it to NBT, your ICapabilityProvider needs to implement INBTSerializable (or ICapabilitySerializable, a combination of the two interfaces). Forge will automatically call the INBTSerializable methods on your ICapabilityProvider when the object it's attached to (the ItemStack in this case) is loaded from or saved to NBT.
Rollback Post to RevisionRollBack
Chisel Facades: For all your decorative pipe-hiding needs.
Please don't PM me to ask for help or to join your mod development team. Asking your question in a public thread preserves it for people who are having the same problem in the future. I'm not interested in developing mods with people.
To load capability data from and save it to NBT, your ICapabilityProvider needs to implement INBTSerializable (or ICapabilitySerializable, a combination of the two interfaces). Forge will automatically call the INBTSerializable methods on your ICapabilityProvider when the object it's attached to (the ItemStack in this case) is loaded from or saved to NBT.
No, the methods you have in mind are related to storing NBT in the entity. Capabilities' NBT is handled in their own classes.
Alright, that's what I was referring to. If you use an Inventory Handler with an entity, you do have to initialize it in the read and write areas for the entity.
Hi again,
I'm trying to make an item with an inventory, mostly based off this method:
That's all well and good, I've got the basics of that down. But items don't share the same nbt initialization format as entities.
How would I set something like this to work with a custom item?
Thanks!
(also if you know how to help with this, I still haven't figured it out o.o)
The only way I could think of is through nbt.
You can attach an inventory to an item through the capability system. You'll need an ICapabilityProvider implementation to store and provide the inventory, you can then create and return an instance of this from Item#initCapabilities.
Chisel Facades: For all your decorative pipe-hiding needs.
Please don't PM me to ask for help or to join your mod development team. Asking your question in a public thread preserves it for people who are having the same problem in the future. I'm not interested in developing mods with people.
This is exactly what I'm trying for-- my problem is, I don't know how to set that up. The videos above explain the method for adding it to a tileentity, and this also works for a mob, but items do not share the same NBT data initialization methods. I'm not sure how to initialize the new capabilities-- for an entity you have to add the handler to the writeEntityToNBT (compound.setTag("ItemStackHandler", this.handler.serializeNBT());) and readEntityFromNBT (this.handler.deserializeNBT(compound.getCompoundTag("ItemStackHandler"));)methods, but those methods do not exist for items from what I can see. What I'm stuck on is where that code needs to go, since the methods are completely different. Is initCapabilities the only place I need to initialize it? Does it automatically read and write to NBT from there?
@everyone else: I'm basically trying to apply the method in the video (originally used for a tile entity) to an item, but as I mentioned above, items don't share the same NBT methods as tile entities and entities.
You need to read up more about capabilities. Only the capability itself is read from and written to nbt. In the item you want to get it's capability and retrieve the required data from the capability. All this is described in the link I provided. If you're having problems with capabilities I can help you.
So am I understanding correctly in that the only place I need to assign the capability is from the initCapabilities? That capabilities are inherently written into items, so you don't have to use any sort of read/write NBT methods for items?
I think that you're correct about assigning the capability from item.initCapabilites. However, it seems to me that you still don't quite understand what capabilities do. Capabilities just store data. You'll need to access the capability from your item's code then do what you want with it. In your case it would probably be retrieving the inventory from the capability when needed and saving/adding to the inventory in the capability when an item is stored or whatever you need.
Yeah, I do get that-- I'm just a little confused because there was additional setup that went into getting capabilities working for the entity.
To load capability data from and save it to NBT, your ICapabilityProvider needs to implement INBTSerializable (or ICapabilitySerializable, a combination of the two interfaces). Forge will automatically call the INBTSerializable methods on your ICapabilityProvider when the object it's attached to (the ItemStack in this case) is loaded from or saved to NBT.
Chisel Facades: For all your decorative pipe-hiding needs.
Please don't PM me to ask for help or to join your mod development team. Asking your question in a public thread preserves it for people who are having the same problem in the future. I'm not interested in developing mods with people.
I don't think entities require any additional steps to set up. Where did you get that from?
Oh, alright-- thank you.
For NBT data I mean. There's like 3 methods that deal with the NBT, and I can't find their equivalents for items.
No, the methods you have in mind are related to storing NBT in the entity. Capabilities' NBT is handled in their own classes.
Alright, that's what I was referring to. If you use an Inventory Handler with an entity, you do have to initialize it in the read and write areas for the entity.