Jump to content

[1.18.2] Need help syncing IItemHandler Capability to client


NindyBun

Recommended Posts

So previously on 1.16.5 I was using the ItemHandler Capability to read and write the nbt data for the sharetags. But in 1.18.2 Forge doesn't have those methods anymore so I simply copied it in. Obviously, it doesn't work since when I try to get the contents of the itemhandler in my item so that I can render the item on the screen and the stack size, the contained itemstacks don't update until I open the item's inventory. So how can I update the item on the client whenever the itemhandler has changes?

I need to get the inventory in BulletCountRender.java and BulletRadialMenu.java

Item Class: AbstractPouch.java

@Override
    public void readShareTag(ItemStack stack, @Nullable CompoundTag nbt) {
        super.readShareTag(stack, nbt);
        LazyOptional<IItemHandler> optional = AbstractPouch.getData(stack).getOptional();
        if (optional.isPresent()){
            IItemHandler handler = optional.resolve().get();
            if (!(handler instanceof IItemHandlerModifiable))
                throw new RuntimeException("IItemHandler instance does not implement IItemHandlerModifiable");
            IItemHandlerModifiable itemHandlerModifiable = (IItemHandlerModifiable) handler;
            ListTag tagList = nbt.getList("ClientInventory", Tag.TAG_COMPOUND);
            for (int i = 0; i < tagList.size(); i++)
            {
                CompoundTag itemTags = tagList.getCompound(i);
                int j = itemTags.getInt("Slot");

                if (j >= 0 && j < handler.getSlots())
                {
                    itemHandlerModifiable.setStackInSlot(j, ItemStack.of(itemTags));
                }
            }
        }
    }

    @Nullable
    @Override
    public CompoundTag getShareTag(ItemStack stack) {
        LazyOptional<IItemHandler> optional = AbstractPouch.getData(stack).getOptional();
        CompoundTag tag = super.getShareTag(stack);
        if (optional.isPresent()){
            IItemHandler handler = optional.resolve().get();
            ListTag nbtTagList = new ListTag();
            int size = handler.getSlots();
            for (int i = 0; i < size; i++)
            {
                ItemStack stack1 = handler.getStackInSlot(i);
                if (!stack1.isEmpty())
                {
                    CompoundTag itemTag = new CompoundTag();
                    itemTag.putInt("Slot", i);
                    stack1.save(itemTag);
                    nbtTagList.add(itemTag);
                }
            }
            tag.put("ClientInventory", nbtTagList);
        }

        return tag;
    }
Edited by NindyBun
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.



×
×
  • Create New...

Important Information

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