1.19 support for both bot and proxy

this somehow took 1 hour(?)
This commit is contained in:
ChomeNS 2022-12-10 13:31:01 +07:00
parent c415d94e50
commit e1a1d92723
8 changed files with 100 additions and 42 deletions

View file

@ -1,5 +1,5 @@
module.exports = { module.exports = {
version: '1.18.2', version: '1.19',
prefixes: [ prefixes: [
'*', '*',
'cbot ', 'cbot ',
@ -12,7 +12,7 @@ module.exports = {
}, },
proxy: { proxy: {
enabled: true, enabled: true,
version: '1.18.2' version: '1.19'
}, },
console: true, console: true,
chat: { chat: {

View file

@ -3,6 +3,7 @@
// const parse = require('../util/text_parser'); // const parse = require('../util/text_parser');
const { containsIllegalCharacters } = require('../util/containsIllegalCharacters') const { containsIllegalCharacters } = require('../util/containsIllegalCharacters')
const { chatPacketListener, parsePlayerMessages } = require('../util/chat') const { chatPacketListener, parsePlayerMessages } = require('../util/chat')
const minecraftVersionToNumber = require('../util/minecraftVersionToNumber')
function inject (bot, dcclient, config) { function inject (bot, dcclient, config) {
bot.chatQueue = [] bot.chatQueue = []
@ -29,7 +30,27 @@ function inject (bot, dcclient, config) {
const chatQueueInterval = setInterval(function () { const chatQueueInterval = setInterval(function () {
if (bot._chatQueue.length !== 0) { if (bot._chatQueue.length !== 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.write('chat', { message: bot._chatQueue[0] })
}
bot._chatQueue.shift() bot._chatQueue.shift()
} }
}, 450) }, 450)
@ -43,9 +64,15 @@ function inject (bot, dcclient, config) {
clearInterval(_chatQueueInterval) clearInterval(_chatQueueInterval)
}) })
const ChatMessage = require('prismarine-chat')(bot.version) function listener (packet) {
chatPacketListener(
bot._client.on('chat', (packet) => chatPacketListener(packet, ChatMessage, bot)) 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)) bot.on('parsed_chat', (message, packet) => parsePlayerMessages(message, packet, bot))
} }

View file

@ -2,6 +2,7 @@
const util = require('util') const util = require('util')
const mc = require('minecraft-protocol') const mc = require('minecraft-protocol')
const { loadPlugins } = require('../util/loadPlugins') const { loadPlugins } = require('../util/loadPlugins')
const minecraftVersionToNumber = require('../util/minecraftVersionToNumber')
function inject (bot, dcclient, config) { function inject (bot, dcclient, config) {
if (!config.proxy.enabled) return if (!config.proxy.enabled) return
@ -31,16 +32,36 @@ function inject (bot, dcclient, config) {
version version
}) })
const clientPacketBlacklist = []
const targetPacketBlacklist = [] const targetPacketBlacklist = []
target.chat = function (message) { target.chat = function (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.write('chat', { message })
} }
}
target.on('login', (packet) => { target.on('login', (packet) => {
bot.console.info(`[Proxy] ${client.username} target logged in`) bot.console.info(`[Proxy] ${client.username} target logged in`)
target.entityId = packet.entityId 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) => { target.on('packet', (data, meta) => {
@ -53,8 +74,9 @@ function inject (bot, dcclient, config) {
target.on('error', () => {}) target.on('error', () => {})
target.on('end', () => { target.on('end', (reason) => {
target.end() target.end()
client.end(`Target disconnected with reason: ${util.inspect(reason)}`)
targetEnded = true targetEnded = true
}) })
@ -74,21 +96,7 @@ function inject (bot, dcclient, config) {
}) })
client.on('packet', (data, meta) => { client.on('packet', (data, meta) => {
if (meta.name === 'chat' && !data.message?.startsWith('/')) { if (clientPacketBlacklist.includes(meta.name)) return
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
}
target.write(meta.name, data) target.write(meta.name, data)
}) })

View file

@ -1,11 +1,12 @@
const { chatPacketListener, parsePlayerMessages } = require('../../util/chat') const { chatPacketListener, parsePlayerMessages } = require('../../util/chat')
const minecraftVersionToNumber = require('../../util/minecraftVersionToNumber')
function inject (bot, client, target) { function inject (bot, client, target) {
const ChatMessage = require('prismarine-chat')(bot.version) function listener (packet) {
chatPacketListener(packet, target, minecraftVersionToNumber(target.version) >= 1.19)
target.on('chat', (packet) => { }
chatPacketListener(packet, ChatMessage, target) target.on('chat', listener)
}) target.on('system_chat', listener)
target.on('parsed_chat', (message, packet) => { target.on('parsed_chat', (message, packet) => {
parsePlayerMessages(message, packet, target) parsePlayerMessages(message, packet, target)

View file

@ -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) const { MessageBuilder } = require('prismarine-chat')(bot.version)
client.on('packet', (data, meta) => { clientPacketBlacklist.push('chat')
if (meta.name === 'chat' && clientPacketBlacklist.push('chat_message')
!data.message?.startsWith('/') && client.on(minecraftVersionToNumber(target.version) >= 1.19 ? 'chat_message' : 'chat', (data) => {
!data.message?.startsWith('.') // 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) => { const codeParsedMessage = data.message.replace(/%[^%]+%/g, (code) => {
try { try {
// eslint-disable-next-line no-eval // eslint-disable-next-line no-eval
@ -33,6 +47,8 @@ function inject (bot, client, target) {
MessageBuilder.fromString('&7' + codeParsedMessage) MessageBuilder.fromString('&7' + codeParsedMessage)
] ]
}) })
} else {
target.chat(data)
} }
}) })
}; };

View file

@ -1,13 +1,15 @@
/** /**
* for the chat packet listener (in util cuz proxy + bot) * for the chat packet listener (in util cuz proxy + bot)
* @param {object} packet chat packet * @param {object} packet chat packet
* @param {object} ChatMessage basically just require prismarine-chat
* @param {object} bot bot * @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 catch prevents json parse error (which prob never happens but still..)
try { 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 // down here it prevents command set message
// for ayunboom cuz its 1.17.1 // for ayunboom cuz its 1.17.1
@ -19,10 +21,12 @@ function chatPacketListener (packet, ChatMessage, bot) {
// VVVVVVVVVVVVVVVVVVVVV // VVVVVVVVVVVVVVVVVVVVV
if (parsedMessage.translate === 'advMode.setCommand.success') return 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) bot.emit('parsed_chat', message, packet)
} catch (e) {} } catch (e) {
bot.console.error(e)
}
}; };
/** /**

View file

@ -11,9 +11,10 @@ const path = require('path')
* @param {object} target proxy target * @param {object} target proxy target
* @param {object} client proxy client * @param {object} client proxy client
* @param {boolean} proxy is proxy * @param {boolean} proxy is proxy
* @param {array} clientPacketBlacklist the client packet blacklist
* @param {array} targetPacketBlacklist target 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 dir = path.join(__dirname, '..', 'plugins', proxy ? 'proxy' : '')
const plugins = await fs.readdir(dir) const plugins = await fs.readdir(dir)
plugins.forEach((plugin) => { plugins.forEach((plugin) => {
@ -21,7 +22,7 @@ async function loadPlugins (bot, dcclient, config, rl, target, client, proxy, ta
try { try {
const plug = require(path.join(dir, plugin)) const plug = require(path.join(dir, plugin))
if (!proxy) plug.inject(bot, dcclient, config, rl) 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) { } catch (e) {
console.log(`Plugin ${plugin} is having exception loading the plugin:`) console.log(`Plugin ${plugin} is having exception loading the plugin:`)
console.log(util.inspect(e)) console.log(util.inspect(e))

View file

@ -5,6 +5,7 @@
*/ */
function minecraftVersionToNumber (version) { function minecraftVersionToNumber (version) {
const versionArray = version.split('.') const versionArray = version.split('.')
if (versionArray.length === 2) return Number(version)
versionArray.pop() versionArray.pop()
return Number(versionArray.join('.')) return Number(versionArray.join('.'))
} }