Skip to content
This repository was archived by the owner on Nov 15, 2022. It is now read-only.

Commit 78f038c

Browse files
authored
Add beforeLogin function option on server (PrismarineJS#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`
1 parent ac099c1 commit 78f038c

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

docs/API.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ automatically logged in and validated against mojang's auth.
1515
* beforePing : allow customisation of the answer to ping the server does.
1616
It takes a function with argument response and client, response is the default json response, and client is client who sent a ping.
1717
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.
18+
* beforeLogin : allow customisation of client before the `success` packet is sent.
19+
It takes a function with argument client and should be synchronous for the server to wait for completion before continuing execution.
1820
* motd : default to "A Minecraft server"
1921
* maxPlayers : default to 20
2022
* keepAlive : send keep alive packets : default to true

src/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ declare module 'minecraft-protocol' {
8282
port?: number
8383
version?: string
8484
beforePing?: (response: any, client: Client, callback?: (result: any) => any) => any
85+
beforeLogin?: (client: Client) => void
8586
errorHandler?: (client: Client, error: Error) => void
8687
agent?: Agent
8788
}

src/server/login.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ module.exports = function (client, server, options) {
109109
if (onlineMode === false || isException) {
110110
client.uuid = nameToMcOfflineUUID(client.username)
111111
}
112+
options.beforeLogin?.(client)
112113
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
113114
client.write('compress', { threshold: 256 }) // Default threshold is 256
114115
client.compressionThreshold = 256

test/serverTest.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,32 @@ for (const supportedVersion of mc.supportedVersions) {
191191
})
192192
server.on('close', done)
193193
})
194+
it('clients can be changed by beforeLogin', function (done) {
195+
const notchUUID = '069a79f4-44e9-4726-a5be-fca90e38aaf5'
196+
const server = mc.createServer({
197+
'online-mode': false,
198+
version: version.minecraftVersion,
199+
port: PORT,
200+
beforeLogin: (client) => {
201+
client.uuid = notchUUID
202+
}
203+
})
204+
server.on('listening', function () {
205+
const client = mc.createClient({
206+
username: 'notNotch',
207+
host: '127.0.0.1',
208+
version: version.minecraftVersion,
209+
port: PORT
210+
})
211+
client.on('packet', (data, {name})=>{
212+
if (name === 'success') {
213+
assert.strictEqual(data.uuid, notchUUID, 'UUID')
214+
server.close()
215+
}
216+
})
217+
})
218+
server.on('close', done)
219+
})
194220
it('clients can log in and chat', function (done) {
195221
const server = mc.createServer({
196222
'online-mode': false,

0 commit comments

Comments
 (0)