Jump to content

Recommended Posts

Posted (edited)

Hello,

I have added throwable weapon to my mod and I want to let any entity, that was hit by that weapon to drop it, when it dies. I have created a capability to store all weapons "inside" the entity, but there is some problem with saving the data to NBT anr the game instantly crashes. Is there anything wrong with this code.

Code:

public class SpearsInStorage implements IStorage<ISpearsIn>{
    
    @Override
    public NBTTagCompound writeNBT(Capability<ISpearsIn> capability, ISpearsIn instance, EnumFacing side) 
    {
        NBTTagCompound compound = new NBTTagCompound();
        ArrayList<ItemStack> list = instance.getSpearsIn();
        int n=1;
        for (ItemStack spear:list)
            {
                NBTTagCompound nbt = spear.serializeNBT();
                compound.setTag("Spear"+n, nbt);
                n=n+1;
            }
        compound.setBoolean("HasSpears", true);
        return compound;
    }
    
    @Override
    public void readNBT(Capability<ISpearsIn> capability, ISpearsIn instance, EnumFacing side, NBTBase nbt)
    {
        for (int n=1;n<64; n++)
        {
            try
            {
                NBTTagCompound compound = ((NBTTagCompound)nbt).getCompoundTag("Spear"+n);
                ItemStack spear = new ItemStack(compound);
                instance.addSpear(spear);
            }
            catch (Exception e)
            {
                n=64;
            }
        }
    }

}

I used this tutorial when creating the capability: http://www.planetminecraft.com/blog/forge-tutorial-capability-system/. I have already added a different capability to my mod and it worked without problems.

Thanks for any help.

Edited by fcelon

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.