From 130f4f4e62758b6380625ca4085c8a5f631b9e9c Mon Sep 17 00:00:00 2001 From: Yaode_owo Date: Sat, 12 Oct 2024 08:11:35 -0400 Subject: [PATCH] change lots remove commandspy fix give command block rewrite translate code use processNbtMessage(), not that already have profileless_chat All_Chat --- main/chatparser.js | 292 +++++++++++++++++++-------------------------- main/main.js | 28 +++-- 2 files changed, 139 insertions(+), 181 deletions(-) diff --git a/main/chatparser.js b/main/chatparser.js index a041d93..7f73e09 100644 --- a/main/chatparser.js +++ b/main/chatparser.js @@ -1,84 +1,105 @@ const lang = require("./en_us.json"); // translate message -const regex = /^(.+):\s*\/(.+)$/; +const nbt = require('prismarine-nbt'); -function parseCommand(message) { - const match = message.match(regex); - if (match) { - const username = match[1]; - const command = match[2]; - return { username, command }; - } else { - return undefined; - } +function uuidFromIntArray (arr) { + const buf = Buffer.alloc(16) + arr.forEach((num, index) => { buf.writeInt32BE(num, index * 4) }) + return buf.toString('hex') +} + +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":""}'; + } } function inject(bot) { -bot.on('systemChat', (packet) => { - // console.log(packet) - const jsonmsg = JSON.parse(packet.formattedMessage); - const msg = parseMinecraftMessage(packet.formattedMessage); - const nocolormsg = parseMinecraftMessageNoColor(packet.formattedMessage); - const cspy = parseCommand(nocolormsg) - if (cspy) bot.emit('custom_Commandspy', msg); - if (jsonmsg === undefined) { // idk, but work - if (msg !== undefined) bot.emit('custom_systemChat', msg, nocolormsg, undefined); - } else { - if (msg !== undefined) bot.emit('custom_systemChat', msg, nocolormsg, jsonmsg); - } + +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); }); -bot.on('playerChat', (packet) => { - let msg, vmsg; - // console.log(packet) - // other - const uuid = packet.sender ? packet.sender : undefined; - const plainMessage = packet.plainMessage ? packet.plainMessage : undefined; - // Color - const SenderName = packet.senderName ? parseMinecraftMessage(packet.senderName) : undefined; - const TargetName = packet.targetName ? parseMinecraftMessage(packet.targetName) : undefined; - const formattedMessage = packet.formattedMessage ? parseMinecraftMessage(packet.formattedMessage) : undefined; - const unsignedContent = packet.unsignedContent ? parseMinecraftMessage(packet.unsignedContent) : undefined; - // No Color - const NoColorSenderName = packet.senderName ? parseMinecraftMessageNoColor(packet.senderName) : undefined; - const NoColorTargetName = packet.targetName ? parseMinecraftMessageNoColor(packet.targetName) : undefined; - const NoColorformattedMessage = packet.formattedMessage ? parseMinecraftMessageNoColor(packet.formattedMessage) : undefined; - const NoColorunsignedContent = packet.unsignedContent ? parseMinecraftMessageNoColor(packet.unsignedContent) : undefined; - switch (packet.type) { + +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) { case 1: // /me text - msg = `* ${SenderName} ${formattedMessage}`; - nocolorplayermsg = `* ${NoColorSenderName} ${NoColorformattedMessage}`; + msg = `* ${playerchat.senderName} ${playerchat.formattedMessage}`; + playerchat.nocolor_msg = `* ${playerchat.nocolor_senderName} ${playerchat.nocolor_formattedMessage}`; break; case 2: // someone /tell you text - msg = `${SenderName} whispers to you: ${plainMessage ? plainMessage : formattedMessage}`; - nocolorplayermsg = `${NoColorSenderName} whispers to you: ${plainMessage ? plainMessage : NoColorformattedMessage}`; + msg = `${playerchat.senderName} whispers to you: ${playerchat.plainMessage || playerchat.formattedMessage}`; + playerchat.nocolor_msg = `${playerchat.nocolor_senderName} whispers to you: ${playerchat.plainMessage || playerchat.nocolor_formattedMessage}`; break; case 3: // you /tell someone text - msg = `You whisper to ${TargetName}: ${plainMessage ? plainMessage : formattedMessage}`; - nocolorplayermsg = `You whisper to ${NoColorTargetName}: ${plainMessage ? plainMessage : NoColorformattedMessage}`; + msg = `You whisper to ${playerchat.targetName}: ${playerchat.plainMessage || playerchat.formattedMessage}`; + playerchat.nocolor_msg = `You whisper to ${NoColorTargetName}: ${playerchat.plainMessage || playerchat.nocolor_formattedMessage}`; break; case 4: // player chat - // /sudo and vanish send formattedMessage message - // normal unsignedContent - vmsg = formattedMessage; - nocolormsg = NoColorformattedMessage - msg = unsignedContent; - nocolorplayermsg = NoColorunsignedContent + msg = playerchat.unsignedContent; + playerchat.nocolor_msg = playerchat.nocolor_unsignedContent; break; case 5: // /say text - msg = `[${SenderName}] ${plainMessage ? plainMessage : formattedMessage}`; - nocolorplayermsg = `[${NoColorSenderName}] ${plainMessage ? plainMessage : NoColorformattedMessage}`; + msg = `[${playerchat.senderName}] ${playerchat.plainMessage || playerchat.formattedMessage}`; + playerchat.nocolor_msg = `[${playerchat.nocolor_senderName}] ${playerchat.plainMessage || playerchat.nocolor_formattedMessage}`; break; case 6: // /minecraft:teammsg text - msg = `${TargetName} <${SenderName}> ${plainMessage}`; - nocolorplayermsg = `${NoColorTargetName} <${NoColorSenderName}> ${plainMessage}`; + msg = `${playerchat.targetName} <${playerchat.senderName}> ${playerchat.plainMessage}`; + playerchat.nocolor_msg = `${playerchat.nocolor_targetName} <${playerchat.nocolor_senderName}> ${playerchat.plainMessage}`; break; default: - console.log(`Unknown player_chat packet. Type: ${packet.type}`); - console.log(packet) + console.log(`Unknown player_chat packet. Type: ${playerchat.type}`); + console.log(packet); break; } - 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, nocolormsg, ""); + bot.emit('custom_PlayerChat', msg, playerchat); + bot.emit('Custom_AllChat', msg, playerchat) }); const ansiColorCodes = { @@ -99,6 +120,8 @@ const ansiFormatCodes = { }; function parseMinecraftMessage(component) { + if (component === undefined) return; + let jsonComponent; try { jsonComponent = JSON.parse(component); @@ -106,7 +129,7 @@ function parseMinecraftMessage(component) { console.error("Invalid JSON format:", component); return ''; } - + function extractText(comp) { let text = ''; if (comp.text && typeof comp.text === 'string' || typeof comp.text === 'number' && comp.text !== undefined && comp.text !== null) { @@ -120,34 +143,39 @@ function parseMinecraftMessage(component) { } if (comp.extra) { + if (!Array.isArray(comp.extra)) comp.extra = [comp.extra] comp.extra.forEach(subComp => { text += formatfunction(comp, extractText(subComp)); }); - } + } if (comp.translate) { - let translateString = lang[comp.translate] || comp.translate; + let translateString = lang[comp.translate] || comp.translate; if (comp.with) { - const withArgs = comp.with.map(arg => extractText(arg)); + 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"; + }); + withArgs.forEach((arg, index) => { - if (arg.length > 2048) return translateString = ''; // anti tellraw translate crash - translateString = translateString.replace('%s', formatfunction(comp, arg)); - const placeholder = new RegExp(`%${index + 1}\\$s`, 'g'); // create tellraw translate crash - translateString = translateString.replace(placeholder, formatfunction(comp, arg)); - }); - } - text += formatfunction(comp, translateString); + const regex = new RegExp(`%${index + 1}\\$s`, 'g'); + translateString = translateString.replace(regex, formatfunction(comp, arg)); + }); + } + text += formatfunction(comp, translateString); } 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); @@ -183,6 +211,7 @@ function parseMinecraftFormat(format) { function parseMinecraftMessageNoColor(component) { + if (component === undefined) return; let jsonComponent; try { jsonComponent = JSON.parse(component); @@ -203,25 +232,34 @@ function parseMinecraftMessageNoColor(component) { return comp; } if (comp.extra) { + if (!Array.isArray(comp.extra)) comp.extra = [comp.extra] comp.extra.forEach(subComp => { text += extractText(subComp); }); } + if (comp.translate) { - let translateString = lang[comp.translate] || comp.translate; - if (comp.with) { - const withArgs = comp.with.map(arg => extractText(arg)); - withArgs.forEach((arg, index) => { - if (arg.length > 10000) return translateString = ''; // anti tellraw translate crash - translateString = translateString.replace('%s', arg); - const placeholder = new RegExp(`%${index + 1}\\$s`, 'g'); // create tellraw translate crash - translateString = translateString.replace(placeholder, arg); + let translateString = lang[comp.translate] || comp.translate; + if (comp.with) { + const withArgs = comp.with.map(arg => extractText(arg)); - }); - } - text += translateString; + let usedReplacements = 0; + translateString = translateString.replace(/%s/g, () => { + if (usedReplacements < withArgs.length) { + return formatfunction(comp, withArgs[usedReplacements++]); + } + return "%s"; + }); + + withArgs.forEach((arg, index) => { + const regex = new RegExp(`%${index + 1}\\$s`, 'g'); + translateString = translateString.replace(regex, arg); + }); + } + text += translateString; } + return text; } msgText = extractText(jsonComponent) @@ -229,92 +267,6 @@ function parseMinecraftMessageNoColor(component) { return msgText; } -function kickparser(component) { - if (component === undefined) return component; - function kickparserText(comp) { - let text = ''; - - if (typeof comp === 'string') { - text += comp; - } - if (typeof comp.value === 'string') { - text += comp.value; - } - if (comp.with) { - text += kickparserText(comp.with); - } - - if (comp.value) { - [comp.value].forEach(subComp => { - if (typeof subComp === 'string') text += subComp; - text += kickparserText(subComp); - }); - } - - // how did i write this??? - if (comp.translate) { - let translateString = lang[comp.translate.value] || comp.translate.value; - - if (typeof translateString === 'string') { - if (comp.with) { - const withArray = Array.isArray(comp.with.value) ? comp.with.value : [comp.with.value]; - let formattedString = translateString; - const replacements = []; - - withArray.forEach((replacementText) => { - if (replacementText.value) { - const items = Array.isArray(replacementText.value) ? replacementText.value : [replacementText.value]; - - items.forEach((item) => { - const itemText = kickparserText(item); - if (itemText) replacements.push(itemText); - if (item.with && item.with.value) { - const nestedValues = Array.isArray(item.with.value) ? item.with.value : [item.with.value]; - nestedValues.forEach(nestedItem => { - const nestedText = kickparserText(nestedItem); - if (nestedText) replacements.push(nestedText); - }); - } - }); - } - }); - - let replacementIndex = 0; - formattedString = formattedString.replace(/%s/g, () => { - return replacementIndex < replacements.length ? replacements[replacementIndex++] : '%s'; - }); - text += formattedString; - } else { - text += kickparserText(translateString); - } - - } else { - throw new Error(`Translation not found for key: ${comp.translate.value}`); - } - } - - - - return text; - } - - let result = ''; // Initialize result to an empty string - - [component].forEach((comp) => { - if (comp.reason) { // Check if reason is defined - result += kickparserText(comp.reason); - } - }); - - return result -} - - -bot.parsekickmsg = (component) => { - return kickparser(component); -}; - - } diff --git a/main/main.js b/main/main.js index 038ba99..4e36139 100644 --- a/main/main.js +++ b/main/main.js @@ -1,28 +1,34 @@ const mc = require('minecraft-protocol'); -const inject = require("./chatparser.js"); - +const ChatParse = require("./chatparser.js"); const bot = mc.createClient({ - host: 'chipmunk.land', - //host: 'kaboom.pw', + // host: '95.216.192.50', // kaboom + // host: 'chipmunk.land', + // host: '168.100.225.224', // Neko port: 25565, username: 'catparser', // cat version: "1.20.4", }); -inject.inject(bot); // load +ChatParse.inject(bot); // load chatparser function -bot.on('custom_playerChat', (msg, uuid, plainMessage, SenderName, TargetName, formattedMessage, unsignedContent, NoColorSenderName, NoColorTargetName, NoColorformattedMessage, NoColorunsignedContent, nocolorplayermsg, type) => { - console.log(`PlayerChat: ${msg}`); + +bot.on('custom_PlayerChat', (message, playerchat) => { + console.log(`PlayerChat: ${message}`); }); -bot.on('custom_systemChat', (msg, nocolormsg, jsonmsg) => { // SystemChat maybe is PlayerChat use sudo or vanish - console.log(`SystemChat: ${msg}`); +bot.on('Custom_SystemChat', (message, systemchat) => { + console.log(`SystemChat: ${message}`); }); -bot.on('custom_Commandspy', (cspy) => { - console.log(`CSPY: ${cspy.username}: /${cspy.command}`); +bot.on('Custom_ProfilelessChat', (message, profilelesschat) => { + console.log(`ProfilelessChat: ${message}`) }) +/* +bot.on('Custom_AllChat', (message, chat) => { + console.log(`AllChat: ${message}`) +}) +*/ bot.on('error', (err) => { console.error('Bot Error:', err); });