Skip to content

Commit 0ca15c9

Browse files
authored
add nbt builder (#48)
* add nbt builder * Update README.md * Update ci.yml * change how builder is exported + fix typings
1 parent 80d4982 commit 0ca15c9

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

.github/workflows/ci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313

1414
strategy:
1515
matrix:
16-
node-version: [12.x]
16+
node-version: [14.x]
1717

1818
steps:
1919
- uses: actions/checkout@v2
@@ -22,4 +22,4 @@ jobs:
2222
with:
2323
node-version: ${{ matrix.node-version }}
2424
- run: npm install
25-
- run: npm test
25+
- run: npm test

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,19 @@ Provide the big-endian protodef instance used to parse and serialize nbt.
8484
### protoLE
8585

8686
Provide the little-endian protodef instance used to parse and serialize little endian nbt.
87+
88+
### builder
89+
90+
Provides a way to build complex nbt structures simply:
91+
92+
```js
93+
const nbt = require('prismarine-nbt')
94+
writePlayerNbt({
95+
Air: nbt.short(300),
96+
Armor: nbt.list(
97+
nbt.comp({ Count: nbt.byte(0), Damage: nbt.short(0), Name: nbt.string("") }),
98+
nbt.comp({ Count: nbt.byte(0), Damage: nbt.short(0), Name: nbt.string("") }),
99+
nbt.comp({ Count: nbt.byte(0), Damage: nbt.short(0), Name: nbt.string("") }),
100+
),
101+
})
102+
```

nbt.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,18 @@ function simplify (data) {
155155
return transform(data.value, data.type)
156156
}
157157

158+
const builder = {
159+
short (value) { return { type: 'short', value } },
160+
byte (value) { return { type: 'byte', value } },
161+
string (value) { return { type: 'string', value } },
162+
comp (value) { return { type: 'compound', value } },
163+
int (value) { return { type: 'int', value } },
164+
list (...value) {
165+
const type = value[0]?.type ?? 'end'
166+
return { type: 'list', value: { type, value } }
167+
}
168+
}
169+
158170
module.exports = {
159171
writeUncompressed,
160172
parseUncompressed,
@@ -164,5 +176,6 @@ module.exports = {
164176
parseAs,
165177
proto: protoBE,
166178
protoLE,
167-
protos
179+
protos,
180+
...builder
168181
}

sample/builderExample.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const nbt = require('prismarine-nbt')
2+
3+
const writePlayerNbt = () => {}
4+
writePlayerNbt({
5+
Air: nbt.short(300),
6+
Armor: nbt.list(
7+
nbt.comp({ Count: nbt.byte(0), Damage: nbt.short(0), Name: nbt.string('') }),
8+
nbt.comp({ Count: nbt.byte(0), Damage: nbt.short(0), Name: nbt.string('') }),
9+
nbt.comp({ Count: nbt.byte(0), Damage: nbt.short(0), Name: nbt.string('') })
10+
)
11+
})

typings/index.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,11 @@ declare module 'prismarine-nbt'{
6565
export function parse(data: Buffer, nbtType: NBTFormat, callback: (err: Error | null, value: NBT) => any): void;
6666
/** @deprecated */
6767
export function parse(data: Buffer, callback: (err: Error | null, value: NBT) => any): void;
68+
69+
export function short<T extends number> (val: T): { type: 'short', value: T }
70+
export function byte<T extends number> (val: T): { type: 'byte', value: T }
71+
export function string<T extends string> (val: T): { type: 'string', value: T }
72+
export function comp<T extends object> (val: T): { type: 'compound', value: T }
73+
export function int<T extends number> (val: T): { type: 'int', value: T }
74+
export function list<T extends string, K extends {type: T}>(...value: K[]): { type: 'list'; value: { type: T | 'end', value: K[] } };
6875
}

0 commit comments

Comments
 (0)