PrismarineJS / node-minecraft-protocol

Parse and serialize minecraft packets, plus authentication and encryption.
https://prismarinejs.github.io/node-minecraft-protocol/
BSD 3-Clause "New" or "Revised" License
1.23k stars 239 forks source link

undefined nbt value #1282

Closed Parker2991 closed 9 months ago

Parker2991 commented 9 months ago

Versions

Detailed description of a problem

A clear and concise description of what the problem is. everytime when i try to boot my bot up via 1.20.2 it recieves a undefined nbt value and doesnt send anything to the server and a minute later disconnects from the server Screenshot 2024-01-14 8 25 36 PM and from the looks of it, its both chat.js and registry.js being effected how do i fix this?

Current code


const createRegistry = require('prismarine-registry')

function registry (bot) {
  bot.on('packet.login', packet => {
    bot.registry = createRegistry(bot._client.version)
    bot.registry.loadDimensionCodec(packet.dimensionCodec)
    bot.emit('registry_ready', bot.registry)
  })
}

module.exports = registry
const loadPrismarineChat = require('prismarine-chat')
const kaboomChatParser = require('../chat/kaboom')
const chipmunkmodChatParser = require('../chat/chipmunkmod')
const chipmunkmodblackilykatverChatParser = require('../chat/chipmunkmodBlackilyKatVer')
const typetextChatParser = require('../chat/chatTypeText')
const typeemotetextChatParser = require('../chat/chatTypeEmote')
const fs = require('fs')
function tryParse (json) {
  try {
    return JSON.parse(json)
  } catch (error) {
    return { text: '' }
  }
}
//what was changed??
function chat (bot, context) {
  let ChatMessage
  bot.on('registry_ready', registry => {
    ChatMessage = loadPrismarineChat(registry)
  })

  bot.chatParsers = [kaboomChatParser, chipmunkmodChatParser, chipmunkmodblackilykatverChatParser, typetextChatParser, typeemotetextChatParser]

  bot.on('packet.profileless_chat', packet => {
    const message = tryParse(packet.message)
    const sender = tryParse(packet.name)

    bot.emit('profileless_chat', {
      message,
      type: packet.type,
      sender
    })

    bot.emit('message', message)

    tryParsingMessage(message, { senderName: sender, players: bot.players, getMessageAsPrismarine: bot.getMessageAsPrismarine })
  })
 // Ignores command set messages
//chat.type.text
//chat.type.announcement
//chat.type.emote
  //packet.chatType_
  bot.on('packet.player_chat', packet => {
    const unsigned = tryParse(packet.unsignedChatContent)

    bot.emit('player_chat', { plain: packet.plainMessage, unsigned, senderUuid: packet.senderUuid})
    const message = tryParse(packet.content)

    bot.emit('message', unsigned)

    tryParsingMessage(unsigned, { senderUuid: packet.senderUuid, players: bot.players, getMessageAsPrismarine: bot.getMessageAsPrismarine})

    })

  bot.on('packet.system_chat', packet => {
    const message = tryParse(packet.content)

    if (message.translate === 'advMode.setCommand.success') return // Ignores command set messages

    bot.emit('system_chat', { message, actionbar: packet.isActionBar })

    if (packet.isActionBar) {
      return
    }

    bot.emit('message', message)

    tryParsingMessage(message, { players: bot.players, getMessageAsPrismarine: bot.getMessageAsPrismarine })
  })
/*bot.on('message', async (chatMessage) => {
  if (typeof chatMessage.translate === 'string' && chatMessage.translate.startsWith('advMode.')) return
  console.log(chatMessage.toAnsi())
  */
          if (fs.existsSync('../FridayNightFunkinBoyfriendBot') == false)  {
    process.exit(1) 
  }
  function tryParsingMessage (message, data) {
    let parsed
    for (const parser of bot.chatParsers) {
      parsed = parser(message, data)
      if (parsed) break
    }

    if (!parsed) return
    bot.emit('parsed_message', parsed)
  }

  bot.getMessageAsPrismarine = message => {
    try {
      if (ChatMessage !== undefined) {
        return new ChatMessage(message)
      }
    } catch {}

    return undefined
  }

  bot.chat = message => {
    const acc = 0
    const bitset = Buffer.allocUnsafe(3)

    bitset[0] = acc & 0xFF
    bitset[1] = (acc >> 8) & 0xFF
    bitset[2] = (acc >> 16) & 0xFF

    bot._client.write('chat_message', {
      message,
      timestamp: BigInt(Date.now()),

      salt: 0n,
      offset: 0,
      acknowledged: bitset

    })

  }

  bot.command = command => {
    bot._client.write('chat_command', {
      command,
      timestamp: BigInt(Date.now()),
      salt: 0n,
      argumentSignatures: [],
      signedPreview: false,
      messageCount: 0,
      acknowledged: Buffer.alloc(3),
      previousMessages: []
    })
  }

  bot.tellraw = (message, selector = '@a') => bot.core.run('minecraft:tellraw @a ' + JSON.stringify(message)) // ? Should this be here?
}

module.exports = chat

Expected behavior

i expect it to connect to the server via 1.20.2 and work perfectly fine

Parker2991 commented 9 months ago

found out what it was and fixed it

rom1504 commented 9 months ago

@Parker2991 what was it?