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 = {
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: {

View file

@ -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) {
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))
}

View file

@ -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) {
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)
})

View file

@ -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)

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)
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)
}
})
};

View file

@ -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)
}
};
/**

View file

@ -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))

View file

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