add smth and patch smth

This commit is contained in:
Yaode_owo 2024-09-07 07:35:50 -04:00
parent 2320953bfa
commit 93ea0c124d

View file

@ -14,7 +14,7 @@ function parseCommand(message) {
function inject(bot) { function inject(bot) {
bot.on('systemChat', (packet) => { bot.on('systemChat', (packet) => {
//console.log(packet) // console.log(packet)
const jsonmsg = JSON.parse(packet.formattedMessage); const jsonmsg = JSON.parse(packet.formattedMessage);
const msg = parseMinecraftMessage(packet.formattedMessage); const msg = parseMinecraftMessage(packet.formattedMessage);
const nocolormsg = parseMinecraftMessageNoColor(packet.formattedMessage); const nocolormsg = parseMinecraftMessageNoColor(packet.formattedMessage);
@ -24,7 +24,6 @@ bot.on('systemChat', (packet) => {
} else { } else {
if (msg !== undefined) bot.emit('custom_systemChat', msg, nocolormsg, jsonmsg); if (msg !== undefined) bot.emit('custom_systemChat', msg, nocolormsg, jsonmsg);
} }
if (cspy !== undefined) bot.emit('custom_commandspy', cspy);
}); });
bot.on('playerChat', (packet) => { bot.on('playerChat', (packet) => {
@ -46,32 +45,39 @@ bot.on('playerChat', (packet) => {
switch (packet.type) { switch (packet.type) {
case 1: // /me text case 1: // /me text
msg = `* ${SenderName} ${formattedMessage}`; msg = `* ${SenderName} ${formattedMessage}`;
nocolorplayermsg = `* ${NoColorSenderName} ${NoColorformattedMessage}`;
break; break;
case 2: // someone /tell you text case 2: // someone /tell you text
msg = `${SenderName} whispers to you: ${formattedMessage}`; msg = `${SenderName} whispers to you: ${formattedMessage}`;
nocolorplayermsg = `${NoColorSenderName} whispers to you: ${NoColorformattedMessage}`;
break; break;
case 3: // you /tell someone text case 3: // you /tell someone text
msg = `You whisper to ${TargetName}: ${formattedMessage}`; msg = `You whisper to ${TargetName}: ${formattedMessage}`;
nocolorplayermsg = `You whisper to ${NoColorTargetName}: ${NoColorformattedMessage}`;
break; break;
case 4: // player chat case 4: // player chat
// /sudo and vanish send formattedMessage message // /sudo and vanish send formattedMessage message
// normal unsignedContent // normal unsignedContent
vmsg = formattedMessage; vmsg = formattedMessage;
nocolormsg = NoColorformattedMessage
msg = unsignedContent; msg = unsignedContent;
nocolorplayermsg = NoColorunsignedContent
break; break;
case 5: // /say text case 5: // /say text
msg = `[${SenderName}] ${plainMessage ? plainMessage : formattedMessage}`; msg = `[${SenderName}] ${plainMessage ? plainMessage : formattedMessage}`;
nocolorplayermsg = `[${NoColorSenderName}] ${plainMessage ? plainMessage : NoColorformattedMessage}`;
break; break;
case 6: // /minecraft:teammsg text case 6: // /minecraft:teammsg text
msg = `${TargetName} <${SenderName}> ${plainMessage}`; msg = `${TargetName} <${SenderName}> ${plainMessage}`;
nocolorplayermsg = `${NoColorTargetName} <${NoColorSenderName}> ${plainMessage}`;
break; break;
default: default:
console.log(`Unknown player_chat packet. Type: ${packet.type}`); console.log(`Unknown player_chat packet. Type: ${packet.type}`);
console.log(packet) console.log(packet)
break; break;
} }
if (msg !== undefined) bot.emit('custom_playerChat', msg, uuid, plainMessage, SenderName, TargetName, formattedMessage, unsignedContent, NoColorSenderName, NoColorTargetName, NoColorformattedMessage, NoColorunsignedContent); if (msg !== undefined) bot.emit('custom_playerChat', msg, uuid, plainMessage, SenderName, TargetName, formattedMessage, unsignedContent, NoColorSenderName, NoColorTargetName, NoColorformattedMessage, NoColorunsignedContent, nocolorplayermsg, packet.type);
if (vmsg !== undefined) bot.emit('custom_systemChat', vmsg, "", ""); if (vmsg !== undefined) bot.emit('custom_systemChat', vmsg, nocolormsg, "");
}); });
const ansiColorCodes = { const ansiColorCodes = {
@ -102,20 +108,20 @@ function parseMinecraftMessage(component) {
function extractText(comp) { function extractText(comp) {
let text = ''; let text = '';
if (comp.text) { if (comp.text && typeof comp.text === 'string' || typeof comp.text === 'number' && comp.text !== undefined && comp.text !== null) {
text += comp.text; text += comp.text;
} }
if (typeof comp[""] === 'string' || typeof comp[""] === 'number' && comp[""] !== undefined && comp[""] !== null) { // fix if text is {"":0} show false if (comp[""] && typeof comp[""] === 'string' || typeof comp[""] === 'number' && comp[""] !== undefined && comp[""] !== null) {
text += comp[""]; // after 1337 years, i found issue at here text += comp[""];
} }
if (typeof comp === 'string' || typeof comp === 'number') { if (comp && typeof comp === 'string' || typeof comp === 'number' && comp !== undefined && comp !== null) {
return comp; return comp;
} }
if (comp.extra) { if (comp.extra) {
comp.extra.forEach(subComp => { comp.extra.forEach(subComp => {
text += ansiFormatCodes['reset'] + parseMinecraftColor(comp.color) + parseMinecraftFormat(comp) + extractText(subComp) + parseMinecraftColor(comp.color) + parseMinecraftFormat(comp); // it must have better way, but i lazy. text += formatfunction(comp, extractText(subComp));
}); });
} }
if (comp.translate) { if (comp.translate) {
@ -123,29 +129,27 @@ function parseMinecraftMessage(component) {
if (comp.with) { if (comp.with) {
const withArgs = comp.with.map(arg => extractText(arg)); const withArgs = comp.with.map(arg => extractText(arg));
withArgs.forEach((arg, index) => { withArgs.forEach((arg, index) => {
if (arg.length > 10000) return translateString = ''; // anti tellraw translate crash if (arg.length > 2048) return translateString = ''; // anti tellraw translate crash
translateString = translateString.replace('%s', arg + parseMinecraftColor(comp.color) + parseMinecraftFormat(comp)); // i need make formatfunction2(comp, text) ? translateString = translateString.replace('%s', formatfunction(comp, arg));
const placeholder = new RegExp(`%${index + 1}\\$s`, 'g'); // create tellraw translate crash const placeholder = new RegExp(`%${index + 1}\\$s`, 'g'); // create tellraw translate crash
translateString = translateString.replace(placeholder, arg + parseMinecraftColor(comp.color) + parseMinecraftFormat(comp)); translateString = translateString.replace(placeholder, formatfunction(comp, arg));
}); });
} }
text += translateString; text += formatfunction(comp, translateString);
} }
text = formatfunction(comp, text); text = parseMinecraftColor(comp.color) + parseMinecraftFormat(comp) + text + ansiFormatCodes['reset'];
return text; return text;
} }
jsonComponent = extractText(jsonComponent); return extractText(jsonComponent) + ansiFormatCodes['reset'];
return jsonComponent + ansiFormatCodes['reset'];
} }
function formatfunction(comp, text) { function formatfunction(comp, text) {
return text = parseMinecraftColor(comp.color) + parseMinecraftFormat(comp) + text + ansiFormatCodes['reset']; if (text === undefined) return '';
return text = parseMinecraftColor(comp.color) + parseMinecraftFormat(comp) + text + parseMinecraftColor(comp.color) + parseMinecraftFormat(comp);
} }
function parseMinecraftColor(color) { function parseMinecraftColor(color) {
@ -168,11 +172,11 @@ function parseMinecraftColor(color) {
function parseMinecraftFormat(format) { function parseMinecraftFormat(format) {
let result = ''; let result = '';
if (format.bold) result += ansiFormatCodes['bold']; if (format.bold && format.bold === 1) result += ansiFormatCodes['bold'];
if (format.italic) result += ansiFormatCodes['italic']; if (format.italic && format.italic === 1) result += ansiFormatCodes['italic'];
if (format.underlined) result += ansiFormatCodes['underlined']; if (format.underlined && format.underlined === 1) result += ansiFormatCodes['underlined'];
if (format.strikethrough) result += ansiFormatCodes['strikethrough']; if (format.strikethrough && format.strikethrough === 1) result += ansiFormatCodes['strikethrough'];
if (format.obfuscated) result += ansiFormatCodes['obfuscated']; if (format.obfuscated && format.obfuscated === 1) result += ansiFormatCodes['obfuscated'];
return result; return result;
} }
@ -188,15 +192,15 @@ function parseMinecraftMessageNoColor(component) {
function extractText(comp) { function extractText(comp) {
let text = ''; let text = '';
if (comp.text) { if (comp.text && typeof comp.text === 'string' || typeof comp.text === 'number' && comp.text !== undefined && comp.text !== null) {
text += comp.text; text += comp.text;
} }
if (comp[""]) { if (comp[""] && typeof comp[""] === 'string' || typeof comp[""] === 'number' && comp[""] !== undefined && comp[""] !== null) {
text += comp[""]; text += comp[""];
} }
if (typeof comp === 'string' || typeof comp === 'number') { if (comp && typeof comp === 'string' || typeof comp === 'number' && comp !== undefined && comp !== null) {
text += comp; return comp;
} }}
if (comp.extra) { if (comp.extra) {
comp.extra.forEach(subComp => { comp.extra.forEach(subComp => {
text += extractText(subComp); text += extractText(subComp);
@ -209,7 +213,7 @@ function parseMinecraftMessageNoColor(component) {
withArgs.forEach((arg, index) => { withArgs.forEach((arg, index) => {
if (arg.length > 10000) return translateString = ''; // anti tellraw translate crash if (arg.length > 10000) return translateString = ''; // anti tellraw translate crash
translateString = translateString.replace('%s', arg); translateString = translateString.replace('%s', arg);
const placeholder = new RegExp(`%${index + 1}\\$s`, 'g'); const placeholder = new RegExp(`%${index + 1}\\$s`, 'g'); // create tellraw translate crash
translateString = translateString.replace(placeholder, arg); translateString = translateString.replace(placeholder, arg);
}); });
@ -219,10 +223,11 @@ function parseMinecraftMessageNoColor(component) {
return text; return text;
} }
msgText = extractText(jsonComponent)
return extractText(jsonComponent); // msgText = msgText.replace('§', '&')
return msgText;
} }
} }
module.exports = inject; module.exports = { inject };