-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathminecraft-protocol-stream.js
90 lines (73 loc) · 2.92 KB
/
minecraft-protocol-stream.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
'use strict';
//process.env.NODE_DEBUG = 'mc-proto'; // for node-minecraft-protocol console packet debugging TODO: envify
var EmptyTransformStream = require('through')();
var Client = require('minecraft-protocol/src').Client;
var forgeHandshake = require('minecraft-protocol-forge').forgeHandshake;
var protocol = require('minecraft-protocol/src');
var assert = require('assert');
var states = protocol.states;
module.exports = {
protocol: protocol,
createClient: createClient
};
// generic stream version of index.js createClient()
function createClient(options) {
assert.ok(options, "options is required");
var stream = options.stream;
assert.ok(stream, "stream is required");
assert.ok(options.username, "username is required");
var keepAlive = options.keepAlive == null ? true : options.keepAlive;
var optVersion = options.version || require('./mcversion.js');
var mcData = require('minecraft-data')(optVersion);
var version = mcData.version;
var client = new Client(false, version.majorVersion);
// Options to opt-out of MC protocol packet framing (useful since WS is alreay framed)
if (options.noPacketFramer) {
client.framer = EmptyTransformStream;
}
if (options.noPacketSplitter) {
client.splitter = EmptyTransformStream;
}
client.on('connect', onConnect);
client.once('success', onLogin);
client.once('compress', onCompressionRequest);
client.once('set_compression', onCompressionRequest);
if (keepAlive) client.on('keep_alive', onKeepAlive);
client.username = options.username;
client.setSocket(stream);
return client;
function onConnect() {
client.socket.write(new Buffer(client.username));
// wsmc uses slightly abbreviated protocol; skip the HANDSHAKING phase, go
// directly to LOGIN, to receive login success and set compression packets
client.state = states.LOGIN;
}
function onLogin(packet) {
console.log('onLogin',packet);
// successful login, transition to play phase
client.state = states.PLAY;
client.uuid = packet.uuid;
if (packet.username.indexOf('\0') !== -1) {
var parts = packet.username.split('\0');
console.log('WSMC-augmented login packet detected, username field pieces: ',parts);
client.username = parts[0];
client.wsmcPingResponse = JSON.parse(parts[1]); // ping payload response, modinfo for Forge servers
if (client.wsmcPingResponse.modinfo && client.wsmcPingResponse.modinfo.type === 'FML') {
var modList = client.wsmcPingResponse.modinfo.modList;
console.log('Enabling Forge support with mods:', modList);
forgeHandshake(client, {forgeMods: modList});
}
} else {
client.username = packet.username;
}
}
function onCompressionRequest(packet) {
console.log('onCompressionRequest', packet);
client.compressionThreshold = packet.threshold;
}
function onKeepAlive(packet) {
client.write('keep_alive', {
keepAliveId: packet.keepAliveId
});
}
}