From 9986dd31edf5450b55e1de809f4ed8dcfbc8f03d Mon Sep 17 00:00:00 2001 From: ChomeNS Date: Mon, 5 Dec 2022 14:55:54 +0700 Subject: [PATCH] add \n and >256 length support for chat --- config.js | 5 +++- plugins/chat.js | 39 ++++++++++++++++++++----------- plugins/core.js | 2 +- util/containsIllegalCharacters.js | 4 ++-- 4 files changed, 33 insertions(+), 17 deletions(-) diff --git a/config.js b/config.js index cb4e57c..25aaff3 100644 --- a/config.js +++ b/config.js @@ -16,7 +16,10 @@ module.exports = { version: '1.18.2' }, console: true, - useChat: false, + chat: { + useChat: false, + messageLength: 100 + }, core: { layers: 3, refillInterval: 1000 * 60, diff --git a/plugins/chat.js b/plugins/chat.js index dbc0ea1..18c4006 100644 --- a/plugins/chat.js +++ b/plugins/chat.js @@ -5,21 +5,33 @@ const { containsIllegalCharacters } = require('../util/containsIllegalCharacters') const { chatPacketListener, parsePlayerMessages } = require('../util/chat') -function inject (bot) { +function inject (bot, dcclient, config) { bot.chatQueue = [] + const _chatQueue = [] - const chatQueue = setInterval(function () { - if (bot.chatQueue[0] || bot.chatQueue[0] === '') { - try { - if (containsIllegalCharacters(bot.chatQueue[0])) { - bot.chatQueue.shift() - return - }; - bot.write('chat', { message: bot.chatQueue[0] }) + const _chatQueueInterval = setInterval(() => { + if (bot.chatQueue.length !== 0) { + if (containsIllegalCharacters(bot.chatQueue[0])) { bot.chatQueue.shift() - } catch (e) { - bot.console.error(e) - } + return + }; + // totallynotskidded™️ from mineflayer/lib/plugins/chat.js + bot.chatQueue[0].split('\n').forEach((subMessage) => { + if (!subMessage) return + let smallMsg + for (let i = 0; i < subMessage.length; i += config.chat.messageLength) { + smallMsg = subMessage.substring(i, i + config.chat.messageLength) + _chatQueue.push(smallMsg) + } + }) + bot.chatQueue.shift() + } + }, 0) + + const chatQueueInterval = setInterval(function () { + if (_chatQueue.length !== 0) { + bot.write('chat', { message: _chatQueue[0] }) + _chatQueue.shift() } }, 450) @@ -28,7 +40,8 @@ function inject (bot) { } bot.on('end', () => { - clearInterval(chatQueue) + clearInterval(chatQueueInterval) + clearInterval(_chatQueueInterval) }) const ChatMessage = require('prismarine-chat')(bot.version) diff --git a/plugins/core.js b/plugins/core.js index 1c3c011..c3d0d62 100644 --- a/plugins/core.js +++ b/plugins/core.js @@ -14,7 +14,7 @@ function inject (bot, dcclient, config) { }, run (command) { try { - if (config.useChat && + if (config.chat.useChat && command.startsWith('minecraft:tellraw @a ') && !command.includes('Console') && !command.includes('Discord') diff --git a/util/containsIllegalCharacters.js b/util/containsIllegalCharacters.js index 77c2ddc..75be149 100644 --- a/util/containsIllegalCharacters.js +++ b/util/containsIllegalCharacters.js @@ -5,7 +5,7 @@ * @return {boolean} allowed */ function isAllowedCharacter (character) { - return character !== '\xa7' && character >= ' ' && character !== '\x7f' + return character !== '\xa7' && character !== '\x7f' } /** * mc chat check if contains illegal chars. @@ -16,4 +16,4 @@ function containsIllegalCharacters (string) { for (let i = 0; i < string.length; i++) if (!isAllowedCharacter(string[i])) return true } -module.exports = { containsIllegalCharacters } +module.exports = { containsIllegalCharacters, isAllowedCharacter }