Jump to content

Question about itemstack capabilities of crafting results


Tavi007

Recommended Posts

Hey,

I stumbled across a bug in my mod, when using the workbench (and presumably any other sort of crafting station). I have an itemstack capability, that gets its default value through json files. So usually when the AttachCapabilityEvent<ItemStack> triggers, I can succesfully add these values to the item. However when the result slot in the workbench container updates to a new item, the attach event does not trigger and the capability stays empty.

I added a hook using ItemCraftedEvent, so the values are being set, when the crafting results gets picked up, but I also added some costum tooltips, that displays the capability data. So currently it will not show any of my data, when the mouse hovers above the crafting result. Only after picking it up, the correct data can be seen. This might confuse the player, so I would like to know, if there is method for setting the default values the moment the resulting stack is generated.

Also I fear, that the ItemCraftedEvent does not trigger for machines from other mods. This would mean, that it would be possible to create itemstacks with incorrect capability data.

I hope my explanation is clear enough. If not, I can add some pictures to show you, what I mean. :)

Link to comment
Share on other sites

just two maps and two strings. I use json files, so anyone can override them with datapacks. Basically each item, that should have default values, also has a corresponding json file. (if an item does not have a corresponding json file, then it will get default default values.)

 

The information in the files are loaded into a Map<ResourceLocation, DataFromJson> once when the server starts, so i can get them whenever i need them. When the attach event (or the item craft event) trigger i resolve the resourceLocation, get the DataFromJson and then set the itemstack capability. As long as the Map doesn't get humongous, this method shouldn't be a performance issure.

Link to comment
Share on other sites

Hmm, I already thought, that it would work like this. I guess, that the itemstack in the result slot gets created as an air item. Therefor the attachCapability event gets triggered with an air item and the default default values will be used. So what happens with the result stack, when I place the last iron ingot for crafting an iron sword and the result slot gets updated? And how could I detect that exact moment?

Link to comment
Share on other sites

Ah, so whenever I craft an item, the values, that were set in attachCapability will be overriden by the desirialized cpability from the old stack (which was an air item). That explains the bug.

I could change the constructor of my capabilities to use ItemStack and then read from my Map<ResourceLocation, DataFromJson> in there, but I don't see, how this will solve the bug.

Link to comment
Share on other sites

Okay, got it. I think, I can use the AttackDataAPI and DefenseDataAPI classes for this, because I (and anyone else) should always use these to interact with my capabilities.Or would this be bad practice?

Thank you for your help anyway :)

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.

Guest
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.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.