Support vanilla servers (more less)

This commit is contained in:
AlexKvazos
2015-04-28 22:19:41 -05:00
parent 1916d33828
commit 80ed83f1ab
2 changed files with 70 additions and 4 deletions
+69 -3
View File
@@ -19,10 +19,19 @@ function stringToCode(string) {
'white': 'f'
};
return dictionary[string] || string;
return dictionary[string] || 'f';
}
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
}
module.exports = function(socket) {
var bot = socket.mcbot;
@@ -46,21 +55,75 @@ module.exports = function(socket) {
// message event
bot.on('message', function(message) {
console.log(message);
// empty buffer
var buffer = '';
// modded servers match here
if (message.extra) {
// for each piece of text
message.extra.forEach(function(data) {
var text = data.text; // get the text
if (text) { // if text is available
// get the text out of the element
var text;
if (typeof data === 'string') {
text = data;
} else if (typeof data === 'object') {
text = data.text;
}
// if text is available
if (text) {
text = text.replace(/§k/ig, ''); // remove crazy format
text = text.replace(/§l/ig, ''); // remove bold format
buffer += '§' + stringToCode(data.color) + text; // add the text to the buffer
}
});
// vanilla server matches here
} else if (message.with) {
var text;
switch (message.translate) {
case 'chat.type.announcement':
text = '§d[' + message.with[0].text + '] ';
message.with[1].extra.forEach(function(x) {
text += x;
});
break;
case 'chat.type.admin':
if (message.with[1].translate === 'commands.op.success') {
text = '§eOpped ' + message.with[1].with;
}
break;
case 'commands.players.list':
text = '§eThere are ' + message.with[0] + '/' + message.with[1] + ' players online.';
break;
case 'commands.kick.success':
text = '§e' + message.with[0] + ' kicked!';
break;
case 'commands.whitelist.add.success':
text = '§f' + message.with[0] + ' whitelisted';
break;
case 'commands.whitelist.remove.success':
text = '§f' + message.with[0] + ' removed from whitelist';
break;
case 'commands.whitelist.list':
text = '§f' + message.with[0] + ' players in the whitelist';
break;
case 'commands.generic.usage':
text = '§cInvalid command usage';
if (message.with[0].translate === 'commands.whitelist.usage') {
console.log(message.with[0].json);
}
break;
}
buffer += text || '[i] [MinecraftChat] Unknown data received from server. [Unsuported Server]';
} else if (message.text) {
buffer += message.text;
} else {
@@ -71,6 +134,9 @@ module.exports = function(socket) {
return;
}
// escape any html in the buffer
buffer = escapeHtml(buffer);
// format the buffer with the correct coloring
buffer = buffer.replace(/§([0-9abcdef])([^§]*)/ig, function replace(regex, color, msg) {
return '<span class="color-'+color+'">'+msg.replace(' ', '&nbsp;')+'</span>';
+1 -1
View File
@@ -66,7 +66,7 @@ body {
.color-c { color: #EA3A3F; }
.color-d { color: #EF1DFF; }
.color-e { color: #FAFF34; }
.color-f { color: #111111; }
.color-f { color: #EDEDED; }
.color-0 { color: #000000; }
.color-1 { color: #19009C; }
.color-2 { color: #30A000; }