clean up and fix client_chat

This commit is contained in:
Romain Beaumont 2015-09-20 22:00:43 +02:00
parent 2c93a4e796
commit a3cd6c49ad

View file

@ -4,38 +4,40 @@ var mc = require('../../');
var states = mc.states;
var util = require('util');
var colors = new Array();
colors["black"] = 'black+white_bg';
colors["dark_blue"] = 'blue';
colors["dark_green"] = 'green';
colors["dark_aqua"] = 'cyan'
colors["dark_red"] = 'red'
colors["dark_purple"] = 'magenta'
colors["gold"] = 'yellow'
colors["gray"] = 'black+white_bg'
colors["dark_gray"] = 'black+white_bg'
colors["blue"] = 'blue'
colors["green"] = 'green'
colors["aqua"] = 'cyan'
colors["red"] = 'red'
colors["light_purple"] = 'magenta'
colors["yellow"] = 'yellow'
colors["white"] = 'white'
colors["obfuscated"] = 'blink'
colors["bold"] = 'bold'
colors["strikethrough"] = ''
colors["underlined"] = 'underlined'
colors["italic"] = ''
colors["reset"] = 'white+black_bg'
var colors = {
"black": 'black+white_bg',
"dark_blue": 'blue',
"dark_green": 'green',
"dark_aqua": 'cyan',
"dark_red": 'red',
"dark_purple": 'magenta',
"gold": 'yellow',
"gray": 'black+white_bg',
"dark_gray": 'black+white_bg',
"blue": 'blue',
"green": 'green',
"aqua": 'cyan',
"red": 'red',
"light_purple": 'magenta',
"yellow": 'yellow',
"white": 'white',
"obfuscated": 'blink',
"bold": 'bold',
"strikethrough": '',
"underlined": 'underlined',
"italic": '',
"reset": 'white+black_bg'
};
var dictionary = {};
dictionary["chat.stream.emote"] = "(%s) * %s %s";
dictionary["chat.stream.text"] = "(%s) <%s> %s";
dictionary["chat.type.achievement"] = "%s has just earned the achievement %s";
dictionary["chat.type.admin"] = "[%s: %s]";
dictionary["chat.type.announcement"] = "[%s] %s";
dictionary["chat.type.emote"] = "* %s %s";
dictionary["chat.type.text"] = "<%s> %s";
var dictionary = {
"chat.stream.emote": "(%s) * %s %s",
"chat.stream.text": "(%s) <%s> %s",
"chat.type.achievement": "%s has just earned the achievement %s",
"chat.type.admin": "[%s: %s]",
"chat.type.announcement": "[%s] %s",
"chat.type.emote": "* %s %s",
"chat.type.text": "<%s> %s"
};
var rl = readline.createInterface({
input: process.stdin,
@ -44,10 +46,10 @@ var rl = readline.createInterface({
});
function print_help() {
console.log("usage: node client_chat.js <hostname> <user> [<password>]");
console.log("usage: node client_chat.js <hostname> <port> <user> [<password>]");
}
if(process.argv.length < 4) {
if(process.argv.length < 5) {
console.log("Too few arguments!");
print_help();
process.exit(1);
@ -61,9 +63,9 @@ process.argv.forEach(function(val, index, array) {
});
var host = process.argv[2];
var port = 25565;
var user = process.argv[3];
var passwd = process.argv[4];
var port = parseInt(process.argv[3]);
var user = process.argv[4];
var passwd = process.argv[5];
if(host.indexOf(':') != -1) {
port = host.substring(host.indexOf(':') + 1);
@ -80,7 +82,7 @@ var client = mc.createClient({
password: passwd
});
client.on([states.PLAY, 0x40], function(packet) { // you can listen for packets by [state, id], too
client.on('kick_disconnect', function(packet) {
console.info(color('Kicked for ' + packet.reason, "blink+red"));
process.exit(1);
});
@ -108,22 +110,21 @@ client.on('state', function(newState) {
client.write('chat', {message: chat});
});
}
})
});
rl.on('line', function(line) {
if(line == '') {
return;
} else if(line == '/quit') {
var reason = 'disconnect.quitting';
console.info('Disconnected from ' + host + ':' + port);
client.write([states.PLAY, 0x40], {reason: reason});
client.end();
return;
} else if(line == '/end') {
console.info('Forcibly ended client');
process.exit(0);
return;
}
if(!client.write([states.PLAY, 0x01], {message: line})) {
if(!client.write('chat', {message: line})) {
chats.push(line);
}
});