npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

eznbt

v1.1.3

Published

Easy Minecraft NBT reading and writing

Downloads

4

Readme

eznbt

Easy Minecraft NBT reading and writing

Prerequisites

Make sure to use a node version that supports BigInts, they're used for the Long NBT Tag

Usage

Pretty much all you need to know is that PascalCase members of eznbt are classes that can be used for both reading and writing. Its constructors accept an object containing either a value or buffer property, from which the class is then constructed. The instance will contain a value and buffer property.

For ease of use lowercase members of eznbt can be used to construct classes like this: int(5) (instead of new Int({ value: 5 }))

Writing stuff

import NBT from 'eznbt'
const { NBTString, string, rootcompound } = NBT

/* NBT files are usually implicitly in a compound. If this is the case you should use
 * a named RootCompound. You name it by providing a [Symbol.for('NBTRootTagName')] string.
 */
const myCompound = rootcompound({
  [Symbol.for('NBTRootTagName')]: 'rootTagName',
  myStr: new NBTString({ value: 'hello' }),
  // is equivalent to
  myStr2: new NBT.String({ value: 'hello' }),
  // is equivalent to
  myStr3: string('hello'),
  // is equivalent to
  myStr4: 'hello'
})

myCompound.buffer // ready to send to your client/server

Reading stuff

import NBT from 'eznbt'
const { Compound } = NBT

const myBuffer = ... // a buffer you received. it is an nbt compound tag (as always)
const myCompound = new Compound({ buffer: myBuffer })
myCompound.value // yields the compound as an object. children are NBT Tag instances
myCompound.json // yields the compound as an object. children are JS types (number, string, bigint, etc.)

Reading and writing lists

Lists are special because they have one set type, which you will first have to pass to the List or list function.

import NBT from 'eznbt'
const { List, list, NBTString } = NBT


const myList = list(NBTString)('hello', 'world!') // creates a string-list

// When the type of the list is unknown, it can only be used to read stuff:
const { value } = new (List())({ buffer: myList.buffer })

Compound shorthands

The compound tag comes with a few handy shorthands

import NBT from 'eznbt'
const { int, compound } = NBT

const myCompound = compound({
  myList: [int(5), int(6)], // creates an int-list
  myLong: 5n, // creates a long
  myStr: 'hello world!', // creates a string
  // creates a compound
  myCompound: {
    myLongList: [5n, 6n, 7n] // creates a long-list
  }
})

Compound vs RootCompound

Since generally all NBT files are implicitly in a compound tag, you should probably always use RootCompound for the outside object, and Compound otherwise

// RootCompound.json vs Compound.json

// RootCompound.json will put all values as a property of the root compound's name
rootcompound({ a: 1, b: 2, [Symbol.for('NBTRootTagName')]: 'hello!' }).json
/** {
  *   'hello!': {
  *     a: 1,
  *     b: 2
  *   }
  * }
  */

// Compound doesn't have a name so it won't be named
compound({ a: 1, b: 2 }).json
/** {
  *   a: 1,
  *   b: 2
  * }
  */

Typescript

This package has been made using Typescript and ships with type declarations.