Add beforeLogin function option on server (#871)

* Add `beforeLogin` event on server

* optional function passin instead of emitted event

* Add documentation and bump version

* undo release push

* add test for `beforeLogin`
This commit is contained in:
Rob9315 2021-07-06 12:42:00 +02:00 committed by GitHub
parent ac099c1d31
commit 78f038cae6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 0 deletions

View file

@ -15,6 +15,8 @@ automatically logged in and validated against mojang's auth.
* beforePing : allow customisation of the answer to ping the server does.
It takes a function with argument response and client, response is the default json response, and client is client who sent a ping.
It can take as third argument a callback. If the callback is passed, the function should pass its result to the callback, if not it should return.
* beforeLogin : allow customisation of client before the `success` packet is sent.
It takes a function with argument client and should be synchronous for the server to wait for completion before continuing execution.
* motd : default to "A Minecraft server"
* maxPlayers : default to 20
* keepAlive : send keep alive packets : default to true

1
src/index.d.ts vendored
View file

@ -82,6 +82,7 @@ declare module 'minecraft-protocol' {
port?: number
version?: string
beforePing?: (response: any, client: Client, callback?: (result: any) => any) => any
beforeLogin?: (client: Client) => void
errorHandler?: (client: Client, error: Error) => void
agent?: Agent
}

View file

@ -109,6 +109,7 @@ module.exports = function (client, server, options) {
if (onlineMode === false || isException) {
client.uuid = nameToMcOfflineUUID(client.username)
}
options.beforeLogin?.(client)
if (client.protocolVersion >= 27) { // 14w28a (27) added whole-protocol compression (http://wiki.vg/Protocol_History#14w28a), earlier versions per-packet compressed TODO: refactor into minecraft-data
client.write('compress', { threshold: 256 }) // Default threshold is 256
client.compressionThreshold = 256

View file

@ -191,6 +191,32 @@ for (const supportedVersion of mc.supportedVersions) {
})
server.on('close', done)
})
it('clients can be changed by beforeLogin', function (done) {
const notchUUID = '069a79f4-44e9-4726-a5be-fca90e38aaf5'
const server = mc.createServer({
'online-mode': false,
version: version.minecraftVersion,
port: PORT,
beforeLogin: (client) => {
client.uuid = notchUUID
}
})
server.on('listening', function () {
const client = mc.createClient({
username: 'notNotch',
host: '127.0.0.1',
version: version.minecraftVersion,
port: PORT
})
client.on('packet', (data, {name})=>{
if (name === 'success') {
assert.strictEqual(data.uuid, notchUUID, 'UUID')
server.close()
}
})
})
server.on('close', done)
})
it('clients can log in and chat', function (done) {
const server = mc.createServer({
'online-mode': false,