Jump to content

[1.11.2] Capabilities on items in creative tabs

Jay Avery

Recommended Posts

I have in item with a decay capability, which depends on the total world time (the item stores its 'birth time' and then calculates the difference from the current world time to get its level of decay). At the moment the default behaviour is that the items in the creative tabs are all given a 'birth time' of zero, which means they end up all being already rotten if the world has existed for a while.


I just experimented with using getSubItems to set the capability using the client world, but this is null when the creative tabs are worked out. Is there any way for me to set the item's capability data at the point when the creative inventory is opened, or when the item is picked up from it - rather than it being set at startup and never changing?

Edited by Jay Avery
Link to comment
Share on other sites

I made a similar system at some point. While I was not using capabilities the logic should still be the same. Basically every time I need to check the rot I would check if the "birth time" is at 0, and if it is - set it to the current time and wait for the next check. It is farely unlikely that the player would obtain a food item with a birth tme of 0, but you could use -1 as a default value and then you can be sure that the item was spawned in rather than obtained in some other way. If your idea is that the capability can be attached to any item, not just some specific ones you could perform a player inventory iteration once in a while to check for this exact case. If you do it infrequently it will pretty much not matter performance-wise. 

Link to comment
Share on other sites

Oh thanks, that gives me some ideas. I'm not sure one birth time check would work though, because it's checked every time the item is drawn in a GUI (for the durability bar) - so the first time it was checked it would be updated and then subsequently it would have a 'valid' value and not be changed again. But maybe I can add a field to the capability to store whether it's in a creative tab (or somewhere else that needs it to be infinitely fresh), and if that's true then keep updating (or perhaps just ignore) the time.

Link to comment
Share on other sites

Update: I figured out that the tabs are repopulated from scratch whenever the creative inventory is opened, so the approach I first tried is right. I just had to add a null check to skip over the world time when the world doesn't exist yet.

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.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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 Terms of Use.