Merge pull request from rom1504/fix_client_chat

clean up and fix client_chat
This commit is contained in:
Robin Lambertz 2015-09-23 20:25:03 +02:00
commit a337e5ae3e

View file

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