Skip to content

Instantly share code, notes, and snippets.

@robere2
Created August 9, 2019 03:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robere2/1fe6b104ed974571b17eb8efe12e073f to your computer and use it in GitHub Desktop.
Save robere2/1fe6b104ed974571b17eb8efe12e073f to your computer and use it in GitHub Desktop.
Example on how to parse compressed or uncompressed Minecraft NBT data in Javascript
let fs = require('fs');
let nbt = require('prismarine-nbt');
let invData = [31, -117, 8, 0, 0, 0, 0, 0, 0, 0, -29, 98, 96, -32, 100, 96, -52, -28, 98, 96, 96, 80, 97, 98, 96, -54, 76, 97, 100, 98, 100, 96,
117, -50, 47, -51, 43, 97, -28, 98, 96, 46, 73, 76, 103, 100, -32, 14, -51, 75, 42, 74, 77, -52, 78, 76, -54, 73, 101, 100, 102, -32, -12,
-56, 76, 73, 117, -53, 73, 76, 47, 6, 106, -6, -57, -59, -64, -98, -110, 89, 92, -112, -109, 88, -55, -55, -64, -30, -109, 95, -108, -54, 1,
20, 101, 97, -32, 57, -76, -36, 60, 32, -75, 40, 91, 33, -77, 36, 53, 23, 40, 34, 124, 104, -71, -91, -74, -71, -126, 99, 73, 73, 98, 114,
-74, -126, 75, 98, 110, 98, 122, 42, 3, 31, 80, 16, -55, 108, 6, 6, 38, 6, 54, -88, 20, -120, 13, 114, 13, 43, 126, -41, -96, -24, -128, 0,
-120, 62, 54, -104, 62, 5, 76, 21, -40, 1, 0, 111, 21, -87, 83, 10, 1, 0, 0];
let buf = Buffer.from(invData);
nbt.parse(buf, (err, data) => {
if(err)
return console.error(err);
console.log(JSON.stringify(data));
/* Response:
{"type":"compound","name":"","value":{"i":{"type":"list","value":{"type":"compound","value":[{"id":{"type":"short","value":258},
"Count":{"type":"byte","value":1},"tag":{"type":"compound","value":{"Unbreakable":{"type":"byte","value":1},"HideFlags":{"type":"int","value":254},
"display":{"type":"compound","value":{"Lore":{"type":"list","value":{"type":"string","value":["§7Perk item","","§9+7 Attack Damage",
"§9Unbreakable"]}}}}}},"Damage":{"type":"short","value":0}},{"id":{"type":"short","value":261},"Count":{"type":"byte","value":1},
"tag":{"type":"compound","value":{"Unbreakable":{"type":"byte","value":1}}},"Damage":{"type":"short","value":0}},{},{},{},{},{},{},
{"id":{"type":"short","value":262},"Count":{"type":"byte","value":32},"Damage":{"type":"short","value":0}},{},{},{},{},{},{},{},{},
{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}]}}}}
*/
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment