Posted March 9, 20214 yr 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.
March 9, 20214 yr Author 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.
March 9, 20214 yr Author 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?
March 9, 20214 yr Author 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.
March 9, 20214 yr Author https://github.com/Tavi007/ElementalCombat you have to look in api/attack or api/defense for the capability
March 9, 20214 yr Author 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
March 9, 20214 yr Author Now you lost me. Where am I supposed to lazily initialise the values? In AttackData, in AttackDataCapability or somewhere complelty different?
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.