Skip to content

Commit 8e131c3

Browse files
authored
Fix server_data payload for 1.19+, fix kicks messages on 1.20.3+ (#1364)
* Fix server_data payload sent to clients for versions 1.19+ Fixes #1362 Add missing fields to `server_data` payload for versions 1.19+. * Add `motd` and `icon` fields to `server_data` payload for version 1.19. * Add `motd`, `icon`, and `enforcesSecureChat` fields to `server_data` payload for version 1.19.2. * Add `motd`, `icon`, and `enforcesSecureChat` fields to `server_data` payload for version 1.19.3. * Add `motd`, `iconBytes`, and `enforcesSecureChat` fields to `server_data` payload for versions 1.19.4, 1.20, and 1.20.2. * Add `motd`, `iconBytes`, and `enforcesSecureChat` fields to `server_data` payload for version 1.20.3. * Add `motd` and `iconBytes` fields to `server_data` payload for version 1.20.5+. * Use NBT components for `motd` if `chatPacketsUseNbtComponents` feature is supported. * Convert `favicon` to buffer for `iconBytes` field if available. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/PrismarineJS/node-minecraft-protocol/issues/1362?shareId=XXXX-XXXX-XXXX-XXXX). * add --retries 2 * lint, debug close event emit twice * bail tests * fix * flaky test fix * remove debug * Update serverTest.js * Fix NBT chat not being used for 1.20.3+ kicks * Update createClient.js * fix client.._supportFeature not being defined * only nbt on the play state disconnect
1 parent f258c76 commit 8e131c3

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed

src/client.js

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Client extends EventEmitter {
3030
this.hideErrors = hideErrors
3131
this.closeTimer = null
3232
const mcData = require('minecraft-data')(version)
33+
this._supportFeature = mcData.supportFeature
3334
this.state = states.HANDSHAKING
3435
this._hasBundlePacket = mcData.supportFeature('hasBundlePacket')
3536
}

src/createServer.js

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ function createServer (options = {}) {
4646
server.onlineModeExceptions = Object.create(null)
4747
server.favicon = favicon
4848
server.options = options
49+
server._supportFeature = mcData.supportFeature
4950
options.registryCodec = options.registryCodec || mcData.registryCodec || mcData.loginPacket?.dimensionCodec
5051

5152
// The RSA keypair can take some time to generate

src/server.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const net = require('net')
44
const EventEmitter = require('events').EventEmitter
55
const Client = require('./client')
66
const states = require('./states')
7+
const nbt = require('prismarine-nbt')
78
const { createSerializer } = require('./transforms/serializer')
89

910
class Server extends EventEmitter {
@@ -26,11 +27,14 @@ class Server extends EventEmitter {
2627
self.socketServer.on('connection', socket => {
2728
const client = new Client(true, this.version, this.customPackets, this.hideErrors)
2829
client._end = client.end
29-
client.end = function end (endReason, fullReason = JSON.stringify({ text: endReason })) {
30+
client.end = function end (endReason, fullReason) {
3031
if (client.state === states.PLAY) {
32+
fullReason ||= this._supportFeature('chatPacketsUseNbtComponents')
33+
? nbt.comp({ text: nbt.string(endReason) })
34+
: JSON.stringify({ text: endReason })
3135
client.write('kick_disconnect', { reason: fullReason })
3236
} else if (client.state === states.LOGIN) {
33-
client.write('disconnect', { reason: fullReason })
37+
client.write('disconnect', { reason: fullReason || endReason })
3438
}
3539
client._end(endReason)
3640
}

src/server/login.js

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const chatPlugin = require('./chat')
77
const { concat } = require('../transforms/binaryStream')
88
const { mojangPublicKeyPem } = require('./constants')
99
const debug = require('debug')('minecraft-protocol')
10+
const nbt = require('prismarine-nbt')
1011

1112
/**
1213
* @param {import('../index').Client} client
@@ -196,7 +197,12 @@ module.exports = function (client, server, options) {
196197
client.settings = {}
197198

198199
if (client.supportFeature('chainedChatWithHashing')) { // 1.19.1+
200+
const jsonMotd = JSON.stringify(server.motdMsg ?? { text: server.motd })
201+
const nbtMotd = nbt.comp({ text: nbt.string(server.motd) })
199202
client.write('server_data', {
203+
motd: client.supportFeature('chatPacketsUseNbtComponents') ? nbtMotd : jsonMotd,
204+
icon: server.favicon, // b64
205+
iconBytes: server.favicon ? Buffer.from(server.favicon, 'base64') : undefined,
200206
previewsChat: options.enableChatPreview,
201207
// Note: in 1.20.5+ user must send this with `login`
202208
enforcesSecureChat: options.enforceSecureProfile

test/serverTest.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ for (const supportedVersion of mc.supportedVersions) {
102102
describe('mc-server ' + supportedVersion + 'v', function () {
103103
this.timeout(5000)
104104
this.beforeEach(async function () {
105+
console.log('🔻 Starting test', this.currentTest.title)
105106
PORT = await getPort()
106107
console.log(`Using port for tests: ${PORT}`)
107108
})
@@ -411,9 +412,12 @@ for (const supportedVersion of mc.supportedVersions) {
411412
})
412413
})
413414
function checkFinish () {
414-
if (serverPlayerDisconnected && clientClosed && serverClosed) done()
415+
if (serverPlayerDisconnected && clientClosed && serverClosed) {
416+
console.log('Kick test is done')
417+
callOnce(done)
418+
}
415419
}
416-
})
420+
}).retries(2)
417421

418422
it('gives correct reason for kicking clients when shutting down', function (done) {
419423
const server = mc.createServer({
@@ -532,3 +536,10 @@ for (const supportedVersion of mc.supportedVersions) {
532536
})
533537
})
534538
}
539+
540+
function callOnce (fn, ...args) {
541+
console.log('Call Fn', fn.called)
542+
if (fn.called) return
543+
fn(...args)
544+
fn.called = true
545+
}

0 commit comments

Comments
 (0)