Jump to content

[1.11.2] Items with Inventories: Persistence (Solved)


BotanicNitro

Recommended Posts

After banging my head into a wall for a few days, I've come here to ask for some assistance in a project of mine. Up-to-date documentation and best practices are often difficult to find, so please correct me if I've made terrible blunders in my code - especially if I'm terribly over-complicating things.

 

My main goals:
1. Create an item that contains data and an inventory.
2. Create a GUI that allows a user to swap items and see data.
3. Persist instance data while the given ItemStack exists.

 

My approach so far:
1. Create a custom Capability (impl, storage, provider) implementing IInventory.
2. Use initCapabilities to return my custom provider.
3. Trigger player.openGui() on item right click.

 

My issue(s) with this approach:
1. The inventory will wipe itself occasionally during play, and consistently upon reloading the world.
2. Bizarrely, the numeric data persists correctly.

 

Additional questions:
- As I understand it, IInventory is deprecated. Is there a better way to meet my goals without it?

 

Notes:
- The issue seems potentially related to my storage of slots in an NBTTagList. The first minimum-impl I was going to submit used a raw ItemStack for internal storage and a lone NBTTagCompound for serialization, and did not exhibit the noted issues. My actual implementation is much like the one now present.

 

Linked is a minimum implementation that exhibits these issues.

 

Main Class
Capability
Capability: Impl
Capability: Provider
Capability: Storage
Example Container
Example GuiContainer
Example GuiHandler
Example Item

 

2017-07-01 11:45

Capability (2)

Capability: Impl (2)

Capability: Provider (2)

Capability: Storage (2)

Edited by BotanicNitro
Change title
Link to comment
Share on other sites

You should be using IItemHandler, Forge's replacement for IInventory. You can use or extend ItemStackHandler, the standard IItemHandler implementation.

 

ItemStackHandler implements INBTSerializable, you should use those methods to read it from/write it to NBT. Alternatively, use the IItemHandler capability's IStorage instance.

 

I'm not sure exactly why your code isn't working, but I noticed that you're comparing the ItemStack to ItemStack.EMPTY directly; don't do this. Use ItemStack#isEmpty to check if an ItemStack is empty, ItemStack.EMPTY is just one possible empty ItemStack.

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.

Link to comment
Share on other sites

7 hours ago, Choonster said:

You should be using IItemHandler, Forge's replacement for IInventory. You can use or extend ItemStackHandler, the standard IItemHandler implementation.

 

ItemStackHandler implements INBTSerializable, you should use those methods to read it from/write it to NBT. Alternatively, use the IItemHandler capability's IStorage instance.

 

I'm not sure exactly why your code isn't working, but I noticed that you're comparing the ItemStack to ItemStack.EMPTY directly; don't do this. Use ItemStack#isEmpty to check if an ItemStack is empty, ItemStack.EMPTY is just one possible empty ItemStack.

 

  1. I initially started out extending IItemHandler to manage my inventory, but Slot wants an instance of a class that extends IInventory. How would I get a GUI working with IItemHandler? I would prefer not to extend ItemStackHandler, as I wish to store other data that will be related to this inventory, but I'm not too stubborn to consider it if it becomes the only viable option.
  2. My own inventory serialization code works extremely similarly to IItemHandler's. However, I use an array instead of a list. I will do further testing to see if following more closely with that structure makes a difference.
  3. I will review and revise my test code where necessary to remove this issue, then upload new versions of each afflicted file after further testing.
Edited by BotanicNitro
Link to comment
Share on other sites

36 minutes ago, BotanicNitro said:

 

  1. I initially started out extending IItemHandler to manage my inventory, but Slot wants an instance of a class that extends IInventory. How would I get a GUI working with IItemHandler? I would prefer not to extend ItemStackHandler, as I wish to store other data that will be related to this inventory, but I'm not too stubborn to consider it if it becomes the only viable option.

Forge has a Slot implementation for IItemHandler inventories - net.minecraftforge.items.SlotItemHandler.

Link to comment
Share on other sites

18 minutes ago, Jay Avery said:

Forge has a Slot implementation for IItemHandler inventories - net.minecraftforge.items.SlotItemHandler.

That's very good to know, thank you.

 

8 hours ago, Choonster said:

You should be using IItemHandler, Forge's replacement for IInventory. You can use or extend ItemStackHandler, the standard IItemHandler implementation.

 

ItemStackHandler implements INBTSerializable, you should use those methods to read it from/write it to NBT. Alternatively, use the IItemHandler capability's IStorage instance.

 

I'm not sure exactly why your code isn't working, but I noticed that you're comparing the ItemStack to ItemStack.EMPTY directly; don't do this. Use ItemStack#isEmpty to check if an ItemStack is empty, ItemStack.EMPTY is just one possible empty ItemStack.

I found that converting my test code to use a similar structure did, in fact, solve the issue with persistence.

Additionally, I found an error in my main codebase that, when fixed, also fixed it there.

If I encounter further issues with persistence in the future, I will be sure to return.

 

The original post has been updated to include the new files.

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