diff --git a/main/chatparser.js b/main/chatparser.js index a9cdd07..36c6c9a 100644 --- a/main/chatparser.js +++ b/main/chatparser.js @@ -1,4 +1,4 @@ -const lang = require("./en_us.json"); // translate message +const lang = require("../util/en_us.json"); // translate message function uuidFromIntArray (arr) { const buf = Buffer.alloc(16) @@ -51,12 +51,17 @@ let systemchat = {}; let profilelesschat = {}; let actionbar = {}; -bot.on('system_chat', (packet) => { // system - if (packet.isActionBar) { + +bot.on('action_bar', (packet) => { actionbar = {}; - console.log(`Actionbar: ${packet}`); - return; - }; + actionbar.message = parseMinecraftMessage(simplify(packet.text)); + actionbar.nocolor_message = parseMinecraftMessageNoColor(simplify(packet.text)) + bot.emit('custom_actionbar', actionbar.message, actionbar, packet); + bot.emit('custom_allchat', 'actionbar', actionbar.message, actionbar, packet); +}); + +bot.on('system_chat', (packet) => { // system + if (packet.isActionBar) return; systemchat = {}; systemchat.jsonMsg = simplify(packet.content); @@ -64,7 +69,7 @@ bot.on('system_chat', (packet) => { // system systemchat.nocolor_message = parseMinecraftMessageNoColor(simplify(packet.content)); bot.emit('custom_systemchat', systemchat.message, systemchat, packet); - bot.emit('custom_allchat', systemchat.message, systemchat, packet); + bot.emit('custom_allchat', 'system', systemchat.message, systemchat, packet); }); @@ -80,15 +85,15 @@ bot.on('profileless_chat', (packet) => { // kinda player_chat profilelesschat.targetName = simplify(packet.target); switch (profilelesschat.type) { - case 1: // /minecraft:me + case 1: // /me profilelesschat.message = parseMinecraftMessage({ "translate": "chat.type.emote", "with": [ profilelesschat.senderName, profilelesschat.formattedMessage ]}); profilelesschat.nocolor_msg = parseMinecraftMessageNoColor({ "translate": "chat.type.emote", "with": [ profilelesschat.senderName, profilelesschat.formattedMessage ]}); break; - case 2: // player /minecraft:tell + case 2: // player /tell profilelesschat.message = parseMinecraftMessage({ translate: "commands.message.display.incoming", with: [ profilelesschat.senderName, profilelesschat.formattedMessage ], color: "gray", italic: true }); profilelesschat.nocolor_msg = parseMinecraftMessageNoColor({ translate: "commands.message.display.incoming", with: [ profilelesschat.senderName, profilelesschat.formattedMessage ], color: "gray", italic: true }); break; - case 3: // you /minecraft:tell + case 3: // you /tell profilelesschat.message = parseMinecraftMessage({ translate: "commands.message.display.outgoing", with: [ profilelesschat.targetName, profilelesschat.formattedMessage ], color: "gray", italic: true }); profilelesschat.nocolor_msg = parseMinecraftMessageNoColor({ translate: "commands.message.display.outgoing", with: [ profilelesschat.targetName, profilelesschat.formattedMessage ], color: "gray", italic: true }); break; @@ -100,11 +105,11 @@ bot.on('profileless_chat', (packet) => { // kinda player_chat profilelesschat.message = parseMinecraftMessage({ translate: 'chat.type.announcement', with: [ profilelesschat.senderName, profilelesschat.formattedMessage ]}); profilelesschat.nocolor_msg = parseMinecraftMessageNoColor({ translate: 'chat.type.announcement', with: [ profilelesschat.senderName, profilelesschat.formattedMessage ]}); break; - case 6: // player /minecraft:teammsg || /teammsg + case 6: // player /teammsg profilelesschat.message = parseMinecraftMessage({ translate: 'chat.type.team.text', with: [ profilelesschat.targetName, profilelesschat.senderName, profilelesschat.formattedMessage ]}); profilelesschat.nocolor_msg = parseMinecraftMessageNoColor({ translate: 'chat.type.team.text', with: [ profilelesschat.targetName, profilelesschat.senderName, profilelesschat.formattedMessage ]}); break; - case 7: // you /minecraft:teammsg || /teammsg + case 7: // you /teammsg profilelesschat.message = parseMinecraftMessage({ translate: 'chat.type.team.sent', with: [ profilelesschat.targetName, profilelesschat.senderName, profilelesschat.formattedMessage ]}); profilelesschat.nocolor_msg = parseMinecraftMessageNoColor({ translate: 'chat.type.team.sent', with: [ profilelesschat.targetName, profilelesschat.senderName, profilelesschat.formattedMessage ]}); break; @@ -115,7 +120,7 @@ bot.on('profileless_chat', (packet) => { // kinda player_chat } bot.emit('custom_profilelesschat', profilelesschat.message, profilelesschat, packet) - bot.emit('custom_allchat', profilelesschat.message, profilelesschat, packet) + bot.emit('custom_allchat', 'profileless', profilelesschat.message, profilelesschat, packet) }) @@ -130,6 +135,9 @@ bot.on('player_chat', (packet) => { // player playerchat.senderName = simplify(packet.networkName); playerchat.targetName = simplify(packet.networkTargetName); + playerchat.nocolor_senderName = parseMinecraftMessageNoColor(simplify(packet.networkName)); + playerchat.nocolor_targetName = parseMinecraftMessageNoColor(simplify(packet.networkTargetName)); + switch (playerchat.type) { // vanish off case 1: // /minecraft:me playerchat.message = parseMinecraftMessage({ "translate": "chat.type.emote", "with": [ playerchat.senderName, playerchat.plainMessage ]}); @@ -165,7 +173,7 @@ bot.on('player_chat', (packet) => { // player break; } bot.emit('custom_playerchat', playerchat.message, playerchat, packet); - bot.emit('custom_allchat', playerchat.message, playerchat, packet) + bot.emit('custom_allchat', 'player', playerchat.message, playerchat, packet) }); } @@ -209,34 +217,31 @@ function parseMinecraftMessage(component) { }); } - if (comp.translate) { // im cook. + if (comp.translate) { 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 => extractText(arg)); - let usedReplacements = 0; + translateString = translateString.replace(/thing__placeholder__/g, 'thing__placeholderdefault__'); translateString = translateString.replace(/%s/g, (match, offset, string) => { if (offset > 0 && string[offset - 1] === '%') { return 's'; } - + if (usedReplacements < withArgs.length) { - const formattedArg = formatfunction(comp, withArgs[usedReplacements]); - if (translateString.length + formattedArg.length > 2048) return 'Translate Crash'; - usedReplacements++; - return formattedArg; + return `thing__placeholder__${usedReplacements++}`; } - + DefaultMsg = true; return "%s"; }); translateString = translateString.replace(/%(-?\d+)\$s/g, (match, index, stringindex, string) => { - const argIndex = index - 1; + const argIndex = parseInt(index, 10) - 1; if (argIndex < 0 || argIndex >= withArgs.length) { DefaultMsg = true; @@ -247,20 +252,24 @@ function parseMinecraftMessage(component) { return match; } - const formattedArg = formatfunction(comp, withArgs[argIndex]); - if (translateString.length + formattedArg.length > 2048) return 'Translate Crash'; - return formattedArg; + return `thing__placeholder__${argIndex}`; }); - } - if (DefaultMsg) { + for (let i = 0; i < withArgs.length; i++) { + translateString = translateString.replace(new RegExp(`thing__placeholder__${i}`, 'g'), (match) => { + const formattedArg = formatfunction(comp, withArgs[i]); + return formattedArg; + }); + } + translateString = translateString.replace(/thing__placeholderdefault__/g, 'thing__placeholder__'); + } + + if (DefaultMsg) { text += formatfunction(comp, DefaultTranslateString); } else { text += formatfunction(comp, translateString); } } - - text = parseMinecraftColor(comp.color) + parseMinecraftFormat(comp) + text + ansiFormatCodes['reset']; return text; @@ -300,28 +309,25 @@ function parseMinecraftMessageNoColor(component) { if (comp.with) { const withArgs = comp.with.map(arg => extractText(arg)); - let usedReplacements = 0; + translateString = translateString.replace(/thing__placeholder__/g, 'thing__placeholderdefault__'); translateString = translateString.replace(/%s/g, (match, offset, string) => { if (offset > 0 && string[offset - 1] === '%') { return 's'; } - + if (usedReplacements < withArgs.length) { - const formattedArg = withArgs[usedReplacements]; - if (translateString.length + formattedArg.length > 2048) return 'Translate Crash'; - usedReplacements++; - return formattedArg; + return `thing__placeholder__${usedReplacements++}`; } - + DefaultMsg = true; return "%s"; }); - translateString = translateString.replace(/%(\d+)\$s/g, (match, index, stringindex, string) => { - const argIndex = index - 1; - + 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; @@ -331,20 +337,25 @@ function parseMinecraftMessageNoColor(component) { return match; } - const formattedArg = withArgs[argIndex]; - if (translateString.length + formattedArg.length > 2048) return 'Translate Crash'; - return formattedArg; + return `thing__placeholder__${argIndex}`; }); - } - if (DefaultMsg) { + for (let i = 0; i < withArgs.length; i++) { + translateString = translateString.replace(new RegExp(`thing__placeholder__${i}`, 'g'), (match) => { + const formattedArg = withArgs[i]; + return formattedArg; + }); + } + translateString = translateString.replace(/thing__placeholderdefault__/g, 'thing__placeholder__'); + } + + if (DefaultMsg) { text += DefaultTranslateString; } else { text += translateString; } } - return text; }