Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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

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.

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.

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));
                    }
                }
            }
 
  • Author

it's odd to see something in the Minecraft code like "nbtTagList". it's like saying "named binary tag tag list." like saying "ATM machine".

Edited by andGarrett

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.