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

16
docs/api.md Normal file
View file

@ -0,0 +1,16 @@
# Documentation
## be.createClient(options)
Returns a `Client` instance and starts listening. All clients will be
automatically logged in and validated against microsoft's auth.
`options` is an object containing the properties :
* host : default to undefined which means listen to all available ipv4 and ipv6 adresses
* port (optional) : default to 25565
(see https://nodejs.org/api/net.html#net_server_listen_port_host_backlog_callback for details)
* kickTimeout (optional) : default to `10*1000` (10s), kick client that doesn't answer to keepalive after that time
* version (optional) : default to latest stable version, version of server
* autoInitPlayer (optional) : default to true, If we should send SetPlayerInitialized to the server after getting play_status spawn.
* offline (optional) : default to false, whether to auth with microsoft
* connectTimeout (optional) : default to 9000, ms to wait before aborting connection attempt

View file

@ -0,0 +1,9 @@
const { createClient } = require('bedrock-protocol')
const client = createClient({ hostname: '127.0.0.1' })
let ix = 0
client.on('packet', (args) => {
console.log(`Packet ${ix} recieved`)
ix++
})

View file

@ -2,5 +2,6 @@ module.exports = {
...require('./src/client'),
...require('./src/server'),
...require('./src/serverPlayer'),
...require('./src/relay')
...require('./src/relay'),
...require('./src/createClient')
}

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 })
}