node-minecraft-protocol/examples/client_auto/client_auto.js

33 lines
913 B
JavaScript
Raw Normal View History

2016-04-27 19:27:32 -04:00
'use strict';
2016-01-24 21:53:34 -05:00
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,
2016-01-24 21:53:34 -05:00
host: process.argv[2],
port: parseInt(process.argv[3]),
username: process.argv[4] ? process.argv[4] : "echo",
password: process.argv[5]
});
2016-01-24 21:53:34 -05:00
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-24 21:53:34 -05:00
});