Yet another Minecraft NBT format file / buffer parser for Node.js.
$ npm install --save mcnbt
First require this package into your code:
const NBT = require('mcnbt');
There're several function for you to parse an NBT structure after you instantiating an object:
loadFromBuffer(buff, callback)
loadFromZlibCompressedBuffer(buff, callback)
loadFromFile(filename, callback)
loadFromZlibCompressedFile(filename, callback)
All those functions's callback should be written like:
function(err) {
//... Your code here
}
For an example:
var nbt = new NBT();
nbt.loadFromZlibCompressedFile('level.dat', function(err) {
if(err) return console.error(err);
console.log(nbt);
});
Two methods for you to write NBT:
writeToCompressedBuffer(callback, [method])
writeToBuffer()
: returns the buffer (sync)writeToCompressedFile(filename, callback, [method])
writeToFile(filename, callback)
method
is an optional parameter that indicates the zlib algorithm. The default value is'gzip'
.
After parsing your NBT, you can get the root tag(s).
keys()
: get the root tag-key(s) into an arraycount()
: root tag(s) countselect(tagname)
: this function will returns you a instance of tag which namedtagname
The tag(s) are instance(s) of
BaseTag
. There're 10 subclasses ofBaseTag
.
All the tag classes's parent class is BaseTag
.
BaseTag
and all of the tags have functions below:
getType()
: get this tag's type name. eg.'TAG_Compound'
getId()
: alias ofgetType
getTypeId()
: get this tag's type id. eg.'TAG_Compound'
's id is10
getName()
: get this tag's namegetValue()
: get this tag's contentsetValue(value)
: set this tag's valuecount()
: get this tag's children count / length (if any)selectAt(idx)
: get this tag's children / char atidx
(if any)select(tagname)
: get this tag's children bytagname
(if any)getAt(idx)
: alias ofselectAt
get(tagname)
: alias ofselect
toJSON()
: convert this tag and all of its children into a JSON object
The value's type is byte (number in js).
No extra method.
The value's type is short (number in js).
No extra method.
The value's type is integer (number in js).
No extra method.
The value's type is long long (bignum in js).
No extra method.
The value's type is float (number in js).
No extra method.
The value's type is double (nbumer in js).
No extra method.
The value's type is array of byte (array of number in js).
There're 5 extra methods:
shift()
unshift(value)
pop()
push(value)
insert(value, position)
The value's type is string.
No extra method.
The value's type is array of any Tag
instances (array of Tag
instances in js).
There're 6 extra methods:
shift()
unshift(value)
pop()
push(value)
insert(value, position)
clean()
The value's type is object contains any Tag
instances.
There're 4 extra methods:
setByName(name, value, [replace])
: setname
child into a certainTag
instance. ifreplace
equals tofalse
, it won't replace an exsiting child.replace
defaults tofalse
getNames()
: get children's namesdeleteByName(name)
: delete a child tag by tag nameclean()
: clean all children
The value's type is array of integer (array of number in js).
There're 5 extra methods:
shift()
unshift(value)
pop()
push(value)
insert(value, position)
You may refer to test/test.js
:
var NBT = require('../nbt');
var nbt = new NBT();
nbt.loadFromZlibCompressedFile('./level.dat', function(err) {
if(err) return console.error(err);
var gameRules = nbt.select('').select('Data').select('GameRules');
console.log(gameRules.getType());
console.log(gameRules.getTypeId());
console.log(gameRules.toString());
});
You're welcome to pull requests!
「雖然我覺得不怎麼可能有人會關注我」