Cleaned up code and used ES6 string templating

This commit is contained in:
AlexKvazos
2015-04-30 19:23:52 -05:00
parent 4b33fc3fff
commit c9af42d284
6 changed files with 46 additions and 30 deletions
+16 -16
View File
@@ -5,37 +5,37 @@ module.exports = (socket) => {
socket.on('server:connect', (data) => {
// log activity to console
console.log(`connecting > ${data.hostname}:${data.port} - ${data.username}`);
// inform user that connection is being made
socket.emit('buffer:info', `Connecting to server ${data.hostname}:${data.port}`);
// if a bot already exists, ask user to disconnect
if (socket.mcbot) {
socket.emit('buffer:error', 'Pleae disconnect before connecting again');
return;
}
// create mineflayer bot
// create a mineflayer bot and store it in the client's socket
socket.mcbot = mineflayer.createBot({
host: data.hostname,
port: data.port,
username: data.username,
password: data.password
host: data.hostname,
port: data.port,
username: data.username,
password: data.password
});
// store connection params in socket
socket.connectionParams = data;
// prepare for errors
socket.mcbot.on('error', (error) => {
if (error.toString() === 'Error: write after end') return;
socket.emit('buffer:error', error);
if (socket.mcbot.entity) socket.mcbot.end();
delete socket.mcbot;
});
// bind bot events
events(socket);
// debug
console.log('connecting > ' + data.hostname + ':' + data.port + ' - ' + ' Username: ' + data.username);
socket.emit('buffer:info', 'Connecting to server ' + data.hostname + ':' + data.port);
});
};