mirror of
https://github.com/unmojang/node-minecraft-protocol.git
synced 2025-10-05 09:01:54 -04:00
59 lines
2.4 KiB
JavaScript
59 lines
2.4 KiB
JavaScript
'use strict';
|
|
|
|
const ping = require('../ping');
|
|
const debug = require('debug')('minecraft-protocol');
|
|
const states = require('../states');
|
|
const assert = require('assert');
|
|
const minecraft_data = require('minecraft-data');
|
|
|
|
module.exports = function(client, options) {
|
|
client.wait_connect = true; // don't let src/client/setProtocol proceed on socket 'connect' until 'connect_allowed'
|
|
debug('pinging',options.host);
|
|
// TODO: use 0xfe ping instead for better compatibility/performance? https://github.com/deathcap/node-minecraft-ping
|
|
ping(options, function(err, response) {
|
|
client.emit('error',err);
|
|
debug('ping response',response);
|
|
// TODO: could also use ping pre-connect to save description, type, max players, etc.
|
|
const motd = response.description;
|
|
debug('Server description:',motd); // TODO: save
|
|
|
|
// Pass server-reported version to protocol handler
|
|
// The version string is interpreted by https://github.com/PrismarineJS/node-minecraft-data
|
|
const brandedMinecraftVersion = response.version.name; // 1.8.9, 1.7.10
|
|
const protocolVersion = response.version.protocol;// 47, 5
|
|
let versions = [brandedMinecraftVersion]
|
|
.concat(brandedMinecraftVersion.match(/((\d+\.)+\d+)/g)||[])
|
|
.map(function (version) {
|
|
return minecraft_data.versionsByMinecraftVersion["pc"][version]
|
|
})
|
|
.filter(function (info) { return info })
|
|
.sort(function (a, b) { return b.version - a.version })
|
|
.concat(minecraft_data.postNettyVersionsByProtocolVersion["pc"][protocolVersion]||[])
|
|
if (versions.length === 0) {
|
|
client.emit('error',`unsupported/unknown protocol version: ${protocolVersion}, update minecraft-data`);
|
|
}
|
|
const minecraftVersion = versions[0].minecraftVersion;
|
|
|
|
debug(`Server version: ${minecraftVersion}, protocol: ${protocolVersion}`);
|
|
|
|
options.version = minecraftVersion;
|
|
options.protocolVersion = protocolVersion;
|
|
|
|
// Reinitialize client object with new version TODO: move out of its constructor?
|
|
client.version = minecraftVersion;
|
|
client.state = states.HANDSHAKING;
|
|
|
|
// Let other plugins such as Forge/FML (modinfo) respond to the ping response
|
|
if (client.autoVersionHooks) {
|
|
client.autoVersionHooks.forEach((hook) => {
|
|
hook(response, client, options);
|
|
});
|
|
}
|
|
|
|
// Finished configuring client object, let connection proceed
|
|
client.emit('connect_allowed');
|
|
client.wait_connect = false;
|
|
});
|
|
return client;
|
|
};
|