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.{byte, compound, nbt, string}

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

Decoding

import gleam/dynamic
import gleam/result
import nbeet

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

fn decode_truth(nbt: BitArray) -> Result(InBeetWe, Nil) {
  let decoder =
    decode.into({
      use trust <- decode.parameter
      use must <- decode.parameter
      InBeetWe(trust, must)
    })
    |> decode.field("trust", decode.int)
    |> decode.field("must", decode.string)
  use #(name, in_beet_we) <- result.try(nbeet.java_decode(nbt, decoder))
  Ok(in_beet_we)
}
Search Document