-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now you can directly pass in a non `NBTData`-wrapped `CompoundTag` into the Write module methods! This makes things easier if you aren't working with `NBTData` objects directly, and with the bare NBT JavaScript object instead. Wrote more about this in the same-named issue on the repo! In the demo test file, I simply wrote the same data to a buffer, using both the `NBT.write()` and `NBTWriter.write()` functions, one using the existing `NBTData` parameter call, and the other with only the `CompoundTag` and options config, no `NBTData` for that one. As you can see in the console, no errors, and the same identical buffer data is written! Noice. Oh yeah, just to verify the value of the `CompoundTag` a little more strictly, I added a `data !== null` check in there too, since `typeof null` will return `"object"`. I think I read that it's a weird JavaScript legacy thing, kind of interesting. I don't want any of the library parameters to accept `null` as an NBT value! That would be confusing, haha. Oooh! While writing that last sentence, decided to check this in TypeScript, and I'm very happy to say that this doesn't pass, aaah :) Scared me for a moment, thinking about this possibly working, since `null` is technically an object in JS: ```ts const thingo: object = null; // Type 'null' is not assignable to type 'object'. ```
- Loading branch information
1 parent
c84cb8a
commit 00425ac
Showing
4 changed files
with
41 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,28 @@ | ||
// @ts-check | ||
|
||
import * as fs from "node:fs/promises"; | ||
import { Buffer } from "node:buffer"; | ||
import * as NBT from "../dist/index.js"; | ||
|
||
const data = await fs.readFile(new URL("./nbt/level.dat",import.meta.url)); | ||
console.log(data,"\n"); | ||
/** @type { NBT.CompoundTag } */ | ||
const value = { | ||
IsFancy: new NBT.Byte(0), | ||
Noice: new Int8Array([55,32,4,125,8,99,57,4]) | ||
}; | ||
console.log(value,"\n"); | ||
|
||
const result = await NBT.read(data); | ||
/** @type { NBT.WriteOptions } */ | ||
const options = { name: null, endian: "little" }; | ||
console.log(options,"\n"); | ||
|
||
const data = new NBT.NBTData(value,options); | ||
// console.log(data,"\n"); | ||
|
||
const writer = new NBT.NBTWriter(); | ||
|
||
const result = Buffer.from(writer.write(data)); | ||
console.log(result,"\n"); | ||
|
||
const result2 = await NBT.write(result,{ bedrockLevel: null }) | ||
.then(buffer => NBT.read(buffer.slice(0,-1),{ endian: "little" })); | ||
const result2 = Buffer.from(await NBT.write(value,options)); | ||
console.log(result2,"\n"); | ||
|
||
const recompile = Buffer.from(await NBT.write(result2)); | ||
console.log(recompile,"\n"); | ||
|
||
console.log(Buffer.compare(data,recompile)); | ||
console.log(Buffer.compare(result,result2)); |