2024-10-11 14:09:07 -04:00
|
|
|
const lang = require("./en_us.json"); // translate message
|
2024-10-12 08:11:35 -04:00
|
|
|
const nbt = require('prismarine-nbt');
|
2024-09-20 12:01:17 -04:00
|
|
|
|
2024-10-12 08:11:35 -04:00
|
|
|
function uuidFromIntArray (arr) {
|
|
|
|
const buf = Buffer.alloc(16)
|
|
|
|
arr.forEach((num, index) => { buf.writeInt32BE(num, index * 4) })
|
|
|
|
return buf.toString('hex')
|
2024-09-20 12:01:17 -04:00
|
|
|
}
|
|
|
|
|
2024-10-12 08:11:35 -04:00
|
|
|
function processNbtMessage(msg) {
|
|
|
|
try { // RangeError: Maximum call stack size exceeded
|
|
|
|
if (!msg || msg.type === 'end' || msg === undefined) return undefined
|
|
|
|
const simplified = nbt.simplify(msg)
|
|
|
|
const json = JSON.stringify(simplified, (key, val) => {
|
|
|
|
if (key === 'id' && Array.isArray(val)) return uuidFromIntArray(val)
|
|
|
|
return val
|
|
|
|
})
|
|
|
|
return json
|
|
|
|
} catch (e) {
|
|
|
|
return '{"text":""}';
|
2024-09-20 12:01:17 -04:00
|
|
|
}
|
2024-10-12 08:11:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function inject(bot) {
|
|
|
|
|
|
|
|
bot.on('system_chat', (packet) => { // system
|
|
|
|
if (packet.isActionBar) return;
|
|
|
|
let systemchat = {};
|
|
|
|
systemchat.jsonMsg = packet.content;
|
|
|
|
systemchat.message = parseMinecraftMessage(processNbtMessage(packet.content));
|
|
|
|
systemchat.nocolor_message = parseMinecraftMessageNoColor(processNbtMessage(packet.content));
|
|
|
|
bot.emit('Custom_SystemChat', systemchat.message, systemchat);
|
|
|
|
bot.emit('Custom_AllChat', systemchat.message, systemchat);
|
2024-09-20 12:01:17 -04:00
|
|
|
});
|
|
|
|
|
2024-10-12 08:11:35 -04:00
|
|
|
|
|
|
|
bot.on('profileless_chat', (packet) => { // sudo and vanish
|
|
|
|
let profilelesschat = {};
|
|
|
|
|
|
|
|
profilelesschat.type = packet.type
|
|
|
|
|
|
|
|
profilelesschat.formattedMessage = parseMinecraftMessage(processNbtMessage(packet.message))
|
|
|
|
profilelesschat.senderName = parseMinecraftMessage(processNbtMessage(packet.name))
|
|
|
|
profilelesschat.targetName = parseMinecraftMessage(processNbtMessage(packet.target))
|
|
|
|
|
|
|
|
profilelesschat.nocolor_formattedMessage = parseMinecraftMessageNoColor(processNbtMessage(packet.message))
|
|
|
|
profilelesschat.nocolor_senderName = parseMinecraftMessageNoColor(processNbtMessage(packet.name))
|
|
|
|
profilelesschat.nocolor_targetName = parseMinecraftMessageNoColor(processNbtMessage(packet.target))
|
|
|
|
|
|
|
|
bot.emit('Custom_ProfilelessChat', profilelesschat.formattedMessage, profilelesschat)
|
|
|
|
bot.emit('Custom_AllChat', profilelesschat.formattedMessage, profilelesschat)
|
|
|
|
})
|
|
|
|
|
|
|
|
bot.on('player_chat', (packet) => { // player
|
|
|
|
let playerchat = {};
|
|
|
|
|
|
|
|
playerchat.plainMessage = packet.plainMessage;
|
|
|
|
playerchat.type = packet.type;
|
|
|
|
playerchat.sender = packet.senderUuid;
|
|
|
|
|
|
|
|
playerchat.unsignedContent = parseMinecraftMessage(processNbtMessage(packet.unsignedChatContent));
|
|
|
|
playerchat.formattedMessage = parseMinecraftMessage(processNbtMessage(packet.formattedMessage));
|
|
|
|
playerchat.senderName = parseMinecraftMessage(processNbtMessage(packet.networkName));
|
|
|
|
playerchat.targetName = parseMinecraftMessage(processNbtMessage(packet.networkTargetName));
|
|
|
|
|
|
|
|
playerchat.nocolor_unsignedContent = parseMinecraftMessageNoColor(processNbtMessage(packet.unsignedChatContent));
|
|
|
|
playerchat.nocolor_formattedMessage = parseMinecraftMessageNoColor(processNbtMessage(packet.formattedMessage));
|
|
|
|
playerchat.nocolor_senderName = parseMinecraftMessageNoColor(processNbtMessage(packet.networkName));
|
|
|
|
playerchat.nocolor_targetName = parseMinecraftMessageNoColor(processNbtMessage(packet.networkTargetName));
|
|
|
|
|
|
|
|
switch (playerchat.type) {
|
2024-09-20 12:01:17 -04:00
|
|
|
case 1: // /me text
|
2024-10-12 08:11:35 -04:00
|
|
|
msg = `* ${playerchat.senderName} ${playerchat.formattedMessage}`;
|
|
|
|
playerchat.nocolor_msg = `* ${playerchat.nocolor_senderName} ${playerchat.nocolor_formattedMessage}`;
|
2024-09-20 12:01:17 -04:00
|
|
|
break;
|
|
|
|
case 2: // someone /tell you text
|
2024-10-12 08:11:35 -04:00
|
|
|
msg = `${playerchat.senderName} whispers to you: ${playerchat.plainMessage || playerchat.formattedMessage}`;
|
|
|
|
playerchat.nocolor_msg = `${playerchat.nocolor_senderName} whispers to you: ${playerchat.plainMessage || playerchat.nocolor_formattedMessage}`;
|
2024-09-20 12:01:17 -04:00
|
|
|
break;
|
|
|
|
case 3: // you /tell someone text
|
2024-10-12 08:11:35 -04:00
|
|
|
msg = `You whisper to ${playerchat.targetName}: ${playerchat.plainMessage || playerchat.formattedMessage}`;
|
|
|
|
playerchat.nocolor_msg = `You whisper to ${NoColorTargetName}: ${playerchat.plainMessage || playerchat.nocolor_formattedMessage}`;
|
2024-09-20 12:01:17 -04:00
|
|
|
break;
|
|
|
|
case 4: // player chat
|
2024-10-12 08:11:35 -04:00
|
|
|
msg = playerchat.unsignedContent;
|
|
|
|
playerchat.nocolor_msg = playerchat.nocolor_unsignedContent;
|
2024-09-20 12:01:17 -04:00
|
|
|
break;
|
|
|
|
case 5: // /say text
|
2024-10-12 08:11:35 -04:00
|
|
|
msg = `[${playerchat.senderName}] ${playerchat.plainMessage || playerchat.formattedMessage}`;
|
|
|
|
playerchat.nocolor_msg = `[${playerchat.nocolor_senderName}] ${playerchat.plainMessage || playerchat.nocolor_formattedMessage}`;
|
2024-09-20 12:01:17 -04:00
|
|
|
break;
|
|
|
|
case 6: // /minecraft:teammsg text
|
2024-10-12 08:11:35 -04:00
|
|
|
msg = `${playerchat.targetName} <${playerchat.senderName}> ${playerchat.plainMessage}`;
|
|
|
|
playerchat.nocolor_msg = `${playerchat.nocolor_targetName} <${playerchat.nocolor_senderName}> ${playerchat.plainMessage}`;
|
2024-09-20 12:01:17 -04:00
|
|
|
break;
|
|
|
|
default:
|
2024-10-12 08:11:35 -04:00
|
|
|
console.log(`Unknown player_chat packet. Type: ${playerchat.type}`);
|
|
|
|
console.log(packet);
|
2024-09-20 12:01:17 -04:00
|
|
|
break;
|
|
|
|
}
|
2024-10-12 08:11:35 -04:00
|
|
|
bot.emit('custom_PlayerChat', msg, playerchat);
|
|
|
|
bot.emit('Custom_AllChat', msg, playerchat)
|
2024-09-20 12:01:17 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
const ansiColorCodes = {
|
|
|
|
'§0': '\x1B[30m', '§1': '\x1B[34m', '§2': '\x1B[32m', '§3': '\x1B[36m',
|
|
|
|
'§4': '\x1B[31m', '§5': '\x1B[35m', '§6': '\x1B[33m', '§7': '\x1B[37m',
|
|
|
|
'§8': '\x1B[90m', '§9': '\x1B[94m', '§a': '\x1B[92m', '§b': '\x1B[96m',
|
|
|
|
'§c': '\x1B[91m', '§d': '\x1B[95m', '§e': '\x1B[93m', '§f': '\x1B[97m',
|
|
|
|
'black': '\x1B[30m', 'dark_blue': '\x1B[34m', 'dark_green': '\x1B[32m',
|
|
|
|
'dark_aqua': '\x1B[36m', 'dark_red': '\x1B[31m', 'dark_purple': '\x1B[35m',
|
|
|
|
'gold': '\x1B[33m', 'gray': '\x1B[37m', 'dark_gray': '\x1B[90m', 'blue': '\x1B[94m',
|
|
|
|
'green': '\x1B[92m', 'aqua': '\x1B[96m', 'red': '\x1B[91m', 'light_purple': '\x1B[95m',
|
|
|
|
'yellow': '\x1B[93m', 'white': '\x1B[97m'
|
|
|
|
};
|
|
|
|
|
|
|
|
const ansiFormatCodes = {
|
|
|
|
'§l': '\x1B[1m', '§o': '\x1B[3m', '§n': '\x1B[4m', '§m': '\x1B[9m', '§k': '\x1B[5m', '§r': '\x1B[0m',
|
|
|
|
'bold': '\x1B[1m', 'italic': '\x1B[3m', 'underlined': '\x1B[4m', 'strikethrough': '\x1B[9m', 'obfuscated': '\x1B[5m', 'reset': '\x1B[0m',
|
|
|
|
};
|
|
|
|
|
|
|
|
function parseMinecraftMessage(component) {
|
2024-10-12 08:11:35 -04:00
|
|
|
if (component === undefined) return;
|
|
|
|
|
2024-09-20 12:01:17 -04:00
|
|
|
let jsonComponent;
|
|
|
|
try {
|
|
|
|
jsonComponent = JSON.parse(component);
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Invalid JSON format:", component);
|
|
|
|
return '';
|
|
|
|
}
|
2024-10-12 08:11:35 -04:00
|
|
|
|
2024-09-20 12:01:17 -04:00
|
|
|
function extractText(comp) {
|
|
|
|
let text = '';
|
|
|
|
if (comp.text && typeof comp.text === 'string' || typeof comp.text === 'number' && comp.text !== undefined && comp.text !== null) {
|
|
|
|
text += comp.text;
|
|
|
|
}
|
|
|
|
if (comp[""] && typeof comp[""] === 'string' || typeof comp[""] === 'number' && comp[""] !== undefined && comp[""] !== null) {
|
|
|
|
text += comp[""];
|
|
|
|
}
|
|
|
|
if (comp && typeof comp === 'string' || typeof comp === 'number' && comp !== undefined && comp !== null) {
|
|
|
|
return comp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (comp.extra) {
|
2024-10-12 08:11:35 -04:00
|
|
|
if (!Array.isArray(comp.extra)) comp.extra = [comp.extra]
|
2024-09-20 12:01:17 -04:00
|
|
|
comp.extra.forEach(subComp => {
|
|
|
|
text += formatfunction(comp, extractText(subComp));
|
|
|
|
});
|
2024-10-12 08:11:35 -04:00
|
|
|
}
|
2024-09-20 12:01:17 -04:00
|
|
|
|
|
|
|
if (comp.translate) {
|
2024-10-12 08:11:35 -04:00
|
|
|
let translateString = lang[comp.translate] || comp.translate;
|
2024-09-20 12:01:17 -04:00
|
|
|
if (comp.with) {
|
2024-10-12 08:11:35 -04:00
|
|
|
const withArgs = comp.with.map(arg => { return parseMinecraftColor(comp.color) + parseMinecraftFormat(comp) + extractText(arg) });
|
|
|
|
|
|
|
|
let usedReplacements = 0;
|
|
|
|
translateString = translateString.replace(/%s/g, () => {
|
|
|
|
if (usedReplacements < withArgs.length) {
|
|
|
|
return formatfunction(comp, withArgs[usedReplacements++]);
|
|
|
|
}
|
|
|
|
return "%s";
|
|
|
|
});
|
|
|
|
|
2024-09-20 12:01:17 -04:00
|
|
|
withArgs.forEach((arg, index) => {
|
2024-10-12 08:11:35 -04:00
|
|
|
const regex = new RegExp(`%${index + 1}\\$s`, 'g');
|
|
|
|
translateString = translateString.replace(regex, formatfunction(comp, arg));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
text += formatfunction(comp, translateString);
|
2024-09-20 12:01:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
text = parseMinecraftColor(comp.color) + parseMinecraftFormat(comp) + text + ansiFormatCodes['reset'];
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
return extractText(jsonComponent) + ansiFormatCodes['reset'];
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatfunction(comp, text) {
|
|
|
|
if (text === undefined) return '';
|
|
|
|
return text = parseMinecraftColor(comp.color) + parseMinecraftFormat(comp) + text + parseMinecraftColor(comp.color) + parseMinecraftFormat(comp);
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseMinecraftColor(color) {
|
|
|
|
if (color && ansiColorCodes[color] && !color.startsWith('#')) {
|
|
|
|
return ansiColorCodes[color];
|
|
|
|
} else if (color && color.startsWith('#')) {
|
|
|
|
const hexRegex = /#?([a-fA-F\d]{2})([a-fA-F\d]{2})([a-fA-F\d]{2})/;
|
|
|
|
const hexCodes = hexRegex.exec(color);
|
|
|
|
if (hexCodes) {
|
|
|
|
const red = parseInt(hexCodes[1], 16);
|
|
|
|
const green = parseInt(hexCodes[2], 16);
|
|
|
|
const blue = parseInt(hexCodes[3], 16);
|
|
|
|
const ansiColor = `\u001b[38;2;${red};${green};${blue}m`;
|
|
|
|
return ansiColor;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseMinecraftFormat(format) {
|
|
|
|
let result = '';
|
|
|
|
if (format.bold && format.bold === 1) result += ansiFormatCodes['bold'];
|
|
|
|
if (format.italic && format.italic === 1) result += ansiFormatCodes['italic'];
|
|
|
|
if (format.underlined && format.underlined === 1) result += ansiFormatCodes['underlined'];
|
|
|
|
if (format.strikethrough && format.strikethrough === 1) result += ansiFormatCodes['strikethrough'];
|
|
|
|
if (format.obfuscated && format.obfuscated === 1) result += ansiFormatCodes['obfuscated'];
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function parseMinecraftMessageNoColor(component) {
|
2024-10-12 08:11:35 -04:00
|
|
|
if (component === undefined) return;
|
2024-09-20 12:01:17 -04:00
|
|
|
let jsonComponent;
|
|
|
|
try {
|
|
|
|
jsonComponent = JSON.parse(component);
|
|
|
|
} catch (e) {
|
|
|
|
console.error("Invalid JSON format:", component);
|
|
|
|
}
|
|
|
|
|
|
|
|
function extractText(comp) {
|
|
|
|
let text = '';
|
|
|
|
|
|
|
|
if (comp.text && typeof comp.text === 'string' || typeof comp.text === 'number' && comp.text !== undefined && comp.text !== null) {
|
|
|
|
text += comp.text;
|
|
|
|
}
|
|
|
|
if (comp[""] && typeof comp[""] === 'string' || typeof comp[""] === 'number' && comp[""] !== undefined && comp[""] !== null) {
|
|
|
|
text += comp[""];
|
|
|
|
}
|
|
|
|
if (comp && typeof comp === 'string' || typeof comp === 'number' && comp !== undefined && comp !== null) {
|
|
|
|
return comp;
|
|
|
|
}
|
|
|
|
if (comp.extra) {
|
2024-10-12 08:11:35 -04:00
|
|
|
if (!Array.isArray(comp.extra)) comp.extra = [comp.extra]
|
2024-09-20 12:01:17 -04:00
|
|
|
comp.extra.forEach(subComp => {
|
|
|
|
text += extractText(subComp);
|
|
|
|
});
|
|
|
|
}
|
2024-10-12 08:11:35 -04:00
|
|
|
|
2024-09-20 12:01:17 -04:00
|
|
|
if (comp.translate) {
|
2024-10-12 08:11:35 -04:00
|
|
|
let translateString = lang[comp.translate] || comp.translate;
|
|
|
|
if (comp.with) {
|
|
|
|
const withArgs = comp.with.map(arg => extractText(arg));
|
|
|
|
|
|
|
|
let usedReplacements = 0;
|
|
|
|
translateString = translateString.replace(/%s/g, () => {
|
|
|
|
if (usedReplacements < withArgs.length) {
|
|
|
|
return formatfunction(comp, withArgs[usedReplacements++]);
|
|
|
|
}
|
|
|
|
return "%s";
|
|
|
|
});
|
2024-09-20 12:01:17 -04:00
|
|
|
|
2024-10-12 08:11:35 -04:00
|
|
|
withArgs.forEach((arg, index) => {
|
|
|
|
const regex = new RegExp(`%${index + 1}\\$s`, 'g');
|
|
|
|
translateString = translateString.replace(regex, arg);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
text += translateString;
|
2024-09-20 12:01:17 -04:00
|
|
|
}
|
|
|
|
|
2024-10-12 08:11:35 -04:00
|
|
|
|
2024-09-20 12:01:17 -04:00
|
|
|
return text;
|
|
|
|
}
|
|
|
|
msgText = extractText(jsonComponent)
|
|
|
|
// msgText = msgText.replace('§', '&')
|
|
|
|
return msgText;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-09-07 07:35:50 -04:00
|
|
|
module.exports = { inject };
|