Jump to content

[1.8.9] Attach custom Capability to an ItemStack


Recommended Posts

Posted

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

Posted

No, this is all you need for the registration: CapabilityManager.INSTANCE.register(IGunHandler.class, new GunStorage<IGunHandler>(), GunContainer.class);

Posted

Somehow you've registered the itemstack itself as needing to serialize.. itself...

hence the infinite loop.

 

I do Forge for free, however the servers to run it arn't free, so anything is appreciated.
Consider supporting the team on Patreon

Posted

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?

Posted

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()))

Current Project: Armerger 

Planned mods: Light Drafter  | Ore Swords

Looking for help getting a mod off the ground? Coding  | Textures

Posted

Yes, but the provider in this case is the ItemStack, which is why I tried it this way.

Posted

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.

Posted

Thanks Choonster, that helped me a lot. I got it to work now.

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.