Skip to main content

NbtCompound

Struct NbtCompound 

Source
pub struct NbtCompound { /* private fields */ }
Expand description

An unordered collection of named NBT tags.

NbtCompound is the primary structured data container in NBT. It maps string names to typed values and is used for virtually all complex game data: items, entities, block entities, player data, level data.

The wire format encodes each entry as: tag type byte + name (u16-prefixed UTF-8) + payload, terminated by an End tag (type 0, no name, no payload).

Uses IndexMap to preserve insertion order (required for deterministic wire encoding) while providing O(1) lookups instead of O(n) scans.

Implementations§

Source§

impl NbtCompound

Source

pub fn new() -> Self

Creates a new empty compound.

Source

pub fn insert(&mut self, name: impl Into<String>, tag: NbtTag)

Inserts a named tag into the compound.

If a tag with the same name already exists, it is replaced. The insertion order of new keys is preserved.

Source

pub fn get(&self, name: &str) -> Option<&NbtTag>

Returns a reference to the tag with the given name, if it exists.

Source

pub fn iter(&self) -> impl Iterator<Item = (&str, &NbtTag)>

Returns an iterator over all (name, tag) pairs in insertion order.

Source

pub fn len(&self) -> usize

Returns the number of entries in the compound.

Source

pub fn is_empty(&self) -> bool

Returns true if the compound contains no entries.

Source

pub fn contains_key(&self, name: &str) -> bool

Returns true if the compound contains a tag with the given name.

Trait Implementations§

Source§

impl Clone for NbtCompound

Source§

fn clone(&self) -> NbtCompound

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for NbtCompound

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Decode for NbtCompound

Decodes an NbtCompound from network NBT format.

Since Minecraft 1.20.3, network NBT uses a simplified root format: a compound tag type byte (0x0A) followed directly by the compound payload (no root tag name). This differs from the traditional NBT format which includes a root tag name.

Fails if the root tag type is not Compound (0x0A).

Source§

fn decode(buf: &mut &[u8]) -> Result<Self>

Reads the compound type byte, then decodes the compound payload.

Fails with Error::Nbt if the root type is not Compound, or with other errors if the payload is malformed.

Source§

impl Default for NbtCompound

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Encode for NbtCompound

Encodes an NbtCompound as a network NBT root compound.

Since Minecraft 1.20.3, network NBT uses a simplified root format: a compound tag type byte (0x0A) followed directly by the compound payload (no root tag name). This differs from the traditional NBT format which includes a root tag name after the type byte.

This encoder produces the network NBT format used in modern protocol packets (chat components, item stacks, registry data).

Source§

fn encode(&self, buf: &mut Vec<u8>) -> Result<()>

Writes the compound type byte (0x0A) followed by the compound payload.

Source§

impl EncodedSize for NbtCompound

Computes the wire size of a network NBT root compound.

Includes the compound type byte (1) plus the compound payload size (entries + End tag).

Source§

fn encoded_size(&self) -> usize

Returns 1 (type byte) + payload size.

Source§

impl PartialEq for NbtCompound

Source§

fn eq(&self, other: &NbtCompound) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for NbtCompound

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.