Add createClient (#61)

* Initial commit

* remove comment

* export obj

* fix export

* add to api.md

* fix old refs to nmp
This commit is contained in:
u9g 2021-04-14 17:39:05 -04:00 committed by GitHub
commit d8ff48258c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 1 deletions

45
src/createClient.js Normal file
View file

@ -0,0 +1,45 @@
const { Client } = require('./client')
const assert = require('assert')
module.exports = { createClient }
/** @param {{ version?: number, hostname: string, port?: number }} options */
function createClient (options) {
assert(options && options.hostname)
const client = new Client({ port: 19132, ...options })
client.once('resource_packs_info', (packet) => {
handleResourcePackInfo(client)
disableClientCache(client)
handleRenderDistance(client)
handleTickSync(client)
})
return client
}
function handleResourcePackInfo (client) {
client.write('resource_pack_client_response', {
response_status: 'completed',
resourcepackids: []
})
client.once('resource_pack_stack', (stack) => {
client.write('resource_pack_client_response', {
response_status: 'completed',
resourcepackids: []
})
})
}
function handleRenderDistance (client) {
client.queue('request_chunk_radius', { chunk_radius: 1 })
}
function disableClientCache (client) {
client.queue('client_cache_status', { enabled: false })
}
function handleTickSync (client) {
client.queue('tick_sync', { request_time: BigInt(Date.now()), response_time: 0n })
}