the job of the CapabilityProvider is too handle and expose all the capabilities for an object. In the getCapabilities method, it will check for which capability that is being requested, and provide it.
in the Item class, the point of the initCapabilities method, is just to say which CapabilityProvider will handle it's data.
so if what you need is an Inventory, you'd have an "InventoryProvider", which when the ITEM_STACK_HANDLER capability was requested, would return the Inventory. and in the item which has this inventory data, it would return this InventoryProvider in initCapabilities.
Here's an example:
public class BackpackCapabilityProvider implements ICapabilitySerializable<INBT> {
// BackpackItemStackHandler is just an extension of ItemStackHandler which makes sure no Backpack can be put in the inventory
private BackpackItemStackHandler backpackItemStackHandler;
// This instantiates the Inventory only when it is first requested, and then caches it
@Nonnull
private BackpackItemStackHandler getCachedInventory() {
if (backpackItemStackHandler == null) backpackItemStackHandler = new BackpackItemStackHandler();
return backpackItemStackHandler;
}
private final LazyOptional<IItemHandler> lazyInventory = LazyOptional.of(this::getCachedInventory);
// Provides the Inventory
@Nonnull @Override
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, Direction side) {
if (cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) return (LazyOptional<T>)(lazyInventory);
// If we needed to provide more than one capability, we'd simply add another check:
// if (cap == SOME_OTHER_CAPABILITY) return (otherCapability)
return LazyOptional.empty();
}
// Saves the Inventory data to an NBT tag, so that it can be saved to disk
@Override
public INBT serializeNBT() {
return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.writeNBT(getCachedInventory(), null);
}
// Reads the Inventory data from an NBT tag that was saved to disk
@Override
public void deserializeNBT(INBT nbt) {
CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.readNBT(getCachedInventory(), null, nbt);
}
}