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

When should the Capability system be used?


Recommended Posts

I use a translator, so I'm sorry if my English is unnatural.

Well, as the title says, I'm pretty sure I understand what Capability is, but there are a few things I don't understand.

-Is it correct to understand that Capability is for holding arbitrary data by associating it with an object such as a player or TileEntity?

-In many cases, it seems recommended to use Capability instead of implementing IInventory when creating a TileEntity with an inventory, why? What are the drawbacks of implementing Vanilla's I Inventory and what are the benefits of using Capability?

-Although it overlaps a little with the second question, when implementing Magic Point in Player, of course it is not possible to rewrite the Vanilla Player class, so it is necessary to link the player and Magic Point instance using Capability. I understand that. But should I use Capability for new entity and tile entities as well? Is it even a very simple inheritance of Vanilla's ChestTileEntity?

Link to post
Share on other sites
35 minutes ago, Zemelua said:

-Is it correct to understand that Capability is for holding arbitrary data by associating it with an object such as a player or TileEntity?

That is one use case, yes. Capabilities attached to "foreign objects" such as the player entity using AttachCapabilityEvent can be used to store data associated with that object.

 

36 minutes ago, Zemelua said:

-In many cases, it seems recommended to use Capability instead of implementing IInventory when creating a TileEntity with an inventory, why? What are the drawbacks of implementing Vanilla's I Inventory and what are the benefits of using Capability?

You're referring to IItemHandler here, one capability. The benefits of using capabilities over directly implemented interfaces on TileEntities (or other objects) are mostly decoupling and code-reuse. Instead of implementing the inventory mechanics from scratch every time you simply create a new ItemStackHandler, which is a ready-made inventory implementation. Additionally, you are not forced to implement the interface yourself, getCapability can return whatever instance you want. It could even delegate to some other tile entity nearby, or whatever.

 

38 minutes ago, Zemelua said:

-Although it overlaps a little with the second question, when implementing Magic Point in Player, of course it is not possible to rewrite the Vanilla Player class, so it is necessary to link the player and Magic Point instance using Capability. I understand that. But should I use Capability for new entity and tile entities as well? Is it even a very simple inheritance of Vanilla's ChestTileEntity?

I think I answered this above already.

Link to post
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.

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.



×
×
  • Create New...

Important Information

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