Update convenience chat events so higher-level clients can properly parse chat (#1055)

* Emit systemChat event in 1.19+

* Pass both plain message and unsigned content

* Rename property for clarity

* Fix verifyMessage throwing in 1.19

* Update docs

* Document systemChat event
This commit is contained in:
Frej Alexander Nielsen 2023-01-15 18:28:46 +01:00 committed by GitHub
parent 2fc4b917d9
commit 0526edf5f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 3 deletions

View file

@ -261,12 +261,19 @@ Called when an error occurs within the client. Takes an Error as parameter.
Called when a chat message from another player arrives. The emitted object contains:
* formattedMessage -- the chat message preformatted, if done on server side
* message -- the chat message without formatting (for example no `<username> message` ; instead `message`), on version 1.19+
* plainMessage -- the chat message without formatting (for example no `<username> message` ; instead `message`), on version 1.19+
* unsignedContent -- unsigned formatted chat contents ; should only be present when the message is modified and server has chat previews disabled - only on version 1.19 - 1.19.2
* type -- the message type - on 1.19, which format string to use to render message ; below, the place where the message is displayed (for example chat or action bar)
* sender -- the UUID of the player sending the message
* senderTeam -- scoreboard team of the player (pre 1.19)
* verified -- true if message is signed, false if not signed, undefined on versions prior to 1.19
### `systemChat` event
Called when a system chat message arrives. A system chat message is any message not sent by a player. The emitted object contains:
* formattedMessage -- the chat message preformatted
* positionid -- the chat type of the message. 1 for system chat and 2 for actionbar
See the [chat example](https://github.com/PrismarineJS/node-minecraft-protocol/blob/master/examples/client_chat/client_chat.js#L1) for usage.
### per-packet events

View file

@ -69,6 +69,13 @@ module.exports = function (client, options) {
}
})
client.on('system_chat', (packet) => {
client.emit('systemChat', {
positionid: packet.isActionBar ? 2 : 1,
formattedMessage: packet.content
})
})
client.on('message_header', (packet) => {
updateAndValidateChat(packet.senderUuid, packet.previousSignature, packet.signature, packet.messageHash)
@ -106,7 +113,8 @@ module.exports = function (client, options) {
const expired = !packet.timestamp || tsDelta > messageExpireTime || tsDelta < 0
const verified = updateAndValidateChat(packet.senderUuid, packet.previousSignature, packet.signature, hash.digest()) && !expired
client.emit('playerChat', {
message: packet.plainMessage || packet.unsignedChatContent,
plainMessage: packet.plainMessage,
unsignedContent: packet.unsignedChatContent,
formattedMessage: packet.formattedMessage,
type: packet.type,
sender: packet.senderUuid,
@ -220,7 +228,7 @@ module.exports = function (client, options) {
}
client.verifyMessage = (pubKey, packet) => {
if (!mcData.supportFeature('chainedChatWithHashing')) { // 1.19.0
if (mcData.supportFeature('chainedChatWithHashing')) { // 1.19.1+
// Verification handled internally in 1.19.1+ as previous messages must be stored to verify future messages
throw new Error("Please listen to the 'playerChat' event instead to check message validity. client.verifyMessage is deprecated and only works on version 1.19.")
}