mirror of
https://github.com/Threnklyn/MinecraftChat.git
synced 2026-06-03 19:58:27 +02:00
Cleaned up code and used ES6 string templating
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
module.exports = (socket) => {
|
||||
|
||||
socket.mcbot.on('error', (error) => {
|
||||
|
||||
// this is okay because the connection was ended
|
||||
if (error.toString() === 'Error: write after end') return;
|
||||
|
||||
// log the error in the client's buffer
|
||||
socket.emit('buffer:error', error);
|
||||
|
||||
// if the bot is logged in and/or is an entity, destroy it
|
||||
if (socket.mcbot.entity) socket.mcbot.end();
|
||||
|
||||
// delete the bot
|
||||
delete socket.mcbot;
|
||||
|
||||
});
|
||||
|
||||
};
|
||||
@@ -1,6 +1,5 @@
|
||||
module.exports = (socket) => {
|
||||
|
||||
// login event
|
||||
socket.mcbot.on('login', () => {
|
||||
socket.emit('buffer:success', 'Successfully logged in as ' + socket.mcbot.username + ' with entity id ' + socket.mcbot.entity.id);
|
||||
socket.emit('bot:connect', {
|
||||
|
||||
@@ -1,20 +1,16 @@
|
||||
import parseVanilla from '../../parsers/vanilla';
|
||||
import parseExtra from '../../parsers/extra';
|
||||
import {escapeHtml} from '../../utils';
|
||||
|
||||
|
||||
var escapeHtml = require('../../utils').escapeHtml;
|
||||
|
||||
module.exports = (socket) => {
|
||||
|
||||
// message event
|
||||
socket.mcbot.on('message', (message) => {
|
||||
|
||||
// empty buffer
|
||||
var buffer = '';
|
||||
|
||||
|
||||
|
||||
|
||||
// parse for json objects with 'extra'
|
||||
if (message.extra) {
|
||||
buffer = parseExtra(message.extra);
|
||||
@@ -39,9 +35,10 @@ module.exports = (socket) => {
|
||||
// escape any html in the buffer
|
||||
buffer = escapeHtml(buffer);
|
||||
|
||||
// format the buffer with the correct coloring
|
||||
// format the buffer with the correct coloring and format whitespace
|
||||
buffer = buffer.replace(/§([0-9abcdef])([^§]*)/ig, (regex, color, msg) => {
|
||||
return '<span class="color-' + color + '">' + msg.replace(/ /g, ' ') + '</span>';
|
||||
msg = msg.replace(/ /g, ' ');
|
||||
return `<span class="color-${color}">${msg}</span>`;
|
||||
});
|
||||
|
||||
// send line back to the client
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
module.exports = (socket) => {
|
||||
|
||||
// spawn event
|
||||
socket.mcbot.on('spawn', () => {
|
||||
var pos = socket.mcbot.entity.position;
|
||||
socket.emit('buffer:info', 'Spawned at X:' + pos.x + ', Y:' + pos.y + ', Z:' + pos.z);
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
// bind all listeners to the bot
|
||||
import login from './events/login';
|
||||
import spawn from './events/spawn';
|
||||
import message from './events/message';
|
||||
import end from './events/end';
|
||||
import error from './events/error';
|
||||
import login from './events/login';
|
||||
import spawn from './events/spawn';
|
||||
import message from './events/message';
|
||||
import end from './events/end';
|
||||
|
||||
// bind all listeners to the bot
|
||||
module.exports = (socket) => {
|
||||
|
||||
error(socket);
|
||||
login(socket);
|
||||
spawn(socket);
|
||||
message(socket);
|
||||
|
||||
@@ -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