andGarrett Posted September 25, 2019 Share Posted September 25, 2019 (edited) 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 September 25, 2019 by andGarrett Quote Link to comment Share on other sites More sharing options...
Animefan8888 Posted September 25, 2019 Share Posted September 25, 2019 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. 1 Quote 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 More sharing options...
Yanny7 Posted September 25, 2019 Share Posted September 25, 2019 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)); } } } Quote Link to comment Share on other sites More sharing options...
andGarrett Posted September 25, 2019 Author Share Posted September 25, 2019 (edited) 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 September 25, 2019 by andGarrett Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.