packet-events-js 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +398 -0
- package/package.json +31 -0
- package/src/auth/AuthHandler.js +138 -0
- package/src/auth/MojangAPI.js +186 -0
- package/src/client/MinecraftClient.js +336 -0
- package/src/crypto/Encryption.js +125 -0
- package/src/events/EventEmitter.js +267 -0
- package/src/events/PacketEvent.js +78 -0
- package/src/index.js +18 -0
- package/src/manager/PacketManager.js +258 -0
- package/src/protocol/ConnectionState.js +37 -0
- package/src/protocol/PacketDirection.js +8 -0
- package/src/protocol/ProtocolVersion.js +141 -0
- package/src/protocol/packets/Packet.js +119 -0
- package/src/protocol/packets/PacketRegistry.js +145 -0
- package/src/protocol/packets/handshake/HandshakePacket.js +44 -0
- package/src/protocol/packets/index.js +265 -0
- package/src/protocol/packets/login/DisconnectPacket.js +71 -0
- package/src/protocol/packets/login/EncryptionRequestPacket.js +47 -0
- package/src/protocol/packets/login/EncryptionResponsePacket.js +34 -0
- package/src/protocol/packets/login/LoginStartPacket.js +35 -0
- package/src/protocol/packets/login/LoginSuccessPacket.js +61 -0
- package/src/protocol/packets/login/SetCompressionPacket.js +29 -0
- package/src/protocol/packets/play/ChatPacket.js +238 -0
- package/src/protocol/packets/play/ChunkPacket.js +122 -0
- package/src/protocol/packets/play/EntityPacket.js +302 -0
- package/src/protocol/packets/play/KeepAlivePacket.js +55 -0
- package/src/protocol/packets/play/PlayerPositionPacket.js +266 -0
- package/src/protocol/packets/status/PingPacket.js +29 -0
- package/src/protocol/packets/status/PongPacket.js +29 -0
- package/src/protocol/packets/status/StatusRequestPacket.js +20 -0
- package/src/protocol/packets/status/StatusResponsePacket.js +58 -0
- package/src/protocol/types/NBT.js +594 -0
- package/src/protocol/types/Position.js +125 -0
- package/src/protocol/types/TextComponent.js +355 -0
- package/src/protocol/types/UUID.js +105 -0
- package/src/protocol/types/VarInt.js +144 -0
- package/src/protocol/types/index.js +5 -0
- package/src/utils/Logger.js +207 -0
- package/src/utils/PacketBuffer.js +389 -0
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
export const LogLevel = Object.freeze({
|
|
2
|
+
TRACE: 0,
|
|
3
|
+
DEBUG: 1,
|
|
4
|
+
INFO: 2,
|
|
5
|
+
WARN: 3,
|
|
6
|
+
ERROR: 4,
|
|
7
|
+
FATAL: 5,
|
|
8
|
+
OFF: 6
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
const Colors = {
|
|
12
|
+
reset: '\x1b[0m',
|
|
13
|
+
bright: '\x1b[1m',
|
|
14
|
+
dim: '\x1b[2m',
|
|
15
|
+
|
|
16
|
+
black: '\x1b[30m',
|
|
17
|
+
red: '\x1b[31m',
|
|
18
|
+
green: '\x1b[32m',
|
|
19
|
+
yellow: '\x1b[33m',
|
|
20
|
+
blue: '\x1b[34m',
|
|
21
|
+
magenta: '\x1b[35m',
|
|
22
|
+
cyan: '\x1b[36m',
|
|
23
|
+
white: '\x1b[37m',
|
|
24
|
+
|
|
25
|
+
bgBlack: '\x1b[40m',
|
|
26
|
+
bgRed: '\x1b[41m',
|
|
27
|
+
bgGreen: '\x1b[42m',
|
|
28
|
+
bgYellow: '\x1b[43m',
|
|
29
|
+
bgBlue: '\x1b[44m',
|
|
30
|
+
bgMagenta: '\x1b[45m',
|
|
31
|
+
bgCyan: '\x1b[46m',
|
|
32
|
+
bgWhite: '\x1b[47m'
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const LevelConfig = {
|
|
36
|
+
[LogLevel.TRACE]: { name: 'TRACE', color: Colors.dim + Colors.white },
|
|
37
|
+
[LogLevel.DEBUG]: { name: 'DEBUG', color: Colors.cyan },
|
|
38
|
+
[LogLevel.INFO]: { name: 'INFO ', color: Colors.green },
|
|
39
|
+
[LogLevel.WARN]: { name: 'WARN ', color: Colors.yellow },
|
|
40
|
+
[LogLevel.ERROR]: { name: 'ERROR', color: Colors.red },
|
|
41
|
+
[LogLevel.FATAL]: { name: 'FATAL', color: Colors.bright + Colors.red }
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export class Logger {
|
|
45
|
+
static _loggers = new Map();
|
|
46
|
+
static _globalLevel = LogLevel.INFO;
|
|
47
|
+
static _useColors = true;
|
|
48
|
+
static _showTimestamp = true;
|
|
49
|
+
static _showLoggerName = true;
|
|
50
|
+
|
|
51
|
+
constructor(name) {
|
|
52
|
+
this.name = name;
|
|
53
|
+
this.level = null;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
static getLogger(name) {
|
|
57
|
+
if (!Logger._loggers.has(name)) {
|
|
58
|
+
Logger._loggers.set(name, new Logger(name));
|
|
59
|
+
}
|
|
60
|
+
return Logger._loggers.get(name);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static setGlobalLevel(level) {
|
|
64
|
+
Logger._globalLevel = level;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
static setColorsEnabled(enabled) {
|
|
68
|
+
Logger._useColors = enabled;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
static setTimestampEnabled(enabled) {
|
|
72
|
+
Logger._showTimestamp = enabled;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
setLevel(level) {
|
|
76
|
+
this.level = level;
|
|
77
|
+
return this;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
getEffectiveLevel() {
|
|
81
|
+
return this.level !== null ? this.level : Logger._globalLevel;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
isEnabled(level) {
|
|
85
|
+
return level >= this.getEffectiveLevel();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
_formatTimestamp() {
|
|
89
|
+
const now = new Date();
|
|
90
|
+
const pad = (n) => n.toString().padStart(2, '0');
|
|
91
|
+
return `${pad(now.getHours())}:${pad(now.getMinutes())}:${pad(now.getSeconds())}.${now.getMilliseconds().toString().padStart(3, '0')}`;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
_format(level, message, args) {
|
|
95
|
+
const config = LevelConfig[level];
|
|
96
|
+
let output = '';
|
|
97
|
+
|
|
98
|
+
if (Logger._useColors) {
|
|
99
|
+
|
|
100
|
+
if (Logger._showTimestamp) {
|
|
101
|
+
output += `${Colors.dim}[${this._formatTimestamp()}]${Colors.reset} `;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
output += `${config.color}[${config.name}]${Colors.reset}`;
|
|
105
|
+
|
|
106
|
+
if (Logger._showLoggerName && this.name) {
|
|
107
|
+
output += ` ${Colors.magenta}[${this.name}]${Colors.reset}`;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
output += ` ${message}`;
|
|
111
|
+
} else {
|
|
112
|
+
|
|
113
|
+
if (Logger._showTimestamp) {
|
|
114
|
+
output += `[${this._formatTimestamp()}] `;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
output += `[${config.name}]`;
|
|
118
|
+
|
|
119
|
+
if (Logger._showLoggerName && this.name) {
|
|
120
|
+
output += ` [${this.name}]`;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
output += ` ${message}`;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (args.length > 0) {
|
|
127
|
+
for (const arg of args) {
|
|
128
|
+
if (arg instanceof Error) {
|
|
129
|
+
output += '\n' + arg.stack;
|
|
130
|
+
} else if (typeof arg === 'object') {
|
|
131
|
+
output += '\n' + JSON.stringify(arg, null, 2);
|
|
132
|
+
} else {
|
|
133
|
+
output += ' ' + arg;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return output;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
log(level, message, ...args) {
|
|
142
|
+
if (!this.isEnabled(level)) return;
|
|
143
|
+
|
|
144
|
+
const output = this._format(level, message, args);
|
|
145
|
+
|
|
146
|
+
if (level >= LogLevel.ERROR) {
|
|
147
|
+
console.error(output);
|
|
148
|
+
} else if (level >= LogLevel.WARN) {
|
|
149
|
+
console.warn(output);
|
|
150
|
+
} else {
|
|
151
|
+
console.log(output);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
trace(message, ...args) {
|
|
156
|
+
this.log(LogLevel.TRACE, message, ...args);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
debug(message, ...args) {
|
|
160
|
+
this.log(LogLevel.DEBUG, message, ...args);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
info(message, ...args) {
|
|
164
|
+
this.log(LogLevel.INFO, message, ...args);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
warn(message, ...args) {
|
|
168
|
+
this.log(LogLevel.WARN, message, ...args);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
error(message, ...args) {
|
|
172
|
+
this.log(LogLevel.ERROR, message, ...args);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
fatal(message, ...args) {
|
|
176
|
+
this.log(LogLevel.FATAL, message, ...args);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
child(suffix) {
|
|
180
|
+
return Logger.getLogger(`${this.name}.${suffix}`);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
hexdump(label, data) {
|
|
184
|
+
if (!this.isEnabled(LogLevel.DEBUG)) return;
|
|
185
|
+
|
|
186
|
+
const lines = [];
|
|
187
|
+
const bytesPerLine = 16;
|
|
188
|
+
|
|
189
|
+
for (let i = 0; i < data.length; i += bytesPerLine) {
|
|
190
|
+
const slice = data.subarray(i, Math.min(i + bytesPerLine, data.length));
|
|
191
|
+
const hex = Array.from(slice)
|
|
192
|
+
.map(b => b.toString(16).padStart(2, '0'))
|
|
193
|
+
.join(' ');
|
|
194
|
+
const ascii = Array.from(slice)
|
|
195
|
+
.map(b => (b >= 32 && b <= 126) ? String.fromCharCode(b) : '.')
|
|
196
|
+
.join('');
|
|
197
|
+
|
|
198
|
+
lines.push(`${i.toString(16).padStart(8, '0')} ${hex.padEnd(48, ' ')} |${ascii}|`);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
this.debug(`${label}:\n${lines.join('\n')}`);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export const logger = Logger.getLogger('PacketEvents');
|
|
206
|
+
|
|
207
|
+
export default Logger;
|
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
import { encodeVarInt, decodeVarInt, encodeVarLong, decodeVarLong, getVarIntSize } from '../protocol/types/VarInt.js';
|
|
2
|
+
import { UUID } from '../protocol/types/UUID.js';
|
|
3
|
+
import { Position } from '../protocol/types/Position.js';
|
|
4
|
+
import { NBTReader, NBTWriter, NBTCompound } from '../protocol/types/NBT.js';
|
|
5
|
+
|
|
6
|
+
export class PacketBuffer {
|
|
7
|
+
|
|
8
|
+
constructor(bufferOrSize) {
|
|
9
|
+
if (Buffer.isBuffer(bufferOrSize)) {
|
|
10
|
+
this.buffer = bufferOrSize;
|
|
11
|
+
} else if (typeof bufferOrSize === 'number') {
|
|
12
|
+
this.buffer = Buffer.alloc(bufferOrSize);
|
|
13
|
+
} else {
|
|
14
|
+
this.buffer = Buffer.alloc(0);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
this.readOffset = 0;
|
|
18
|
+
this.writeOffset = 0;
|
|
19
|
+
this._writeBuffers = [];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
static writer() {
|
|
23
|
+
const pb = new PacketBuffer();
|
|
24
|
+
pb._mode = 'write';
|
|
25
|
+
return pb;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
static reader(buffer) {
|
|
29
|
+
const pb = new PacketBuffer(buffer);
|
|
30
|
+
pb._mode = 'read';
|
|
31
|
+
return pb;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
static wrap(buffer) {
|
|
35
|
+
return new PacketBuffer(buffer);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
get readableBytes() {
|
|
39
|
+
return this.buffer.length - this.readOffset;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
isReadable(count = 1) {
|
|
43
|
+
return this.readableBytes >= count;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
resetReader() {
|
|
47
|
+
this.readOffset = 0;
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
skip(count) {
|
|
52
|
+
this.readOffset += count;
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
readBoolean() {
|
|
57
|
+
return this.buffer.readUInt8(this.readOffset++) !== 0;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
readByte() {
|
|
61
|
+
return this.buffer.readInt8(this.readOffset++);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
readUByte() {
|
|
65
|
+
return this.buffer.readUInt8(this.readOffset++);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
readShort() {
|
|
69
|
+
const value = this.buffer.readInt16BE(this.readOffset);
|
|
70
|
+
this.readOffset += 2;
|
|
71
|
+
return value;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
readUShort() {
|
|
75
|
+
const value = this.buffer.readUInt16BE(this.readOffset);
|
|
76
|
+
this.readOffset += 2;
|
|
77
|
+
return value;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
readInt() {
|
|
81
|
+
const value = this.buffer.readInt32BE(this.readOffset);
|
|
82
|
+
this.readOffset += 4;
|
|
83
|
+
return value;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
readUInt() {
|
|
87
|
+
const value = this.buffer.readUInt32BE(this.readOffset);
|
|
88
|
+
this.readOffset += 4;
|
|
89
|
+
return value;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
readLong() {
|
|
93
|
+
const value = this.buffer.readBigInt64BE(this.readOffset);
|
|
94
|
+
this.readOffset += 8;
|
|
95
|
+
return value;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
readULong() {
|
|
99
|
+
const value = this.buffer.readBigUInt64BE(this.readOffset);
|
|
100
|
+
this.readOffset += 8;
|
|
101
|
+
return value;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
readFloat() {
|
|
105
|
+
const value = this.buffer.readFloatBE(this.readOffset);
|
|
106
|
+
this.readOffset += 4;
|
|
107
|
+
return value;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
readDouble() {
|
|
111
|
+
const value = this.buffer.readDoubleBE(this.readOffset);
|
|
112
|
+
this.readOffset += 8;
|
|
113
|
+
return value;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
readVarInt() {
|
|
117
|
+
const { value, bytesRead } = decodeVarInt(this.buffer, this.readOffset);
|
|
118
|
+
this.readOffset += bytesRead;
|
|
119
|
+
return value;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
readVarLong() {
|
|
123
|
+
const { value, bytesRead } = decodeVarLong(this.buffer, this.readOffset);
|
|
124
|
+
this.readOffset += bytesRead;
|
|
125
|
+
return value;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
readString(maxLength = 32767) {
|
|
129
|
+
const length = this.readVarInt();
|
|
130
|
+
|
|
131
|
+
if (length > maxLength * 4) {
|
|
132
|
+
throw new Error(`String too long: ${length} > ${maxLength * 4}`);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const value = this.buffer.toString('utf8', this.readOffset, this.readOffset + length);
|
|
136
|
+
this.readOffset += length;
|
|
137
|
+
|
|
138
|
+
if (value.length > maxLength) {
|
|
139
|
+
throw new Error(`String too long: ${value.length} > ${maxLength}`);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return value;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
readIdentifier() {
|
|
146
|
+
return this.readString(32767);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
readUUID() {
|
|
150
|
+
const uuid = UUID.fromBuffer(this.buffer, this.readOffset);
|
|
151
|
+
this.readOffset += 16;
|
|
152
|
+
return uuid;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
readPosition() {
|
|
156
|
+
const position = Position.fromBuffer(this.buffer, this.readOffset);
|
|
157
|
+
this.readOffset += 8;
|
|
158
|
+
return position;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
readNBT(named = true) {
|
|
162
|
+
const reader = new NBTReader(this.buffer, this.readOffset);
|
|
163
|
+
const compound = reader.read(named);
|
|
164
|
+
this.readOffset = reader.offset;
|
|
165
|
+
return compound;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
readByteArray() {
|
|
169
|
+
const length = this.readVarInt();
|
|
170
|
+
const value = this.buffer.subarray(this.readOffset, this.readOffset + length);
|
|
171
|
+
this.readOffset += length;
|
|
172
|
+
return Buffer.from(value);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
readBytes(length) {
|
|
176
|
+
const value = this.buffer.subarray(this.readOffset, this.readOffset + length);
|
|
177
|
+
this.readOffset += length;
|
|
178
|
+
return Buffer.from(value);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
readRemainingBytes() {
|
|
182
|
+
const value = this.buffer.subarray(this.readOffset);
|
|
183
|
+
this.readOffset = this.buffer.length;
|
|
184
|
+
return Buffer.from(value);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
readOptional(reader) {
|
|
188
|
+
const present = this.readBoolean();
|
|
189
|
+
return present ? reader.call(this) : null;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
readArray(reader) {
|
|
193
|
+
const length = this.readVarInt();
|
|
194
|
+
const array = [];
|
|
195
|
+
|
|
196
|
+
for (let i = 0; i < length; i++) {
|
|
197
|
+
array.push(reader.call(this));
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return array;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
readAngle() {
|
|
204
|
+
return (this.readUByte() / 256) * 360;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
readFixedPoint(intBits, fracBits) {
|
|
208
|
+
const raw = intBits + fracBits <= 32 ? this.readInt() : Number(this.readLong());
|
|
209
|
+
return raw / (1 << fracBits);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
writeBoolean(value) {
|
|
213
|
+
const buf = Buffer.alloc(1);
|
|
214
|
+
buf.writeUInt8(value ? 1 : 0);
|
|
215
|
+
this._writeBuffers.push(buf);
|
|
216
|
+
return this;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
writeByte(value) {
|
|
220
|
+
const buf = Buffer.alloc(1);
|
|
221
|
+
buf.writeInt8(value);
|
|
222
|
+
this._writeBuffers.push(buf);
|
|
223
|
+
return this;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
writeUByte(value) {
|
|
227
|
+
const buf = Buffer.alloc(1);
|
|
228
|
+
buf.writeUInt8(value);
|
|
229
|
+
this._writeBuffers.push(buf);
|
|
230
|
+
return this;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
writeShort(value) {
|
|
234
|
+
const buf = Buffer.alloc(2);
|
|
235
|
+
buf.writeInt16BE(value);
|
|
236
|
+
this._writeBuffers.push(buf);
|
|
237
|
+
return this;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
writeUShort(value) {
|
|
241
|
+
const buf = Buffer.alloc(2);
|
|
242
|
+
buf.writeUInt16BE(value);
|
|
243
|
+
this._writeBuffers.push(buf);
|
|
244
|
+
return this;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
writeInt(value) {
|
|
248
|
+
const buf = Buffer.alloc(4);
|
|
249
|
+
buf.writeInt32BE(value);
|
|
250
|
+
this._writeBuffers.push(buf);
|
|
251
|
+
return this;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
writeUInt(value) {
|
|
255
|
+
const buf = Buffer.alloc(4);
|
|
256
|
+
buf.writeUInt32BE(value);
|
|
257
|
+
this._writeBuffers.push(buf);
|
|
258
|
+
return this;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
writeLong(value) {
|
|
262
|
+
const buf = Buffer.alloc(8);
|
|
263
|
+
buf.writeBigInt64BE(BigInt(value));
|
|
264
|
+
this._writeBuffers.push(buf);
|
|
265
|
+
return this;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
writeULong(value) {
|
|
269
|
+
const buf = Buffer.alloc(8);
|
|
270
|
+
buf.writeBigUInt64BE(BigInt(value));
|
|
271
|
+
this._writeBuffers.push(buf);
|
|
272
|
+
return this;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
writeFloat(value) {
|
|
276
|
+
const buf = Buffer.alloc(4);
|
|
277
|
+
buf.writeFloatBE(value);
|
|
278
|
+
this._writeBuffers.push(buf);
|
|
279
|
+
return this;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
writeDouble(value) {
|
|
283
|
+
const buf = Buffer.alloc(8);
|
|
284
|
+
buf.writeDoubleBE(value);
|
|
285
|
+
this._writeBuffers.push(buf);
|
|
286
|
+
return this;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
writeVarInt(value) {
|
|
290
|
+
this._writeBuffers.push(encodeVarInt(value));
|
|
291
|
+
return this;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
writeVarLong(value) {
|
|
295
|
+
this._writeBuffers.push(encodeVarLong(value));
|
|
296
|
+
return this;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
writeString(value) {
|
|
300
|
+
const strBuf = Buffer.from(value, 'utf8');
|
|
301
|
+
this.writeVarInt(strBuf.length);
|
|
302
|
+
this._writeBuffers.push(strBuf);
|
|
303
|
+
return this;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
writeIdentifier(value) {
|
|
307
|
+
return this.writeString(value);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
writeUUID(uuid) {
|
|
311
|
+
this._writeBuffers.push(uuid.toBytes());
|
|
312
|
+
return this;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
writePosition(position) {
|
|
316
|
+
this._writeBuffers.push(position.toBytes());
|
|
317
|
+
return this;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
writeNBT(compound, named = true) {
|
|
321
|
+
const writer = new NBTWriter();
|
|
322
|
+
const nbtBuffer = writer.write(compound, named);
|
|
323
|
+
this._writeBuffers.push(nbtBuffer);
|
|
324
|
+
return this;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
writeByteArray(value) {
|
|
328
|
+
this.writeVarInt(value.length);
|
|
329
|
+
this._writeBuffers.push(value);
|
|
330
|
+
return this;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
writeBytes(value) {
|
|
334
|
+
this._writeBuffers.push(Buffer.from(value));
|
|
335
|
+
return this;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
writeOptional(value, writer) {
|
|
339
|
+
if (value !== null && value !== undefined) {
|
|
340
|
+
this.writeBoolean(true);
|
|
341
|
+
writer.call(this, value);
|
|
342
|
+
} else {
|
|
343
|
+
this.writeBoolean(false);
|
|
344
|
+
}
|
|
345
|
+
return this;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
writeArray(array, writer) {
|
|
349
|
+
this.writeVarInt(array.length);
|
|
350
|
+
|
|
351
|
+
for (const item of array) {
|
|
352
|
+
writer.call(this, item);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
return this;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
writeAngle(degrees) {
|
|
359
|
+
return this.writeUByte(Math.floor((degrees / 360) * 256) & 0xFF);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
getBuffer() {
|
|
363
|
+
if (this._writeBuffers.length > 0) {
|
|
364
|
+
return Buffer.concat(this._writeBuffers);
|
|
365
|
+
}
|
|
366
|
+
return this.buffer;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
get length() {
|
|
370
|
+
if (this._writeBuffers.length > 0) {
|
|
371
|
+
return this._writeBuffers.reduce((sum, buf) => sum + buf.length, 0);
|
|
372
|
+
}
|
|
373
|
+
return this.buffer.length;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
clear() {
|
|
377
|
+
this._writeBuffers = [];
|
|
378
|
+
this.readOffset = 0;
|
|
379
|
+
return this;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
copy() {
|
|
383
|
+
const pb = new PacketBuffer(Buffer.from(this.getBuffer()));
|
|
384
|
+
pb.readOffset = this.readOffset;
|
|
385
|
+
return pb;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
export default PacketBuffer;
|