Add onLoginPluginRequest callback in client Options

This commit is contained in:
IceTank 2024-03-27 13:49:43 +01:00
parent ccab9fb396
commit 6a15f4a0c6
4 changed files with 39 additions and 1 deletions

View file

@ -151,6 +151,7 @@ Returns a `Client` instance and perform login.
* realmId : The id of the Realm to join.
* pickRealm(realms) : A function which will have an array of the user Realms (joined/owned) passed to it. The function should return a Realm.
* Client : You can pass a custom client class to use instead of the default one, which would allow you to create completely custom communication. Also note that you can use the `stream` option instead where you can supply custom duplex, but this will still use serialization/deserialization of packets.
* onLoginPluginRequest(packet, meta) : (optional) callback that is called when a `login_plugin_request` packet is received. When this option is defined and of type function then the callback is called and expected to write a `login_plugin_response` packet on client with at least `messageId: packet.messageId` in the payload. If no login_plugin_response is send the connection will halt. When the `onLoginPluginRequest` option is not defined or not of type function then a login_plugin_response packet with `messageId: packet.messageId` and no data is send.
## mc.Client(isServer,version,[customPackets])

View file

@ -18,7 +18,13 @@ module.exports = function (client, options) {
const above385 = mcdata.version.version >= 385
if (above385) { // 1.13-pre3 (385) added Added Login Plugin Message (https://wiki.vg/Protocol_History#1.13-pre3)
client.on('login_plugin_request', onLoginPluginRequest)
client.on('login_plugin_request', (...args) => {
if (options.onLoginPluginRequest != null && typeof options.onLoginPluginRequest === 'function') {
options.onLoginPluginRequest(...args)
} else {
onLoginPluginRequest(...args)
}
})
}
const channelNames = above385 ? ['minecraft:register', 'minecraft:unregister'] : ['REGISTER', 'UNREGISTER']

1
src/index.d.ts vendored
View file

@ -141,6 +141,7 @@ declare module 'minecraft-protocol' {
disableChatSigning?: boolean
/** Pass custom client implementation if needed. */
Client?: Client
onLoginPluginRequest?: (packet: any, meta: any, ...args: any[]) => void
}
export class Server extends EventEmitter {

View file

@ -259,6 +259,36 @@ for (const supportedVersion of mc.supportedVersions) {
})
})
})
it('sends a valid login_plugin_request packet', function (done) {
const onLoginPluginRequest = function (packet) {
client.write('login_plugin_response', {
messageId: packet.messageId,
data: Buffer.from('Hallo from nmp')
})
}
const client = mc.createClient({
username: 'Player',
version: version.minecraftVersion,
port: PORT,
onLoginPluginRequest: onLoginPluginRequest
})
client.on('error', err => done(err))
client.on('login', function () {
client.write('login_plugin_request', {
messageId: 0,
channel: 'minecraft:brand',
data: Buffer.from([0x00])
})
})
client.on('login_plugin_response', function (packet) {
assert.strictEqual(packet.messageId, 0)
assert.strictEqual(!!packet.data)
console.info('Client send in plugin response: ' + packet.data.toString())
client.end()
done()
})
})
})
describe.skip('online', function () {