Skip to content

nbt #

max_depth is how deeply lists and compounds may nest. Decoding recurses once per level, so without it a few hundred kilobytes of nested lists from a client overflow the stack.

Constants #

const max_fixed_string = 65535

max_fixed_string is the longest string, in bytes, the fixed size forms can hold. Their string length is a u16.

const tag_end = u8(0)
const tag_byte = u8(1)
const tag_short = u8(2)
const tag_int = u8(3)
const tag_long = u8(4)
const tag_float = u8(5)
const tag_double = u8(6)
const tag_byte_array = u8(7)
const tag_string = u8(8)
const tag_list = u8(9)
const tag_compound = u8(10)
const tag_int_array = u8(11)
const tag_long_array = u8(12)

fn decode #

fn decode(data []u8, opts Options) !DecodeResult

fn encode #

fn encode(root RootTag, opts Options) []u8

encode writes a root tag. In the fixed size forms a string longer than max_fixed_string can't be written and encode panics on one rather than write NBT that misreads everything after it. encode_checked returns that as an error instead.

fn encode_checked #

fn encode_checked(root RootTag, opts Options) ![]u8

encode_checked writes a root tag, or fails on one the encoding can't hold.

fn new_compound #

fn new_compound() Compound

fn tag_id #

fn tag_id(t Tag) u8

fn Encoding.from #

fn Encoding.from[W](input W) !Encoding

from returns the Encoding value of an integer or of a field name, or an error if there is none.

type Tag #

type Tag = ByteArray
	| Compound
	| IntArray
	| List
	| LongArray
	| f32
	| f64
	| i16
	| i32
	| i64
	| i8
	| string

enum Encoding #

enum Encoding {
	// network_little_endian is what Bedrock sends over the network: ints, longs
	// and lengths are varints.
	network_little_endian
	// little_endian is what Bedrock writes to disk (LevelDB worlds) and hashes
	// block states over: every number has a fixed size.
	little_endian
	// big_endian is the fixed size form Java Edition uses and some Bedrock data
	// files are distributed in.
	big_endian
}

Encoding is the form NBT is written in.

struct ByteArray #

struct ByteArray {
pub mut:
	values []u8
}

struct Compound #

struct Compound {
pub mut:
	values map[string]Tag
}

fn (Compound) get #

fn (c &Compound) get(key string) ?Tag

fn (Compound) set #

fn (mut c Compound) set(key string, value Tag)

struct DecodeResult #

struct DecodeResult {
pub:
	root RootTag
	read int
}

struct IntArray #

struct IntArray {
pub mut:
	values []i32
}

struct List #

struct List {
pub mut:
	element_type u8
	values       []Tag
}

struct LongArray #

struct LongArray {
pub mut:
	values []i64
}

struct Options #

@[params]
struct Options {
pub:
	encoding Encoding = .network_little_endian
}

struct RootTag #

struct RootTag {
pub mut:
	name string
	tag  Tag
}