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

Right Way To Add Arbitrary Data To PlayerEntities

Recommended Posts

I want to have a field or some attribute to PlayerEntity that indicates whether the player is pressing a key.

I've tried DataManagers but the id value overlap makes me think that is the wrong way to do it on top of causing exceptions.

I've also tried Lists and such to store players that did but sidedness and thread safety make me think thats also the wrong way.

My question is how do I add this boolean attribute in such a way that avoids sidedness and is the proper way?



Link to post
Share on other sites


I do wonder though, why are they called Capabilities? What does that mean in this context? I would imagine it's the ability for an object to do something, which then I'm not sure how that translates to checking if it is doing it, but then there are types and also "facing" which I don't think is like in world directions. 

Edited by Iron1601
Link to post
Share on other sites
5 minutes ago, Iron1601 said:

I do wonder though, why are they called Capabilities?

Because they are originally intended to support "this object can do X"-scenarios. For example: "this tile entity has an inventory" (or in capability-speak: "this tile entity has the inventory capability").

For example, to get an object's inventory capability you call thing.getCapability(ITEM_HANDLER_CAPABILITY, null). This gives you a LazyOptional, which will either be present (thing has an inventory) or not (thing doesn't have an inventory). The facing can be used to convey additional info. For blocks it means the actual facing (a furnace exposes a different inventory on the top than on the bottom), for other things it could mean all kinds of things (entities use sides to give access to separate inventories for just armor or just the held item).


Additionally, you can also use AttachCapabilitiesEvent to attach capabilities to "foreign objects". What you attach here is not really a capability, but a capability provider ("something that can have capabilities") - the capabilities given by this provider will also be exposed by the "foreign object". This provider is special, in that it can also save data, which will be attached to the "foreign object". This whole mechanism allows you to:

  • Write your own capability which stores the data you need.
  • Write your own provider which manages serialization of that data and exposes the capability.
  • Attach that provider to all players using AttachCapabilitiesEvent - players will now store your data.
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.

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.