-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PrismarineJS/node-minecraft-protocol#331 Add dynamic cross-protocol support
- Loading branch information
Showing
15 changed files
with
143 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
var mc = require('minecraft-protocol'); | ||
|
||
if(process.argv.length < 4 || process.argv.length > 6) { | ||
console.log("Usage : node echo.js <host> <port> [<name>] [<password>]"); | ||
process.exit(1); | ||
} | ||
|
||
var client = mc.createClient({version: false, | ||
host: process.argv[2], | ||
port: parseInt(process.argv[3]), | ||
username: process.argv[4] ? process.argv[4] : "echo", | ||
password: process.argv[5] | ||
}); | ||
|
||
client.on('connect', function() { | ||
console.info('connected'); | ||
}); | ||
client.on('disconnect', function(packet) { | ||
console.log('disconnected: '+ packet.reason); | ||
}); | ||
client.on('chat', function(packet) { | ||
var jsonMsg = JSON.parse(packet.message); | ||
if(jsonMsg.translate == 'chat.type.announcement' || jsonMsg.translate == 'chat.type.text') { | ||
var username = jsonMsg.with[0].text; | ||
var msg = jsonMsg.with[1]; | ||
if(username === client.username) return; | ||
client.write('chat', {message: msg}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "node-minecraft-protocol-example", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
}, | ||
"description": "A node-minecraft-protocol example" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
'use strict'; | ||
|
||
var ping = require('../ping'); | ||
var debug = require('../debug'); | ||
var states = require('../states'); | ||
var assert = require('assert'); | ||
var minecraft_data = require('minecraft-data'); | ||
|
||
// Get the minecraft-data version string for a protocol version | ||
// TODO: add to node-minecraft-data index (protocol to newest release, if multiple) | ||
function protocolVersion2MinecraftVersion(n) { | ||
var usesNetty = true; // for now, only Netty protocols are supported TODO: pre-Netty (beware, colliding protocol version numbers) | ||
for (var i = 0; i < minecraft_data.versions.length; ++i) { | ||
var version = minecraft_data.versions[i]; | ||
if (version.version === n && version.usesNetty === usesNetty) { | ||
return [version.minecraftVersion, version.majorVersion]; | ||
} | ||
} | ||
|
||
throw new Error(`unsupported/unknown protocol version: ${n}, update minecraft-data`); | ||
} | ||
|
||
module.exports = function(client) { | ||
var options = client.options; | ||
|
||
options.wait_connect = true; // don't let src/client/setProtocol proceed on socket 'connect' until 'connect_allowed' | ||
debug('pinging',options.host); | ||
var pingOptions = {host: options.host, port: options.port}; | ||
// TODO: use 0xfe ping instead for better compatibility/performance? https://github.com/deathcap/node-minecraft-ping | ||
ping(pingOptions, function(err, response) { | ||
if (err) throw err; // hmm | ||
debug('ping response',response); | ||
// TODO: could also use ping pre-connect to save description, type, max players, etc. | ||
var motd = response.description; | ||
debug('Server description:',motd); // TODO: save | ||
|
||
// Pass server-reported version to protocol handler | ||
// The version string is interpereted by https://github.com/PrismarineJS/node-minecraft-data | ||
var minecraftVersion = response.version.name; // 1.8.9, 1.7.10 | ||
var protocolVersion = response.version.protocol;// 47, 5 | ||
|
||
debug(`Server version: ${minecraftVersion}, protocol: ${protocolVersion}`); | ||
// Note that versionName is a descriptive version stirng like '1.8.9' on vailla, but other | ||
// servers add their own name (Spigot 1.8.8, Glowstone++ 1.8.9) so we cannot use it directly, | ||
// even though it is in a format accepted by minecraft-data. Instead, translate the protocol. | ||
var [minecraftVersion, majorVersion] = protocolVersion2MinecraftVersion(protocolVersion); | ||
client.options.version = minecraftVersion; | ||
client.options.protocolVersion = protocolVersion; | ||
|
||
// Reinitialize client object with new version TODO: move out of its constructor? | ||
client.version = majorVersion; | ||
client.state = states.HANDSHAKING; | ||
|
||
if (response.modinfo && response.modinfo.type === 'FML') { | ||
// Use the list of Forge mods from the server ping, so client will match server | ||
var forgeMods = response.modinfo.modList; | ||
debug('Using forgeMods:',forgeMods); | ||
// TODO: https://github.com/PrismarineJS/node-minecraft-protocol/issues/114 | ||
// https://github.com/PrismarineJS/node-minecraft-protocol/pull/326 | ||
// TODO: modify client object to set forgeMods and enable forgeHandshake | ||
throw new Error('FML/Forge not yet supported'); | ||
} | ||
// Finished configuring client object, let connection proceed | ||
client.emit('connect_allowed'); | ||
}); | ||
return client; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters