nbeet logo

nbeet

An NBT encoder and decoder for gleam

Package Version Hex Docs

About

nbeet is a gleam package for all your NBT needs. For those that don’t know NBT (Named Binary Tag) is a file format utilized by the game Minecraft for storing information. This package focuses on encoding and decoding NBT data and has syntax inspired by gleam_json and bison packages.

Installation

Add nbeet to your Gleam project.

gleam add nbeet

Usage

Encoding

import nbeet/nbt

pub fn encode_truth() -> Result(BitArray, Nil) {
  let nbt =
    nbt.root(
      "in beet we",
      nbt.compound([#("trust", nbt.byte(1)), #("must", nbt.string("true")), ])
    )
  nbt.java_encode(nbt)
}

Decoding

import gleam/dynamic/decode
import gleam/result
import nbeet/nbt

pub type InBeetWe {
  InBeetWe(trust: Int, must: String)
}

fn truth_decoder() {
  use trust <- decode.field("trust", decode.int)
  use must <- decode.field("must", decode.string)
  decode.success(InBeetWe(trust, must))
}

fn decode_truth(bit_array: BitArray) -> Result(InBeetWe, Nil) {
  let decoder = truth_decoder()
  use #(_, in_beet_we) <- result.try(nbt.java_decode(bit_array, decoder))
  Ok(in_beet_we)
}
Search Document