nbt #
nbt
A standalone V library for Minecraft Bedrock Edition's network (little-endian / varint) NBT format.
Format
Network NBT encoding:
| Tag | Encoding |
|---|---|
| Byte (1) | 1 byte |
| Short (2) | LE int16 |
| Int (3) | zigzag varint (varint32) |
| Long (4) | zigzag varint (varint64) |
| Float (5) | LE f32 |
| Double (6) | LE f64 |
| ByteArray (7) | zigzag varint length + bytes |
| String (8) | varuint32 length + UTF-8 bytes |
| List (9) | element type byte + zigzag varint length + elements |
| Compound (10) | (type byte, name, payload)* + TAG_End |
| IntArray (11) | zigzag varint length + zigzag varints |
| LongArray (12) | zigzag varint length + zigzag varints |
Names are always written as String (with a varuint32 length prefix).
API
import nbt
// Build
mut c := nbt.new_compound()
c.set('name', nbt.Tag('Steve'))
c.set('health', nbt.Tag(i16(20)))
root := nbt.RootTag{ name: '', tag: nbt.Tag(c) }
// Encode
data := nbt.encode(root)
// Decode (read = number of bytes consumed; for advancing a position within a stream)
res := nbt.decode(data)!
compound := res.root.tag as nbt.Compound
health := compound.get('health') or { nbt.Tag(i16(0)) } as i16
Tag is a sum type with the following variants: i8, i16, i32, i64, f32, f64, string, ByteArray, List, Compound, IntArray, LongArray.
Build
v -shared -skip-unused .
Constants #
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) !DecodeResult
fn encode #
fn encode(root RootTag) []u8
fn new_compound #
fn new_compound() Compound
fn tag_id #
fn tag_id(t Tag) u8
type Tag #
type Tag = ByteArray
| Compound
| IntArray
| List
| LongArray
| f32
| f64
| i16
| i32
| i64
| i8
| string
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 RootTag #
struct RootTag {
pub mut:
name string
tag Tag
}