mirror of
https://github.com/Threnklyn/MinecraftChat.git
synced 2026-06-13 17:43:31 +02:00
Cleaned up code and used ES6 string templating
This commit is contained in:
@@ -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);
|
||||
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user