mirror of
https://github.com/Threnklyn/MinecraftChat.git
synced 2026-06-02 19:29:20 +02:00
Support vanilla servers (more less)
This commit is contained in:
@@ -19,10 +19,19 @@ function stringToCode(string) {
|
|||||||
'white': 'f'
|
'white': 'f'
|
||||||
};
|
};
|
||||||
|
|
||||||
return dictionary[string] || string;
|
return dictionary[string] || 'f';
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function escapeHtml(unsafe) {
|
||||||
|
return unsafe
|
||||||
|
.replace(/&/g, "&")
|
||||||
|
.replace(/</g, "<")
|
||||||
|
.replace(/>/g, ">")
|
||||||
|
.replace(/"/g, """)
|
||||||
|
.replace(/'/g, "'");
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = function(socket) {
|
module.exports = function(socket) {
|
||||||
|
|
||||||
var bot = socket.mcbot;
|
var bot = socket.mcbot;
|
||||||
@@ -46,21 +55,75 @@ module.exports = function(socket) {
|
|||||||
// message event
|
// message event
|
||||||
bot.on('message', function(message) {
|
bot.on('message', function(message) {
|
||||||
|
|
||||||
|
console.log(message);
|
||||||
|
|
||||||
// empty buffer
|
// empty buffer
|
||||||
var buffer = '';
|
var buffer = '';
|
||||||
|
|
||||||
|
// modded servers match here
|
||||||
if (message.extra) {
|
if (message.extra) {
|
||||||
|
|
||||||
// for each piece of text
|
// for each piece of text
|
||||||
message.extra.forEach(function(data) {
|
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(/§k/ig, ''); // remove crazy format
|
||||||
text = text.replace(/§l/ig, ''); // remove bold format
|
text = text.replace(/§l/ig, ''); // remove bold format
|
||||||
buffer += '§' + stringToCode(data.color) + text; // add the text to the buffer
|
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) {
|
} else if (message.text) {
|
||||||
buffer += message.text;
|
buffer += message.text;
|
||||||
} else {
|
} else {
|
||||||
@@ -71,6 +134,9 @@ module.exports = function(socket) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// escape any html in the buffer
|
||||||
|
buffer = escapeHtml(buffer);
|
||||||
|
|
||||||
// format the buffer with the correct coloring
|
// format the buffer with the correct coloring
|
||||||
buffer = buffer.replace(/§([0-9abcdef])([^§]*)/ig, function replace(regex, color, msg) {
|
buffer = buffer.replace(/§([0-9abcdef])([^§]*)/ig, function replace(regex, color, msg) {
|
||||||
return '<span class="color-'+color+'">'+msg.replace(' ', ' ')+'</span>';
|
return '<span class="color-'+color+'">'+msg.replace(' ', ' ')+'</span>';
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ body {
|
|||||||
.color-c { color: #EA3A3F; }
|
.color-c { color: #EA3A3F; }
|
||||||
.color-d { color: #EF1DFF; }
|
.color-d { color: #EF1DFF; }
|
||||||
.color-e { color: #FAFF34; }
|
.color-e { color: #FAFF34; }
|
||||||
.color-f { color: #111111; }
|
.color-f { color: #EDEDED; }
|
||||||
.color-0 { color: #000000; }
|
.color-0 { color: #000000; }
|
||||||
.color-1 { color: #19009C; }
|
.color-1 { color: #19009C; }
|
||||||
.color-2 { color: #30A000; }
|
.color-2 { color: #30A000; }
|
||||||
|
|||||||
Reference in New Issue
Block a user