Jump to content

[1.14.4][SOLVED] storing complex sets of data in capability


andGarrett

Recommended Posts

I've been experimenting with storing complex sets of data inside a capability. To be more specific, in my "SaleItemListCapability", I'm trying to store an arraylist of objects that store a string and a double. I'm also storing another arraylist of objects that store a string, integer, and a long. finally the capability stores a long and an integer. this is pretty simple for a Class, but when it comes time to write said information to the NBT format, I run into problems. As far as I know, there should be only one "writeNBT" method for each capability storage class, which means I would need to store all these sets into one NBT. This seems almost possible given the types of data I am trying to store. It seems that ListNBT can only store one type of data per list, so I cant use that for my two arraylists as the each store more than one type. CompoundNBT can do what the ListNBT can't when it comes to storing more than one type, but I can't seem to store either ListNBT or CompoundNBT in a CompoundNBT.

 

am I missing something or should I not be storing such complex sets of data in capabilities?

Edited by andGarrett
Link to comment
Share on other sites

21 minutes ago, andGarrett said:

but I can't seem to store either ListNBT or CompoundNBT in a CompoundNBT.

This is completely possible you have to use the CompoundNBT#put(String, INBT) method. There is no specific method for compounds or lists. Also ListNBT can be a list of CompoundNBT so you can store any and all data complex or simple.

  • Thanks 1

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

You can inspire yourself from existing CapabilityItemHandler

Spoiler

            @Override
            public INBT writeNBT(Capability<IItemHandler> capability, IItemHandler instance, Direction side)
            {
                ListNBT nbtTagList = new ListNBT();
                int size = instance.getSlots();
                for (int i = 0; i < size; i++)
                {
                    ItemStack stack = instance.getStackInSlot(i);
                    if (!stack.isEmpty())
                    {
                        CompoundNBT itemTag = new CompoundNBT();
                        itemTag.putInt("Slot", i);
                        stack.write(itemTag);
                        nbtTagList.add(itemTag);
                    }
                }
                return nbtTagList;
            }

            @Override
            public void readNBT(Capability<IItemHandler> capability, IItemHandler instance, Direction side, INBT base)
            {
                if (!(instance instanceof IItemHandlerModifiable))
                    throw new RuntimeException("IItemHandler instance does not implement IItemHandlerModifiable");
                IItemHandlerModifiable itemHandlerModifiable = (IItemHandlerModifiable) instance;
                ListNBT tagList = (ListNBT) base;
                for (int i = 0; i < tagList.size(); i++)
                {
                    CompoundNBT itemTags = tagList.getCompound(i);
                    int j = itemTags.getInt("Slot");

                    if (j >= 0 && j < instance.getSlots())
                    {
                        itemHandlerModifiable.setStackInSlot(j, ItemStack.read(itemTags));
                    }
                }
            }
 
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.