Giter Site home page Giter Site logo

prismarine-nbt's Introduction

Prismarine-NBT

NPM version Build Status Discord Try it on gitpod

Prismarine-NBT is a JavaScript parser and serializer for NBT archives. It supports big, little, and little-varint encoded NBT files.

Usage

as a async promise

const fs = require('fs/promises')
const nbt = require('prismarine-nbt')

async function main(file) {
  const buffer = await fs.readFile(file)
  const { parsed, type } = await nbt.parse(buffer)
  console.log('JSON serialized', JSON.stringify(result, null, 2))
  fs.createWriteStream('file.nbt').write(nbt.writeUncompressed(result, type)) // Write it back 
}

main('file.nbt')

as a callback

var fs = require('fs'),
    nbt = require('prismarine-nbt');

fs.readFile('bigtest.nbt', function(error, data) {
    if (error) throw error;

    nbt.parse(data, function(error, data) {
        console.log(data.value.stringTest.value);
        console.log(data.value['nested compound test'].value);
    });
});

If the data is gzipped, it is automatically decompressed, for the buffer see metadata.buffer

API

parse(data, [format]): Promise<{ parsed, type, metadata: { size, buffer? } }>

parse(data, [format,] callback)

Takes an optionally compressed data buffer and reads the nbt data.

If the endian format is known, it can be specified as 'big', 'little' or 'littleVarint'. If not specified, the library will try to sequentially load as big, little and little varint until the parse is successful. The deduced type is returned as type.

Minecraft Java Edition uses big-endian format, and Bedrock uses little-endian.

writeUncompressed(value, format='big')

Returns a buffer with a serialized nbt value.

parseUncompressed(data, format='big')

Takes a buffer data and returns a parsed nbt value.

simplify(nbt)

Returns a simplified nbt representation : keep only the value to remove one level. This loses the types so you cannot use the resulting representation to write it back to nbt.

protos : { big, little, littleVarint }

Provides compiled protodef instances used to parse and serialize nbt

proto

Provide the big-endian protodef instance used to parse and serialize nbt.

protoLE

Provide the little-endian protodef instance used to parse and serialize little endian nbt.

builder

Provides a way to build complex nbt structures simply:

const nbt = require('prismarine-nbt')
const tag = nbt.comp({
  Air: nbt.short(300),
  Armor: nbt.list(nbt.comp([
    { Count: nbt.byte(0), Damage: nbt.short(0), Name: nbt.string('a') },
    { Count: nbt.byte(0), Damage: nbt.short(0), Name: nbt.string('b') },
    { Count: nbt.byte(0), Damage: nbt.short(0), Name: nbt.string('c') }
  ]))
})
nbt.writeUncompressed(tag) // now do something with this nbt buffer...

See index.d.ts for methods

Browser usage

For webpack usage, see an example configuration here.

For a web bundle with browserify (after you ran npm install prismarine-nbt in your project):

npx browserify -r prismarine-nbt -r buffer -o pnbt.js
<script src="./pnbt.js"></script>
<script>
  const nbt = require('prismarine-nbt')
  const { Buffer } = require('buffer')
  fetch('test.nbt').then(resp => resp.arrayBuffer())
    .then(buf => nbt.parse(Buffer.from(buf))).then(console.log)
</script>

prismarine-nbt's People

Contributors

rom1504 avatar sjmulder avatar roblabla avatar deathcap avatar dependabot-preview[bot] avatar extremeheat avatar u9g avatar dependabot[bot] avatar karang avatar luminess avatar groobledierne avatar darkflame72 avatar malekashkar avatar mcjeffr avatar kelly-dance avatar mrzillagold avatar thenuclearnexus avatar umrninside avatar

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.