Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.20.4 chat not parsing #111

Open
Parker2991 opened this issue Aug 4, 2024 · 5 comments
Open

1.20.4 chat not parsing #111

Parker2991 opened this issue Aug 4, 2024 · 5 comments

Comments

@Parker2991
Copy link

Parker2991 commented Aug 4, 2024

so when i try to parse nbt chat for 1.20.3 and 1.20.4 its either a blank message or the bot reads it incorrectly

const loadPrismarineChat = require('prismarine-chat')
const kaboomChatParser = require('../chat/kaboom')
const creayunChatParser = require('../chat/creayun')
const fs = require('fs')
const chipmunkmodChatParser = require('../chat/chipmunkmod')
const chipmunkmodblackilykatverChatParser = require('../chat/chipmunkmodBlackilyKatVer')
const typetextChatParser = require('../chat/chatTypeText')
const typeemotetextChatParser = require('../chat/chatTypeEmote')
const nbt = require('prismarine-nbt')
function tryParse (json) {
  try {
    return JSON.parse(json)
  } catch (error) {
    return { text: '' }
  }
}
//  const chat = require('prismarine-chat'); const nbt = require('prismarine-nbt'); chat.processNbtMessage(nbt.comp({ text: nbt.string('hi!') }))
function parseNbt (json) {
  try {
    return loadPrismarineChat.processNbtMessage(nbt.comp({text: nbt.string(json)}))
  } catch (error) {
    return { text: '' }
  }
}
//what was changed??
function inject (bot) {

       // const ChatMessage = require('prismarine-chat')
        
  bot.on('registry_ready', registry => {
    ChatMessage = loadPrismarineChat(registry)
  })
   
  bot.chatParsers = [kaboomChatParser]

  bot.on('packet.profileless_chat', packet => {
      const message = JSON.parse(loadPrismarineChat.processNbtMessage(nbt.comp(nbt.string(packet.message))))
      const sender = JSON.parse(loadPrismarineChat.processNbtMessage(nbt.comp(nbt.string(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 => {
      unsigned = JSON.parse(loadPrismarineChat.processNbtMessage(nbt.comp(nbt.string(packet.unsignedChatContent))))
      bot.emit('player_chat', { plain: packet.plainMessage, unsigned, senderUuid: packet.senderUuid })
    
    bot.emit('message', unsigned)
  


    tryParsingMessage(unsigned, { senderUuid: packet.senderUuid, players: bot.players, getMessageAsPrismarine: bot.getMessageAsPrismarine})
  
    })
 bot.getMessageAsPrismarine = message => {
  try {
    if (ChatMessage !== undefined) {
      return new ChatMessage(message)
    }
  } catch (error) {
    console.error(error); // Log any errors that occur during object creation
  }
  return undefined;
}
       
  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())
  */
       
  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: message.substring(0, 256),
      timestamp: BigInt(Date.now()),      
      salt: 0n,
      offset: 0,
      acknowledged: bitset
      
    })

  }
    
  
  bot.command = command => {
    bot._client.write('chat_command', {
      command: command.substring(0, 256),
      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 = inject
const createRegistry = require('prismarine-registry')

function inject (bot) {
  bot.on('packet.login', packet => {
    bot.registry = createRegistry(bot._client.version)
    bot.registry.language = require('../data/language.json');
    bot.emit('registry_ready', bot.registry)
  })
}

module.exports = inject;
const util = require('util')

function parseMessage (message, data) {
  if (message === null || typeof message !== 'object') return

  if (message.text !== '' || !Array.isArray(message.extra) || message.extra.length < 3) return

  const children = message.extra

  const prefix = children[0]
  let displayName = data.senderName ?? { text: '' }
  let contents = { text: '' }

  if (isSeparatorAt(children, 1)) { // Missing/blank display name
    if (children.length > 3) contents = children[3]
  } else if (isSeparatorAt(children, 2)) {
    displayName = children[1]
    if (children.length > 4) contents = children[4]
  } else {
    return undefined
  }

  const playerListDisplayName = { extra: [prefix, displayName], text: '' }
  let sender
  if (data.uuid) {
    sender = data.players.find(player => player.uuid === data.senderUuid)
  } else {
    const playerListDisplayName = { extra: [prefix, displayName], text: '' }
    sender = data.players.find(player => util.isDeepStrictEqual(player.displayName, playerListDisplayName))
  }

  if (!sender) return undefined

  return { sender, contents, type: 'minecraft:chat', displayName }
}

function isSeparatorAt (children, start) {
  return (children[start]?.text === ':' || children[start]?.text === '\xa7f:') && children[start + 1]?.text  === ' ' 
}

module.exports = parseMessage

the server is kaboom.pw that the code was tried on
the chat format is [] player§f: message
and it doesnt parse the : in the chat

@extremeheat
Copy link
Member

There is too much code here. Please post a snippet that can reproduce the bug you are reporting. For general support questions please use the Discord.

@Parker2991
Copy link
Author

Parker2991 commented Aug 6, 2024

  bot.on('packet.player_chat', packet => {
      unsigned = JSON.parse(loadPrismarineChat.processNbtMessage(nbt.comp(nbt.string(packet.unsignedChatContent))))
      bot.emit('player_chat', { plain: packet.plainMessage, unsigned, senderUuid: packet.senderUuid })
    
    bot.emit('message', unsigned)
  


    tryParsingMessage(unsigned, { senderUuid: packet.senderUuid, players: bot.players, getMessageAsPrismarine: bot.getMessageAsPrismarine})
  
    })
   bot.on('packet.profileless_chat', packet => {
  const message = JSON.parse(loadPrismarineChat.processNbtMessage(nbt.comp(nbt.string(packet.message))))
  const sender = JSON.parse(loadPrismarineChat.processNbtMessage(nbt.comp(nbt.string(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 })

})

im saying that when i try to parse nbt chat its either blank or it shows the : in the chat format of [] playerusername§f: message. system chat parses just fine

@Parker2991
Copy link
Author

Screenshot 2024-08-06 5 23 24 PM

@extremeheat
Copy link
Member

Why are you not using mineflayer?

@Parker2991
Copy link
Author

Parker2991 commented Aug 7, 2024

i prefer to use nmp it runs smoother than mineflayer and it fits my needs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants