mirror of
https://github.com/Threnklyn/MinecraftChat.git
synced 2026-05-19 04:43:28 +02:00
Working connections with chat event listener
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
var mineflayer = require('mineflayer');
|
||||
|
||||
module.exports = function(socket) {
|
||||
|
||||
socket.on('server:connect', function(data, response) {
|
||||
|
||||
if (socket.mcbot) {
|
||||
socket.mcbot.end();
|
||||
}
|
||||
|
||||
// create mineflayer bot
|
||||
socket.mcbot = mineflayer.createBot({
|
||||
host: data.hostname,
|
||||
port: data.port,
|
||||
username: data.username,
|
||||
password: data.password
|
||||
});
|
||||
|
||||
// prepare for errors
|
||||
socket.mcbot.on('error', function(error) {
|
||||
socket.emit('buffer:error', error);
|
||||
});
|
||||
|
||||
// bind bot events
|
||||
require('../../bot')(socket);
|
||||
|
||||
// debug
|
||||
console.log('login > ' + data.hostname + ':' + data.port + ' - ' + ' Username: ' + data.username);
|
||||
socket.emit('buffer:info', 'Connecting to server ' + data.hostname + ':' + data.port);
|
||||
|
||||
});
|
||||
|
||||
};
|
||||
@@ -0,0 +1,9 @@
|
||||
module.exports = function(socket) {
|
||||
|
||||
socket.on('disconnect', function() {
|
||||
if (socket.mcbot) {
|
||||
socket.mcbot.end();
|
||||
}
|
||||
});
|
||||
|
||||
};
|
||||
@@ -5,22 +5,9 @@ module.exports = function(io) {
|
||||
|
||||
io.on('connection', function(socket) {
|
||||
|
||||
socket.on('server:connect', function(data, response) {
|
||||
socket.mcbot = mineflayer.createBot({
|
||||
host: data.hostname,
|
||||
port: data.port,
|
||||
username: data.username,
|
||||
password: data.password
|
||||
});
|
||||
socket.mcbot.on('error', function(error) {
|
||||
console.log(error);
|
||||
socket.emit('bot:error', error);
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('disconnect', function() {
|
||||
socket.mcbot = null;
|
||||
});
|
||||
// bind all events to the socket
|
||||
require('./events/connection')(socket);
|
||||
require('./events/disconnection')(socket);
|
||||
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user