diff --git a/main/chatparser.js b/main/chatparser.js index 6e9589c..1634e23 100644 --- a/main/chatparser.js +++ b/main/chatparser.js @@ -1,3 +1,6 @@ +// Don't change this or you gay. +// Version: 1.0 + const lang = require("./en_us.json"); // translate message function uuidFromIntArray (arr) { @@ -147,7 +150,7 @@ bot.on('profileless_chat', (packet) => { // kinda player_chat profilelesschat = {}; - profilelesschat.type = packet.type.registryIndex; + profilelesschat.type = packet.type; profilelesschat.formattedMessage = simplify(packet.message); profilelesschat.senderName = simplify(packet.name); @@ -197,7 +200,7 @@ bot.on('player_chat', (packet) => { // player playerchat = {}; playerchat.plainMessage = packet.plainMessage; - playerchat.type = packet.type.registryIndex; + playerchat.type = packet.type; playerchat.sender = packet.senderUuid; playerchat.unsignedContent = simplify(packet.unsignedChatContent); @@ -439,4 +442,124 @@ function parseMinecraftMessageNoColor(component) { return extractText(component); } -module.exports = { inject }; \ No newline at end of file +function kickparser(component) { + if (component === undefined) return; + if (typeof component === "string") return component; + + function kickparserText(comp) { + let text = ''; + + if (comp.text && typeof comp.text === 'string' || typeof comp.text === 'number') { + text += comp.text; + } + + if (comp[""] && typeof comp[""] === 'string' || typeof comp[""] === 'number') { + text += comp[""]; + } + + if (comp && typeof comp === 'string' || typeof comp === 'number') { + return comp; + } + + if (comp.translate) { + if (comp.fallback && !lang[comp.translate]) return text += comp.fallback; + let translateString = lang[comp.translate] || comp.translate; + let DefaultTranslateString = lang[comp.translate] || comp.translate; + let DefaultMsg = false; + + if (comp.with) { + const withArgs = comp.with.map(arg => kickparserText(arg)); + let usedReplacements = 0; + + translateString = translateString.replace(/thing__placeholder__/g, 'default_thing__placeholder__'); + translateString = translateString.replace(/%s/g, (match, offset, string) => { + if (offset > 0 && string[offset - 1] === '%') { + return 's'; + } + + if (usedReplacements < withArgs.length) { + if (translateString.length + withArgs[usedReplacements].length > 2048) return 'Translate Crash'; // Prevent translate crash + return `thing__placeholder__${usedReplacements++}`; + } + + DefaultMsg = true; + return "%s"; + }); + + translateString = translateString.replace(/%(-?\d+)\$s/g, (match, index, stringindex, string) => { + const argIndex = parseInt(index, 10) - 1; + + if (argIndex < 0 || argIndex >= withArgs.length) { + DefaultMsg = true; + return match; + } + + if (stringindex > 0 && string[stringindex - 1] === '%') { + return match; + } + + if (translateString.length + withArgs[argIndex].length > 2048) return 'Translate Crash'; // Prevent translate crash + return `thing__placeholder__${argIndex}`; + }); + + for (let i = 0; i < withArgs.length; i++) { + if (translateString.length + withArgs[i].length > 2048) return 'Translate Crash'; // Prevent translate crash + translateString = translateString.replace(new RegExp(`thing__placeholder__${i}`, 'g'), (match) => { + const formattedArg = withArgs[i]; + return formattedArg; + }); + } + translateString = translateString.replace(/default_thing__placeholder__/g, 'thing__placeholder__'); + } + + if (DefaultMsg) { + text += DefaultTranslateString; + } else { + text += translateString; + } + } + + if (comp.extra) { + if (!Array.isArray(comp.extra)) comp.extra = [comp.extra] + comp.extra.forEach(subComp => { + text += kickparserText(subComp); + }); + } + + return text; + } + return kickparserText(simplify(component)); +} + +function cboutput(component) { + if (component === undefined) return; + + function extractText(comp) { + + let text = ''; + if (comp.text && typeof comp.text === 'string' || typeof comp.text === 'number') { + text += comp.text; + } + if (comp && typeof comp === 'string' || typeof comp === 'number') { + return comp; + } + + if (comp.extra) { + if (!Array.isArray(comp.extra)) comp.extra = [comp.extra] + comp.extra.forEach(subComp => { + text += formatfunction(comp, extractText(subComp)); + }); + } + + text = parseMinecraftColor(comp.color) + parseMinecraftFormat(comp) + text + ansiFormatCodes['reset']; + return text; + + } + + return extractText(component) + ansiFormatCodes['reset']; +} + + + + +module.exports = { inject, kickparser, cboutput }; \ No newline at end of file diff --git a/main/main.js b/main/main.js index 60a1fe6..55108ef 100644 --- a/main/main.js +++ b/main/main.js @@ -1,3 +1,5 @@ +// for version 1.0 chatparser + const mc = require('minecraft-protocol'); const ChatParse = require("./chatparser.js"); const bot = mc.createClient({ @@ -5,33 +7,46 @@ const bot = mc.createClient({ // host: 'chipmunk.land', // host: '168.100.225.224', // Neko port: 25565, - username: 'susparser', // cat + username: 'catparser', // cat version: "1.20.4", }); ChatParse.inject(bot); // load chatparser function - bot.on('custom_playerchat', (message, playerchat, packet) => { - console.log(`PlayerChat: ${message}`); -}); - -bot.on('custom_systemchat', (message, systemchat, packet) => { - console.log(`SystemChat: ${message}`); + console.log(`[PlayerChat] ${message}`); }); bot.on('custom_profilelesschat', (message, profilelesschat, packet) => { - console.log(`ProfilelessChat: ${message}`) + console.log(`[ProfilelessChat] ${message}`); +}); +bot.on('custom_systemchat', (message, systemchat, packet) => { + if (systemchat?.jsonMsg?.translate === "advMode.setCommand.success") return; // if you have core and dont want see "Command set: %s" + console.log(`[SystemChat] ${message}`); +}); + +bot.on('custom_actionbar', (message, actionbar, packet) => { + console.log(`[ActionBar] ${message}`); +}); + +bot.on('custom_bossbar', (title, bossbar, packet) => { + console.log(`[BossBar | ${bossbar.action}] ${title}`); }); /* -bot.on('custom_allchat', (message, chat, packet) => { - console.log(`AllChat: ${message}`) +bot.on('custom_allchat', (chatType, message, messagePacket, packet) => { + if (chatType === "actionbar" || chatType === "bossbar") return; + if (chatType === "systemchat" && messagePacket?.jsonMsg?.translate === "advMode.setCommand.success") return; // if you have core and dont want see "Command set: %s" + console.log(`${chatType}: ${message}`) }) */ -bot.on('error', (err) => { - console.error('Bot Error:', err); +bot.on('login', () => { + console.log(`Bot Joined!`); }); + +bot.on('error', (err) => { + console.error(err); +}); \ No newline at end of file