Nbt Api Plugin Wiki › Guides › Work with Nested NBT Compounds and Lists
Work with Nested NBT Compounds and Lists
Understanding Complex NBT Structures
NBT data isn't always flat—tags can contain other tags (compounds) or ordered collections (lists). The NBT API provides tools to navigate and edit these nested structures safely.
Steps
1. Access the parent compound — Start with the root NBT data of your item or entity.
2. Navigate to nested compounds — Use the API to drill down into child compounds (e.g., display → Name).
3. Work with lists — If a tag contains a list (like Enchantments), iterate through list entries or add new ones using the API.
4. Modify values at any depth — Edit numbers, strings, or tags anywhere in the hierarchy.
5. Rebuild the structure — Ensure all parent compounds are updated and written back together.
Best Practices
- Check if a compound or list exists before accessing it to avoid null errors.
- Use helper functions to safely create missing intermediate compounds.
- When adding to lists, maintain the correct data type for each entry.
- Test deeply nested changes carefully, as errors can corrupt the entire NBT structure.