From d8ff48258c36ee59f3bb7fd2c16241cba2fc15d7 Mon Sep 17 00:00:00 2001 From: u9g <43508353+u9g@users.noreply.github.com> Date: Wed, 14 Apr 2021 17:39:05 -0400 Subject: [PATCH] Add createClient (#61) * Initial commit * remove comment * export obj * fix export * add to api.md * fix old refs to nmp --- docs/api.md | 16 ++++++++++++ examples/createClientExample.js | 9 +++++++ index.js | 3 ++- src/createClient.js | 45 +++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 docs/api.md create mode 100644 examples/createClientExample.js create mode 100644 src/createClient.js diff --git a/docs/api.md b/docs/api.md new file mode 100644 index 0000000..f5f6564 --- /dev/null +++ b/docs/api.md @@ -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 diff --git a/examples/createClientExample.js b/examples/createClientExample.js new file mode 100644 index 0000000..6de4b9b --- /dev/null +++ b/examples/createClientExample.js @@ -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++ +}) diff --git a/index.js b/index.js index 6169a77..d6ba9b1 100644 --- a/index.js +++ b/index.js @@ -2,5 +2,6 @@ module.exports = { ...require('./src/client'), ...require('./src/server'), ...require('./src/serverPlayer'), - ...require('./src/relay') + ...require('./src/relay'), + ...require('./src/createClient') } diff --git a/src/createClient.js b/src/createClient.js new file mode 100644 index 0000000..612fd48 --- /dev/null +++ b/src/createClient.js @@ -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 }) +}