-
Notifications
You must be signed in to change notification settings - Fork 1
/
bufferGenerators.js
113 lines (96 loc) · 2.14 KB
/
bufferGenerators.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const ByteStream = require("./byteStream.js");
const uuidParse = require("uuid-parse");
function string(str) {
var strLengthBuffer = varInt(str.length);
var strBuffer = Buffer.from(str)
return Buffer.concat([strLengthBuffer, strBuffer])
}
function uuid(uuid) {
return Buffer.from(uuidParse.parse(uuid))
}
function position(position) {
var x = position.x
var y = position.y
var z = position.z
var firstInt32 = ((x & 0x03FFFFFF) << 6) | ((y & 0x0FC0) >>> 6)
var secondInt32 = ((y & 0x3F) << 26) | (z & 0x03FFFFFF)
var buf = Buffer.alloc(8)
buf.writeInt32BE(firstInt32)
buf.slice(4).writeInt32BE(secondInt32)
return buf
}
function varInt(value) {
var byteSize = varIntSizeOf(value)
var bs = new ByteStream(Buffer.alloc(byteSize));
bs.writeVarInt(value);
return bs.buffer
}
function array(values, f){
return Buffer.concat(values.map(f))
}
function float(value) {
var b = Buffer.alloc(4)
b.writeFloatBE(value, 0)
return b
}
function angle(value) {
value = value % 360
value = value < 0 ? value + 360 : value
value = Math.floor((value/360) * 256)
return unsignedByte(value)
}
function slot(value) {
buf = value.present ?
Buffer.concat([
unsignedByte(value.present),
varInt(value.id),
unsignedByte(value.count),
unsignedByte(0) //blank nbt
]) :
unsignedByte(value.present)
return buf
}
function double(value) {
var b = Buffer.alloc(8)
b.writeDoubleBE(value, 0)
return b
}
function int(value) {
var b = Buffer.alloc(4)
b.writeInt32BE(value)
return b
}
function short(value) {
var b = Buffer.alloc(2)
b.writeInt16BE(value)
return b
}
function unsignedByte(value) {
var b = Buffer.alloc(1)
b.writeUInt8(value)
return b
}
function signedByte(value) {
var b = Buffer.alloc(1)
b.writeInt8(value)
return b
}
function varIntSizeOf(x) {
return x < 2 ? 1 : Math.ceil(Math.log2(x)/7);
}
module.exports = {
string: string,
uuid: uuid,
position: position,
varInt: varInt,
array: array,
float: float,
angle: angle,
slot: slot,
double: double,
int: int,
short: short,
unsignedByte: unsignedByte,
signedByte: signedByte,
varIntSizeOf: varIntSizeOf
}