2016-01-23 15:02:41 -05:00
|
|
|
var mc = require('minecraft-protocol');
|
2016-01-23 16:49:48 -05:00
|
|
|
var ProtoDef = require('protodef').ProtoDef;
|
2016-01-23 15:02:41 -05:00
|
|
|
|
|
|
|
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({
|
2016-01-23 15:03:28 -05:00
|
|
|
forge: true,
|
2016-01-23 15:02:41 -05:00
|
|
|
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});
|
|
|
|
}
|
|
|
|
});
|
2016-01-23 16:49:48 -05:00
|
|
|
|
|
|
|
var proto = new ProtoDef();
|
2016-01-23 18:12:40 -05:00
|
|
|
// copied from ../../dist/transforms/serializer.js TODO: refactor
|
|
|
|
proto.addType("string", ["pstring", {
|
|
|
|
countType: "varint"
|
|
|
|
}]);
|
|
|
|
|
|
|
|
|
2016-01-23 17:00:59 -05:00
|
|
|
// http://wiki.vg/Minecraft_Forge_Handshake
|
2016-01-23 17:16:07 -05:00
|
|
|
proto.addType('FML|HS',
|
|
|
|
[
|
2016-01-23 16:49:48 -05:00
|
|
|
'container',
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"name": "discriminator",
|
|
|
|
"type": "byte"
|
2016-01-23 17:00:59 -05:00
|
|
|
},
|
2016-01-23 17:16:07 -05:00
|
|
|
|
|
|
|
// ServerHello
|
2016-01-23 17:00:59 -05:00
|
|
|
{
|
2016-01-23 17:23:32 -05:00
|
|
|
"name": "fmlProtocolVersionServer",
|
2016-01-23 17:00:59 -05:00
|
|
|
"type": [
|
|
|
|
"switch",
|
|
|
|
{
|
|
|
|
"compareTo": "discriminator",
|
|
|
|
"fields": {
|
|
|
|
"0": "byte"
|
|
|
|
},
|
|
|
|
"default": "void"
|
|
|
|
},
|
2016-01-23 17:08:20 -05:00
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "overrideDimension",
|
|
|
|
"type": [
|
|
|
|
"switch",
|
|
|
|
{
|
|
|
|
"compareTo": "discriminator",
|
|
|
|
"fields": {
|
2016-01-23 17:12:50 -05:00
|
|
|
"0": [
|
2016-01-23 17:08:20 -05:00
|
|
|
"switch",
|
|
|
|
{
|
|
|
|
// "Only sent if protocol version is greater than 1."
|
|
|
|
"compareTo": "fmlProtocolVersion",
|
|
|
|
"fields": {
|
|
|
|
"0": "void",
|
|
|
|
"1": "void"
|
|
|
|
},
|
|
|
|
"default": "int"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
2016-01-23 17:12:50 -05:00
|
|
|
"default": "void"
|
2016-01-23 17:08:20 -05:00
|
|
|
},
|
2016-01-23 17:12:50 -05:00
|
|
|
],
|
2016-01-23 17:16:07 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
// ClientHello
|
|
|
|
{
|
2016-01-23 17:23:32 -05:00
|
|
|
"name": "fmlProtocolVersionClient", // TODO: merge or fix name collision with fmlProtocolVersionServer?
|
2016-01-23 17:16:07 -05:00
|
|
|
"type": [
|
|
|
|
"switch",
|
|
|
|
{
|
|
|
|
"compareTo": "discriminator",
|
|
|
|
"fields": {
|
|
|
|
"1": "byte"
|
|
|
|
},
|
|
|
|
"default": "void"
|
|
|
|
}
|
|
|
|
],
|
2016-01-23 17:31:03 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
// ModList
|
|
|
|
{
|
2016-01-23 17:45:52 -05:00
|
|
|
"name": "mods",
|
2016-01-23 17:31:03 -05:00
|
|
|
"type": [
|
|
|
|
"switch",
|
|
|
|
{
|
|
|
|
"compareTo": "discriminator",
|
|
|
|
"fields": {
|
2016-01-23 17:45:52 -05:00
|
|
|
"2": [
|
|
|
|
"array",
|
|
|
|
{
|
|
|
|
"countType": "varint",
|
|
|
|
"type": [
|
|
|
|
"container",
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"name": "name",
|
|
|
|
"type": "string"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "version",
|
|
|
|
"type": "string"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
],
|
|
|
|
},
|
|
|
|
],
|
2016-01-23 17:31:03 -05:00
|
|
|
},
|
|
|
|
"default": "void"
|
|
|
|
}
|
|
|
|
// TODO: mods array: modname string, modversion string
|
|
|
|
],
|
|
|
|
},
|
2016-01-23 17:12:50 -05:00
|
|
|
]
|
2016-01-23 17:16:07 -05:00
|
|
|
]
|
|
|
|
);
|
2016-01-23 16:49:48 -05:00
|
|
|
|
2016-01-23 16:40:59 -05:00
|
|
|
client.on('custom_payload', function(packet) {
|
|
|
|
var channel = packet.channel;
|
|
|
|
var data = packet.data;
|
|
|
|
|
|
|
|
if (channel === 'REGISTER') {
|
|
|
|
var channels = data.toString().split('\0');
|
|
|
|
console.log('Server-side registered channels:',channels);
|
|
|
|
// TODO: do something?
|
|
|
|
// expect: [ 'FML|HS', 'FML', 'FML|MP', 'FML', 'FORGE' ]
|
2016-01-23 16:49:48 -05:00
|
|
|
} else if (channel === 'FML|HS') {
|
|
|
|
var parsed = proto.parsePacketBuffer('FML|HS', data);
|
|
|
|
console.log('FML|HS',parsed);
|
2016-01-23 17:23:32 -05:00
|
|
|
|
|
|
|
|
|
|
|
if (parsed.data.discriminator === 0) { // ServerHello
|
|
|
|
if (parsed.data.fmlProtocolVersionServer > 2) {
|
|
|
|
// TODO: support higher protocols, if they change
|
|
|
|
}
|
|
|
|
|
2016-01-23 17:26:11 -05:00
|
|
|
client.write('custom_payload', {
|
|
|
|
channel: 'REGISTER',
|
|
|
|
data: new Buffer(['FML|HS', 'FML', 'FML|MP', 'FML', 'FORGE'].join('\0'))
|
|
|
|
});
|
|
|
|
|
2016-01-23 17:23:32 -05:00
|
|
|
var clientHello = proto.createPacketBuffer('FML|HS', {
|
|
|
|
discriminator: 1, // ClientHello
|
|
|
|
fmlProtocolVersionClient: parsed.data.fmlProtocolVersionServer
|
|
|
|
});
|
|
|
|
|
|
|
|
client.write('custom_payload', {
|
|
|
|
channel: 'FML|HS',
|
|
|
|
data: clientHello
|
|
|
|
});
|
2016-01-23 17:31:03 -05:00
|
|
|
|
|
|
|
var modList = proto.createPacketBuffer('FML|HS', {
|
|
|
|
discriminator: 2, // ModList
|
2016-01-23 17:45:52 -05:00
|
|
|
mods: []
|
2016-01-23 17:31:03 -05:00
|
|
|
// TODO: send some mods
|
2016-01-23 17:45:52 -05:00
|
|
|
// TODO: fix Error: SizeOf error for mods.2.0.name : missing data type: string
|
|
|
|
/*
|
|
|
|
mods: [
|
|
|
|
{name:'IronChest', version:'6.0.121.768'}
|
|
|
|
]
|
|
|
|
*/
|
2016-01-23 17:31:03 -05:00
|
|
|
});
|
|
|
|
client.write('custom_payload', {
|
|
|
|
channel: 'FML|HS',
|
|
|
|
data: modList
|
|
|
|
});
|
2016-01-23 17:23:32 -05:00
|
|
|
}
|
2016-01-23 16:40:59 -05:00
|
|
|
}
|
|
|
|
});
|