Coder Social home page Coder Social logo

nbt-ts's Introduction

nbt-ts

⚠️ This library has not been maintained for some time. Please use it with caution or switch to another implementation!

npm downloads

An easy to use encoder and decoder for the NBT format.

NBT compound tags are represented as plain JavaScript objects. The Byte, Short, Int and Float number types are wrapped in custom classes since JavaScript does not support them directly.

Node 10.4 or higher is required for BigInts, which are used to represent 64 bit integers.

Usage

const { encode, decode, Byte, Short, Int, Float } = require("nbt-ts")

const buffer = encode("root", {
    byte: new Byte(-1),
    short: new Short(65535),
    int: new Int(-2147483648),
    long: 0x7fffffffffffffffn,
    float: new Float(0.75),
    double: 0.1 + 0.2,
    string: "Hello world",
    list: ["item 1", "item 2"],
    compound: {
        byteArray: Buffer.from([0x80, 0x40, 0x20]),
        // Int8Array does work here too
        intArray: new Int32Array([1, 2, 3, 4]),
        longArray: new BigInt64Array([1n, 2n, 3n, 4n])
    },
})

decode(Buffer.from("02000973686F7274546573747FFF", "hex"))
// → { name: 'shortTest', value: Short { value: 32767 }, length: 14 }

// Encode unnamed tag
encode(null, "a")
// → <Buffer 08 00 01 61>

// Decode unnamed tag
decode(Buffer.from("08000161", "hex"), { unnamed: true })
// → { name: null, value: 'a', length: 4 }

Note that the encode function accepts both unsigned numbers such as 255 and signed numbers like -1 which are essentially the same in the case of a 8 bit integer. However when decoded, they will always have the signed representation. If you want to convert a number to the unsigned representation, you might do something like this:

value & 0xff   // for bytes
value & 0xffff // for shorts
value >>> 0    // for ints
BigInt.asUintN(64, value) // for longs
// or
value & 0xffffffffffffffffn

SNBT

The NBT format also has a more user-friendly variant in plain text. This format is referred to as SNBT, short for stringified NBT. Here are all the types represented in SNBT:

{
    byte: 1b, short: 1s, int: 1, long: 1l,
    float: 0.5f, double: 0.5,
    string: "Hello world",
    list: [{}, {}],
    compound: {
        byteArray: [B; 128, 64, 32],
        intArray: [I; 1, 2, 3, 4],
        longArray: [L; 1, 2, 3, 4]
    }
}

Here is an example how you can stringify or parse SNBT:

const { stringify, parse } = require("nbt-ts")

const tag = parse(`{'Flying' :1b , unquoted: hello} `)
// → { Flying: Byte { value: 1 }, unquoted: 'hello' }

stringify(tag)
// → '{Flying:1b,unquoted:"hello"}'

Related projects

nbt-ts's People

Contributors

hateofhades avatar janispritzkau avatar me4502 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

nbt-ts's Issues

ReferenceError: Buffer is not defined

I'm trying to use this library in my Svelte app, but I keep getting the error Uncaught (in promise) ReferenceError: Buffer is not defined.

From my research it seems Buffer is a node-only thing, and I can't use that.

What could I do to make this work?

SNBT parser fails to skip `\r` and `\t` chars.

Incorrect escape string serialization

const origin = '"'
const str = JSON.stringify(origin)
const str2 = MojangSON.parse(MojangSON.stringify({ str })).str

console.log(str === str2) // false
console.log(origin === JSON.parse(str)) // true
console.log(str) // "\""
console.log(str2) // """

And this works:

const escapeJSON = text => text.replace(/"/g, '\\u0022').replace(/'/g, '\\u0027')

const str = JSON.stringify(escapeJSON(origin))
const str2 = MojangSON.parse(MojangSON.stringify({ str })).str

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.