Guest Posted May 7, 2016 Posted May 7, 2016 I am trying to attach a custom Capability to an ItemStack if the Item stored in the ItemStack is an instance of my ItemGun. The purpose of the Capability is to not need to access the stackTagCompound every tick while shooting. While I understand how to attach a Capability to an Entity or a TileEntity, I don't get how to attach the Capability to an ItemStack. I know that I need to use the AttachCapabilityEvent but its method addCapability needs an ICapabilityProvider. If I create a new ItemStack of the Item I want to attach the Capability to (which doesn't make sense if I think about it again) or use the ItemStack provided by the event but both ways give me a StackOverflowError (different depending on what method I use). First stacktrace (when creating a new ItemStack: java.lang.StackOverflowError: Updating screen events at java.util.HashMap.putVal(HashMap.java:628) at java.util.HashMap.put(HashMap.java:611) at net.minecraft.nbt.NBTTagCompound.setString(NBTTagCompound.java:134) at net.minecraft.item.ItemStack.writeToNBT(ItemStack.java:187) at net.minecraft.item.ItemStack.serializeNBT(ItemStack.java:1130) at net.minecraft.item.ItemStack.serializeNBT(ItemStack.java:38) at net.minecraftforge.common.capabilities.CapabilityDispatcher.serializeNBT(CapabilityDispatcher.java:101) at net.minecraft.item.ItemStack.writeToNBT(ItemStack.java:196) at net.minecraft.item.ItemStack.serializeNBT(ItemStack.java:1130) at net.minecraft.item.ItemStack.serializeNBT(ItemStack.java:38) at net.minecraftforge.common.capabilities.CapabilityDispatcher.serializeNBT(CapabilityDispatcher.java:101) at net.minecraft.item.ItemStack.writeToNBT(ItemStack.java:196) at net.minecraft.item.ItemStack.serializeNBT(ItemStack.java:1130) at net.minecraft.item.ItemStack.serializeNBT(ItemStack.java:38) continues on and on Second stacktrace (when using the ItemStack provided by the event): java.lang.StackOverflowError: Updating screen events at net.minecraft.item.ItemStack.getItem(ItemStack.java:139) at net.minecraft.item.ItemStack.writeToNBT(ItemStack.java:186) at net.minecraft.item.ItemStack.serializeNBT(ItemStack.java:1130) at net.minecraft.item.ItemStack.serializeNBT(ItemStack.java:38) at net.minecraftforge.common.capabilities.CapabilityDispatcher.serializeNBT(CapabilityDispatcher.java:101) at net.minecraft.item.ItemStack.writeToNBT(ItemStack.java:196) at net.minecraft.item.ItemStack.serializeNBT(ItemStack.java:1130) at net.minecraft.item.ItemStack.serializeNBT(ItemStack.java:38) at net.minecraftforge.common.capabilities.CapabilityDispatcher.serializeNBT(CapabilityDispatcher.java:101) at net.minecraft.item.ItemStack.writeToNBT(ItemStack.java:196) at net.minecraft.item.ItemStack.serializeNBT(ItemStack.java:1130) at net.minecraft.item.ItemStack.serializeNBT(ItemStack.java:38) at net.minecraftforge.common.capabilities.CapabilityDispatcher.serializeNBT(CapabilityDispatcher.java:101) continues on and on This is my EventHandler: https://gist.github.com/XFactHD/af08977f0d6c97dd7a265b617e3bee43 This is my Capability: https://gist.github.com/XFactHD/1ecbc563c6e8432f74f06ae5c7eb88c0 This is the IStorage implementation: https://gist.github.com/XFactHD/2cb4c2da6fd51ae860653ba2821665bf This is the default implementation (unfinished): https://gist.github.com/XFactHD/9c8e57b9d7c110d52bf7a2ea51167e06 Quote
UberAffe Posted May 7, 2016 Posted May 7, 2016 You still need a provider to actually register your capability with forge. Quote Current Project: Armerger Planned mods: Light Drafter | Ore Swords Looking for help getting a mod off the ground? Coding | Textures
Guest Posted May 7, 2016 Posted May 7, 2016 No, this is all you need for the registration: CapabilityManager.INSTANCE.register(IGunHandler.class, new GunStorage<IGunHandler>(), GunContainer.class); Quote
LexManos Posted May 7, 2016 Posted May 7, 2016 Somehow you've registered the itemstack itself as needing to serialize.. itself... hence the infinite loop. Quote I do Forge for free, however the servers to run it arn't free, so anything is appreciated. Consider supporting the team on Patreon
Guest Posted May 7, 2016 Posted May 7, 2016 That's what I thougth, too, while looking at the stacktrace. But that's my problem, I don't get what I should provide to the addCapability method as the provider in this case is the ItemStack provided by the event. I tried to follow the explanation provided on mcforge.readthedocs.io, but they didn't give me the right information for this use case of Capabilities. Can you give me a little hint? Quote
UberAffe Posted May 8, 2016 Posted May 8, 2016 I was wrong about registering it, but I still don't understand why you wouldn't use a provider here. The process for attaching capabilities to all three types is identical. check if the instance has your capability already if not then e#addCapability(stringIdentifier, new Provider(new Capability())) Quote Current Project: Armerger Planned mods: Light Drafter | Ore Swords Looking for help getting a mod off the ground? Coding | Textures
Guest Posted May 8, 2016 Posted May 8, 2016 Yes, but the provider in this case is the ItemStack, which is why I tried it this way. Quote
Guest Posted May 8, 2016 Posted May 8, 2016 I am using the event because there are almost no tutorials on capabilities and even mcforge.readthedocs.io can't tell me what you told me in your reply. Thanks for your tip, I will try it out as soon as possible. Quote
Leviathan143 Posted May 8, 2016 Posted May 8, 2016 Williewillus has a good primer on Capabilities here: https://gist.github.com/williewillus/c8dc2a1e7963b57ef436c699f25a710d Quote
Choonster Posted May 8, 2016 Posted May 8, 2016 I have a few examples of capabilities in my test mod. 1.8.9: API, implementation. 1.9: API, implementation. Quote Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.
Guest Posted May 8, 2016 Posted May 8, 2016 Thanks Choonster, that helped me a lot. I got it to work now. Quote
Recommended Posts
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.