mirror of
https://github.com/Threnklyn/MinecraftChat.git
synced 2026-05-18 20:33:28 +02:00
Modularized event listeners
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
module.exports = function(socket) {
|
||||
|
||||
socket.mcbot.on('end', function() {
|
||||
socket.emit('bot:disconnect');
|
||||
socket.mcbot = null;
|
||||
});
|
||||
|
||||
};
|
||||
@@ -0,0 +1,13 @@
|
||||
module.exports = function(socket) {
|
||||
|
||||
// login event
|
||||
socket.mcbot.on('login', function() {
|
||||
socket.emit('buffer:success', 'Successfully logged in as ' + socket.mcbot.username + ' with entity id ' + socket.mcbot.entity.id);
|
||||
socket.emit('bot:connect', {
|
||||
host: socket.connectionParams.hostname,
|
||||
port: socket.connectionParams.port,
|
||||
username: socket.connectionParams.username
|
||||
});
|
||||
});
|
||||
|
||||
};
|
||||
@@ -1,59 +1,10 @@
|
||||
function stringToCode(string) {
|
||||
|
||||
var dictionary = {
|
||||
'black': 0,
|
||||
'dark_blue': 1,
|
||||
'dark_green': 2,
|
||||
'dark_aqua': 3,
|
||||
'dark_red': 4,
|
||||
'dark_purple': 5,
|
||||
'gold': 6,
|
||||
'gray': 7,
|
||||
'dark_gray': 8,
|
||||
'indigo': 9,
|
||||
'green': 'a',
|
||||
'aqua': 'b',
|
||||
'red': 'c',
|
||||
'light_purple': 'd',
|
||||
'yellow': 'e',
|
||||
'white': 'f'
|
||||
};
|
||||
|
||||
return dictionary[string] || 'f';
|
||||
|
||||
}
|
||||
|
||||
function escapeHtml(unsafe) {
|
||||
return unsafe
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/"/g, """)
|
||||
.replace(/'/g, "'");
|
||||
}
|
||||
var stringToCode = require('../../utils').stringToCode;
|
||||
var escapeHtml = require('../../utils').escapeHtml;
|
||||
|
||||
module.exports = function(socket) {
|
||||
|
||||
var bot = socket.mcbot;
|
||||
|
||||
// login event
|
||||
bot.on('login', function() {
|
||||
socket.emit('buffer:success', 'Successfully logged in as ' + bot.username + ' with entity id ' + bot.entity.id);
|
||||
socket.emit('bot:connect', {
|
||||
host: socket.connectionParams.hostname,
|
||||
port: socket.connectionParams.port,
|
||||
username: socket.connectionParams.username
|
||||
});
|
||||
});
|
||||
|
||||
// spawn event
|
||||
bot.on('spawn', function() {
|
||||
var pos = bot.entity.position;
|
||||
socket.emit('buffer:info', 'Spawned at X:' + pos.x + ', Y:' + pos.y + ', Z:' + pos.z);
|
||||
});
|
||||
|
||||
// message event
|
||||
bot.on('message', function(message) {
|
||||
socket.mcbot.on('message', function(message) {
|
||||
|
||||
// empty buffer
|
||||
var buffer = '';
|
||||
@@ -142,9 +93,4 @@ module.exports = function(socket) {
|
||||
|
||||
});
|
||||
|
||||
bot.on('end', function() {
|
||||
socket.emit('bot:disconnect');
|
||||
socket.mcbot = null;
|
||||
});
|
||||
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,9 @@
|
||||
module.exports = function(socket) {
|
||||
|
||||
// spawn event
|
||||
socket.mcbot.on('spawn', function() {
|
||||
var pos = socket.mcbot.entity.position;
|
||||
socket.emit('buffer:info', 'Spawned at X:' + pos.x + ', Y:' + pos.y + ', Z:' + pos.z);
|
||||
});
|
||||
|
||||
};
|
||||
@@ -1,5 +1,9 @@
|
||||
module.exports = function(socket) {
|
||||
|
||||
require('./events')(socket);
|
||||
// bind all listeners to the bot
|
||||
require('./events/login')(socket);
|
||||
require('./events/spawn')(socket);
|
||||
require('./events/message')(socket);
|
||||
require('./events/end')(socket);
|
||||
|
||||
};
|
||||
|
||||
+1
-1
@@ -43,4 +43,4 @@ server.listen(app.get('port'), function() {
|
||||
// handle exceptions
|
||||
process.on('uncaughtException', function(ex) {
|
||||
console.error(ex.stack);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
var mineflayer = require('mineflayer');
|
||||
|
||||
|
||||
module.exports = function(io) {
|
||||
|
||||
io.on('connection', function(socket) {
|
||||
|
||||
// bind all events to the socket
|
||||
// bind all listeners to the socket
|
||||
require('./events/connection')(socket);
|
||||
require('./events/disconnection')(socket);
|
||||
require('./events/chat')(socket);
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
module.exports = function escapeHtml(unsafe) {
|
||||
return unsafe
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/"/g, """)
|
||||
.replace(/'/g, "'");
|
||||
};
|
||||
@@ -0,0 +1,2 @@
|
||||
exports.escapeHtml = require('./escapeHtml');
|
||||
exports.stringToCode = require('./stringToCode');
|
||||
@@ -0,0 +1,24 @@
|
||||
module.exports = function stringToCode(string) {
|
||||
|
||||
var dictionary = {
|
||||
'black': 0,
|
||||
'dark_blue': 1,
|
||||
'dark_green': 2,
|
||||
'dark_aqua': 3,
|
||||
'dark_red': 4,
|
||||
'dark_purple': 5,
|
||||
'gold': 6,
|
||||
'gray': 7,
|
||||
'dark_gray': 8,
|
||||
'indigo': 9,
|
||||
'green': 'a',
|
||||
'aqua': 'b',
|
||||
'red': 'c',
|
||||
'light_purple': 'd',
|
||||
'yellow': 'e',
|
||||
'white': 'f'
|
||||
};
|
||||
|
||||
return dictionary[string] || 'f';
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user