mirror of
https://github.com/PrismarineJS/node-minecraft-protocol.git
synced 2024-11-14 19:04:59 -05:00
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:
parent
ac099c1d31
commit
78f038cae6
4 changed files with 30 additions and 0 deletions
|
@ -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
1
src/index.d.ts
vendored
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue