Jump to content

Sync container inventory


TehStoneMan

Recommended Posts

Hello. I have a custom inventory GUI that includes a special slot for a container item. The GUI also has a couple of slots that I would like to have synced with the contents of the container item, so that the player can place or remove items from the container without changing GUI's.

 

The contents of the container item can already be accessed by placing it into the hotbar, and right-clicking with it in hand, but that would mean needing to remove it from it's special slot first, essentially "equipping" it.

 

Does anybody have any suggestions?

Link to comment
Share on other sites

Super easy

 

As long as you've set up your read/write NBT functions (inventory persists across a save/load boundary) then all you need is this:

 

        public Packet getDescriptionPacket() {
                NBTTagCompound nbtTag = new NBTTagCompound();
                this.writeToNBT(nbtTag);
                return new Packet132TileEntityData(this.xCoord, this.yCoord, this.zCoord, 1, nbtTag);
        }

        public void onDataPacket(INetworkManager net, Packet132TileEntityData packet) {
                readFromNBT(packet.data);
        }

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

If your container Item is kept in sync when using it from the equipped slot, then it should be as simple as using that exact same method for when it's accessed from the custom slot.

 

For example, if you are using the Item's onUpdate method to save the inventory contents, it is not called automatically for custom slots, only vanilla inventory slots.

 

Use Forge's LivingUpdateEvent to check if the entity/player has your custom inventory slot and, if so, get the stack from there and call the onUpdate method for the item.

 

If you don't have your container Item inventory saved and synchronized even just when right-clicked, then you need to go back and get that part working first.

Link to comment
Share on other sites

Well, I seem to have it mostly working now, but there is still one problem I am having.

 

I have a full container item, and an empty one. I can take one out of the "equip" slot, and place the other in, and the "contents" slots work as expected.

 

But if I were to directly swap containers by clicking on the occupied slot with the other container, the GUI doesn't update with the change of contents.

 

Through various tests, I have checked that the inventory slots themselves update, but not the GUI, and any interactions with "contents" slots can result in copying the contents of one container to the other.

 

Closing and re-opening the GUI does update properly, though. Suggestions?

Link to comment
Share on other sites

  • 3 weeks later...

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.