Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.8.9] Attach custom Capability to an ItemStack


XFactHD
 Share

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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

 Share



×
×
  • Create New...

Important Information

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