From e1a1d92723fe8d3ec4925c1bc0b2146c74050970 Mon Sep 17 00:00:00 2001 From: ChomeNS Date: Sat, 10 Dec 2022 13:31:01 +0700 Subject: [PATCH] 1.19 support for both bot and proxy this somehow took 1 hour(?) --- config.js | 4 +-- plugins/chat.js | 35 ++++++++++++++++++++++--- plugins/proxy.js | 44 +++++++++++++++++++------------- plugins/proxy/chat.js | 11 ++++---- plugins/proxy/custom_chat.js | 28 +++++++++++++++----- util/chat.js | 14 ++++++---- util/loadPlugins.js | 5 ++-- util/minecraftVersionToNumber.js | 1 + 8 files changed, 100 insertions(+), 42 deletions(-) diff --git a/config.js b/config.js index d85423a..5d268c8 100644 --- a/config.js +++ b/config.js @@ -1,5 +1,5 @@ module.exports = { - version: '1.18.2', + version: '1.19', prefixes: [ '*', 'cbot ', @@ -12,7 +12,7 @@ module.exports = { }, proxy: { enabled: true, - version: '1.18.2' + version: '1.19' }, console: true, chat: { diff --git a/plugins/chat.js b/plugins/chat.js index 2956a6d..1c6477d 100644 --- a/plugins/chat.js +++ b/plugins/chat.js @@ -3,6 +3,7 @@ // const parse = require('../util/text_parser'); const { containsIllegalCharacters } = require('../util/containsIllegalCharacters') const { chatPacketListener, parsePlayerMessages } = require('../util/chat') +const minecraftVersionToNumber = require('../util/minecraftVersionToNumber') function inject (bot, dcclient, config) { bot.chatQueue = [] @@ -29,7 +30,27 @@ function inject (bot, dcclient, config) { const chatQueueInterval = setInterval(function () { if (bot._chatQueue.length !== 0) { - bot.write('chat', { message: bot._chatQueue[0] }) + if (minecraftVersionToNumber(bot.version) >= 1.19) { + // totallynotskidded™️ from mineflayer/lib/plugins/chat.js + if (bot._chatQueue[0].startsWith('/')) { + bot.write('chat_command', { + command: bot._chatQueue[0].substring(1), // removes / from the command + timestamp: BigInt(Date.now()), + salt: 0n, + argumentSignatures: [], + signedPreview: false + }) + } else { + bot.write('chat_message', { + message: bot._chatQueue[0], + timestamp: BigInt(Date.now()), + salt: 0, + signature: Buffer.alloc(0) // the bot will never go online mode i guess so i will just use this + }) + } + } else { + bot.write('chat', { message: bot._chatQueue[0] }) + } bot._chatQueue.shift() } }, 450) @@ -43,9 +64,15 @@ function inject (bot, dcclient, config) { clearInterval(_chatQueueInterval) }) - const ChatMessage = require('prismarine-chat')(bot.version) - - bot._client.on('chat', (packet) => chatPacketListener(packet, ChatMessage, bot)) + function listener (packet) { + chatPacketListener( + packet, + bot, + minecraftVersionToNumber(bot.version) >= 1.19 + ) + } + bot._client.on('system_chat', listener) + bot._client.on('chat', listener) bot.on('parsed_chat', (message, packet) => parsePlayerMessages(message, packet, bot)) } diff --git a/plugins/proxy.js b/plugins/proxy.js index 30d6de0..d9a5905 100644 --- a/plugins/proxy.js +++ b/plugins/proxy.js @@ -2,6 +2,7 @@ const util = require('util') const mc = require('minecraft-protocol') const { loadPlugins } = require('../util/loadPlugins') +const minecraftVersionToNumber = require('../util/minecraftVersionToNumber') function inject (bot, dcclient, config) { if (!config.proxy.enabled) return @@ -31,16 +32,36 @@ function inject (bot, dcclient, config) { version }) + const clientPacketBlacklist = [] const targetPacketBlacklist = [] target.chat = function (message) { - target.write('chat', { message }) + if (minecraftVersionToNumber(target.version) >= 1.19) { + if (message.startsWith('/')) { + target.write('chat_command', { + command: message.substring(1), // removes / from the command + timestamp: BigInt(Date.now()), + salt: 0n, + argumentSignatures: [], + signedPreview: false + }) + } else { + target.write('chat_message', { + message, + timestamp: BigInt(Date.now()), + salt: 0, + signature: Buffer.alloc(0) // the bot will never go online mode i guess so i will just use this + }) + } + } else { + target.write('chat', { message }) + } } target.on('login', (packet) => { bot.console.info(`[Proxy] ${client.username} target logged in`) target.entityId = packet.entityId - loadPlugins(bot, null, config, null, target, client, true, targetPacketBlacklist) + loadPlugins(bot, null, config, null, target, client, true, clientPacketBlacklist, targetPacketBlacklist) }) target.on('packet', (data, meta) => { @@ -53,8 +74,9 @@ function inject (bot, dcclient, config) { target.on('error', () => {}) - target.on('end', () => { + target.on('end', (reason) => { target.end() + client.end(`Target disconnected with reason: ${util.inspect(reason)}`) targetEnded = true }) @@ -74,21 +96,7 @@ function inject (bot, dcclient, config) { }) client.on('packet', (data, meta) => { - if (meta.name === 'chat' && !data.message?.startsWith('/')) { - if (data.message.startsWith('.')) { - return bot.command_handler.run( - client.username, - client.username, - '*' + data.message.substring(1), - client.uuid, - null, - 'h', - 'o', - client.username - ) - } - return - } + if (clientPacketBlacklist.includes(meta.name)) return target.write(meta.name, data) }) diff --git a/plugins/proxy/chat.js b/plugins/proxy/chat.js index 1fc4a5a..6e25139 100644 --- a/plugins/proxy/chat.js +++ b/plugins/proxy/chat.js @@ -1,11 +1,12 @@ const { chatPacketListener, parsePlayerMessages } = require('../../util/chat') +const minecraftVersionToNumber = require('../../util/minecraftVersionToNumber') function inject (bot, client, target) { - const ChatMessage = require('prismarine-chat')(bot.version) - - target.on('chat', (packet) => { - chatPacketListener(packet, ChatMessage, target) - }) + function listener (packet) { + chatPacketListener(packet, target, minecraftVersionToNumber(target.version) >= 1.19) + } + target.on('chat', listener) + target.on('system_chat', listener) target.on('parsed_chat', (message, packet) => { parsePlayerMessages(message, packet, target) diff --git a/plugins/proxy/custom_chat.js b/plugins/proxy/custom_chat.js index 65146d3..3279fb7 100644 --- a/plugins/proxy/custom_chat.js +++ b/plugins/proxy/custom_chat.js @@ -1,11 +1,25 @@ +const minecraftVersionToNumber = require('../../util/minecraftVersionToNumber') -function inject (bot, client, target) { +function inject (bot, client, target, config, clientPacketBlacklist) { const { MessageBuilder } = require('prismarine-chat')(bot.version) - client.on('packet', (data, meta) => { - if (meta.name === 'chat' && - !data.message?.startsWith('/') && - !data.message?.startsWith('.') - ) { + clientPacketBlacklist.push('chat') + clientPacketBlacklist.push('chat_message') + client.on(minecraftVersionToNumber(target.version) >= 1.19 ? 'chat_message' : 'chat', (data) => { + // not the best place to put command handler thing here but ok + if (data.message?.startsWith('.')) { + return bot.command_handler.run( + client.username, + client.username, + '*' + data.message.substring(1), + client.uuid, + null, + 'h', + 'o', + client.username + ) + } + + if (!data.message?.startsWith('/')) { const codeParsedMessage = data.message.replace(/%[^%]+%/g, (code) => { try { // eslint-disable-next-line no-eval @@ -33,6 +47,8 @@ function inject (bot, client, target) { MessageBuilder.fromString('&7' + codeParsedMessage) ] }) + } else { + target.chat(data) } }) }; diff --git a/util/chat.js b/util/chat.js index c451439..87a782d 100644 --- a/util/chat.js +++ b/util/chat.js @@ -1,13 +1,15 @@ /** * for the chat packet listener (in util cuz proxy + bot) * @param {object} packet chat packet - * @param {object} ChatMessage basically just require prismarine-chat * @param {object} bot bot + * @param {boolean} mc119 minecraft 1.19 or newer */ -function chatPacketListener (packet, ChatMessage, bot) { +function chatPacketListener (packet, bot, mc119) { // try catch prevents json parse error (which prob never happens but still..) try { - const parsedMessage = JSON.parse(packet.message) + const ChatMessage = require('prismarine-chat')(bot.version) + + const parsedMessage = JSON.parse(mc119 ? packet.content : packet.message) // down here it prevents command set message // for ayunboom cuz its 1.17.1 @@ -19,10 +21,12 @@ function chatPacketListener (packet, ChatMessage, bot) { // VVVVVVVVVVVVVVVVVVVVV if (parsedMessage.translate === 'advMode.setCommand.success') return - const message = ChatMessage.fromNotch(packet.message) + const message = ChatMessage.fromNotch(mc119 ? packet.content : packet.message) bot.emit('parsed_chat', message, packet) - } catch (e) {} + } catch (e) { + bot.console.error(e) + } }; /** diff --git a/util/loadPlugins.js b/util/loadPlugins.js index ccb0f91..71896f2 100644 --- a/util/loadPlugins.js +++ b/util/loadPlugins.js @@ -11,9 +11,10 @@ const path = require('path') * @param {object} target proxy target * @param {object} client proxy client * @param {boolean} proxy is proxy + * @param {array} clientPacketBlacklist the client packet blacklist * @param {array} targetPacketBlacklist target packet blacklist */ -async function loadPlugins (bot, dcclient, config, rl, target, client, proxy, targetPacketBlacklist) { +async function loadPlugins (bot, dcclient, config, rl, target, client, proxy, clientPacketBlacklist, targetPacketBlacklist) { const dir = path.join(__dirname, '..', 'plugins', proxy ? 'proxy' : '') const plugins = await fs.readdir(dir) plugins.forEach((plugin) => { @@ -21,7 +22,7 @@ async function loadPlugins (bot, dcclient, config, rl, target, client, proxy, ta try { const plug = require(path.join(dir, plugin)) if (!proxy) plug.inject(bot, dcclient, config, rl) - else plug.inject(bot, client, target, config, targetPacketBlacklist) + else plug.inject(bot, client, target, config, clientPacketBlacklist, targetPacketBlacklist) } catch (e) { console.log(`Plugin ${plugin} is having exception loading the plugin:`) console.log(util.inspect(e)) diff --git a/util/minecraftVersionToNumber.js b/util/minecraftVersionToNumber.js index d0ab94e..50184be 100644 --- a/util/minecraftVersionToNumber.js +++ b/util/minecraftVersionToNumber.js @@ -5,6 +5,7 @@ */ function minecraftVersionToNumber (version) { const versionArray = version.split('.') + if (versionArray.length === 2) return Number(version) versionArray.pop() return Number(versionArray.join('.')) }