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.3 / 1.20.4 support #1275

Merged
merged 19 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ The client's protocol version

### client.version

The client's version
The client's version, as a string

### `packet` event

Expand Down
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Parse and serialize minecraft packets, plus authentication and encryption.

* Supports Minecraft PC version 1.7.10, 1.8.8, 1.9 (15w40b, 1.9, 1.9.1-pre2, 1.9.2, 1.9.4),
1.10 (16w20a, 1.10-pre1, 1.10, 1.10.1, 1.10.2), 1.11 (16w35a, 1.11, 1.11.2), 1.12 (17w15a, 17w18b, 1.12-pre4, 1.12, 1.12.1, 1.12.2), and 1.13 (17w50a, 1.13, 1.13.1, 1.13.2-pre1, 1.13.2-pre2, 1.13.2), 1.14 (1.14, 1.14.1, 1.14.3, 1.14.4)
, 1.15 (1.15, 1.15.1, 1.15.2) and 1.16 (20w13b, 20w14a, 1.16-rc1, 1.16, 1.16.1, 1.16.2, 1.16.3, 1.16.4, 1.16.5), 1.17 (21w07a, 1.17, 1.17.1), 1.18 (1.18, 1.18.1 and 1.18.2), 1.19 (1.19, 1.19.1, 1.19.2, 1.19.3, 1.19.4), 1.20 (1.20, 1.20.1, 1.20.2)
, 1.15 (1.15, 1.15.1, 1.15.2) and 1.16 (20w13b, 20w14a, 1.16-rc1, 1.16, 1.16.1, 1.16.2, 1.16.3, 1.16.4, 1.16.5), 1.17 (21w07a, 1.17, 1.17.1), 1.18 (1.18, 1.18.1 and 1.18.2), 1.19 (1.19, 1.19.1, 1.19.2, 1.19.3, 1.19.4), 1.20 (1.20, 1.20.1, 1.20.2, 1.20.3 and 1.20.4)
* Parses all packets and emits events with packet fields as JavaScript
objects.
* Send a packet by supplying fields as a JavaScript object.
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"minecraft-wrap": "^1.2.3",
"mocha": "^10.0.0",
"power-assert": "^1.0.0",
"prismarine-chat": "^1.9.1",
"standard": "^17.0.0"
},
"dependencies": {
Expand All @@ -51,7 +52,7 @@
"endian-toggle": "^0.0.0",
"lodash.get": "^4.1.2",
"lodash.merge": "^4.3.0",
"minecraft-data": "^3.53.0",
"minecraft-data": "^3.55.0",
"minecraft-folder-path": "^1.2.0",
"node-fetch": "^2.6.1",
"node-rsa": "^0.4.2",
Expand Down
33 changes: 25 additions & 8 deletions src/client/chat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const crypto = require('crypto')
const concat = require('../transforms/binaryStream').concat
const nbt = require('prismarine-nbt')
const uuid = require('../datatypes/uuid')
const messageExpireTime = 420000 // 7 minutes (ms)

function isFormatted (message) {
Expand All @@ -22,6 +24,22 @@ module.exports = function (client, options) {
client._lastChatSignature = null
client._lastRejectedMessage = null

// 1.20.3+ serializes chat components in chat packets with NBT. Non-chat packets that send messages (like disconnect) still use JSON chat.
// NMP API expects a JSON string, so since the schema is mostly the same we can convert the NBT to JSON with a transform to UUID encoding
function handleNbtComponent (nbtDataOrString) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should expose that function here or in pchat so that mineflayer and flying-squid can use it

Copy link
Member

@extremeheat extremeheat Jan 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added prismarine-chat to the test code so it's cleaner (as opposed to handling chat internally), but otherwise there isn't much to add to prismarine-chat. The handleNbtComponent basically just calls simplify and does some mapping for UUIDs.

But I guess there could be a ChatMessage.fromNbtComponent() inside prismarine-chat to get a ChatMessage directly. But would it would require a change in nmp API as playerChat and systemChat current expect JSON strings, not ChatMessage's

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my concern is how do we handle the "chat nbt" in the 10 other non-chat packets where that "chat nbt" is used in mineflayer?

I can see these solutions

  1. we duplicate handleNbtComponent in nmp and mineflayer and flying-squid (and any other nmp users)
  2. we expose handleNbtComponent in nmp and use them in users
  3. we expose handleNbtComponent in pchat and use it in nmp, mineflayer, flying-squid and other places
  4. we expose handleNbtComponent in pnbt and use it in all users

I think 3 or 4 are better.
2 is okay

1 I don't think it's a good idea

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@extremeheat extremeheat Jan 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah I see, looks like will need some changes to all node-minecraft-protocol, prismarine-chat, mineflayer to swap the event from JSON string to a ChatMessage for #4. Maybe updating fromNotch() to and then emitting the ChatMessage:

    static fromNotch (msg) {
      if (registry.supportFeature('chatPacketsUseNbtComponents') && msg.type) {
        const simplified = nbt.simplify(msg)
        const json = JSON.stringify(simplified, (key, val) => {
          if (key === 'id' && Array.isArray(val)) return uuidFromIntArray(val)
          return val
        })
        return new ChatMessage(JSON.parse(json))
      } else try {
        return new ChatMessage(JSON.parse(msg))
      } catch (e) {
        return new ChatMessage(msg)
      }      
    }

However doing this would break the fromNetwork in prismarine-chat as it expects a JSON object when formatting the chat string, so that too would need to be updated.

Would also need to figure out a good way to do toNotch()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should expose an independent method in pchat

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but yeah hmm let's figure out how to unblock this

if (mcData.supportFeature('chatPacketsUseNbtComponents') && nbtDataOrString) {
// UUIDs are encoded in NBT as a 4x i32 array, so convert to a hex string
const simplified = nbt.simplify(nbtDataOrString)
return JSON.stringify(simplified, (key, val) => {
if (key === 'id' && Array.isArray(val)) return uuid.fromIntArray(val)
return val
})
}
// already plaintext JSON or empty
return nbtDataOrString
}
client._handleNbtComponent = handleNbtComponent
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this exposed privately and then not used?

if we want to actually expose it, it should be exposed publicly (without _ and in API.md)


// This stores the last n (5 or 20) messages that the player has seen, from unique players
if (mcData.supportFeature('chainedChatWithHashing')) client._lastSeenMessages = new LastSeenMessages()
else client._lastSeenMessages = new LastSeenMessagesWithInvalidation()
Expand Down Expand Up @@ -139,12 +157,11 @@ module.exports = function (client, options) {

client.on('profileless_chat', (packet) => {
// Profileless chat is parsed as an unsigned player chat message but logged as a system message

client.emit('playerChat', {
formattedMessage: packet.message,
formattedMessage: handleNbtComponent(packet.message),
type: packet.type,
senderName: packet.name,
targetName: packet.target,
senderName: handleNbtComponent(packet.name),
targetName: handleNbtComponent(packet.target),
verified: false
})

Expand All @@ -160,7 +177,7 @@ module.exports = function (client, options) {
client.on('system_chat', (packet) => {
client.emit('systemChat', {
positionId: packet.isActionBar ? 2 : 1,
formattedMessage: packet.content
formattedMessage: handleNbtComponent(packet.content)
})

client._lastChatHistory.push({
Expand Down Expand Up @@ -198,11 +215,11 @@ module.exports = function (client, options) {
if (verified) client._signatureCache.push(packet.signature)
client.emit('playerChat', {
plainMessage: packet.plainMessage,
unsignedContent: packet.unsignedChatContent,
unsignedContent: handleNbtComponent(packet.unsignedChatContent),
type: packet.type,
sender: packet.senderUuid,
senderName: packet.networkName,
targetName: packet.networkTargetName,
senderName: handleNbtComponent(packet.networkName),
targetName: handleNbtComponent(packet.networkTargetName),
verified
})

Expand Down
8 changes: 7 additions & 1 deletion src/datatypes/uuid.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ function nameToMcOfflineUUID (name) {
return (new UUID(javaUUID('OfflinePlayer:' + name))).toString()
}

module.exports = { nameToMcOfflineUUID }
function fromIntArray (arr) {
const buf = Buffer.alloc(16)
arr.forEach((num, index) => { buf.writeInt32BE(num, index * 4) })
return buf.toString('hex')
}

module.exports = { nameToMcOfflineUUID, fromIntArray }
4 changes: 2 additions & 2 deletions src/version.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

module.exports = {
defaultVersion: '1.20.2',
supportedVersions: ['1.7', '1.8.8', '1.9.4', '1.10.2', '1.11.2', '1.12.2', '1.13.2', '1.14.4', '1.15.2', '1.16.5', '1.17.1', '1.18.2', '1.19', '1.19.2', '1.19.3', '1.19.4', '1.20', '1.20.1', '1.20.2']
defaultVersion: '1.20.4',
supportedVersions: ['1.7', '1.8.8', '1.9.4', '1.10.2', '1.11.2', '1.12.2', '1.13.2', '1.14.4', '1.15.2', '1.16.5', '1.17.1', '1.18.2', '1.19', '1.19.2', '1.19.3', '1.19.4', '1.20', '1.20.1', '1.20.2', '1.20.4']
}
16 changes: 9 additions & 7 deletions test/clientTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ for (const supportedVersion of mc.supportedVersions) {
'server-port': PORT,
motd: 'test1234',
'max-players': 120,
// 'level-type': 'flat',
'use-native-transport': 'false' // java 16 throws errors without this, https://www.spigotmc.org/threads/unable-to-access-address-of-buffer.311602
}, (err) => {
if (err) reject(err)
Expand Down Expand Up @@ -165,21 +166,22 @@ for (const supportedVersion of mc.supportedVersions) {
}
} else {
// 1.19+

const message = JSON.parse(data.formattedMessage || JSON.stringify({ text: data.plainMessage }))
console.log('Chat Message', data)
const sender = JSON.parse(data.senderName)
const msgPayload = data.formattedMessage ? JSON.parse(data.formattedMessage) : data.plainMessage
const plainMessage = client.parseMessage(msgPayload).toString()

if (chatCount === 1) {
assert.strictEqual(message.text, 'hello everyone; I have logged in.')
const sender = JSON.parse(data.senderName)
assert.strictEqual(plainMessage, 'hello everyone; I have logged in.')
assert.deepEqual(sender.clickEvent, {
action: 'suggest_command',
value: '/tell Player '
})
assert.strictEqual(sender.text, 'Player')
} else if (chatCount === 2) {
assert.strictEqual(message.text, 'hello')
const sender = JSON.parse(data.senderName)
assert.strictEqual(sender.text, 'Server')
const plainSender = client.parseMessage(sender).toString()
assert.strictEqual(plainMessage, 'hello')
assert.strictEqual(plainSender, 'Server')
wrap.removeListener('line', lineListener)
client.end()
done()
Expand Down
19 changes: 19 additions & 0 deletions test/common/clientHelpers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const Registry = require('prismarine-registry')
module.exports = client => {
client.nextMessage = (containing) => {
return new Promise((resolve) => {
Expand All @@ -20,5 +21,23 @@ module.exports = client => {
})
}

client.on('login', (packet) => {
client.registry ??= Registry(client.version)
if (packet.dimensionCodec) {
client.registry.loadDimensionCodec(packet.dimensionCodec)
}
})
client.on('registry_data', (data) => {
client.registry ??= Registry(client.version)
client.registry.loadDimensionCodec(data.codec)
})

client.on('playerJoin', () => {
const ChatMessage = require('prismarine-chat')(client.registry || client.version)
client.parseMessage = (comp) => {
return new ChatMessage(comp)
}
})

return client
}
4 changes: 4 additions & 0 deletions test/packetTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ const values = {
packedChunkPos: {
x: 10,
z: 12
},
particle: {
particleId: 0,
data: null
}
}

Expand Down
Loading