From 0bdd071876365b0ce7bc17dfe15fee74bb2df46b Mon Sep 17 00:00:00 2001 From: extremeheat Date: Fri, 26 Mar 2021 04:38:25 -0400 Subject: [PATCH] client example updates --- examples/clientTest.js | 26 ++++++++++++++------------ src/client.js | 7 ++++++- test/vanilla.js | 6 ++++++ tools/genPacketDumps.js | 2 +- 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/examples/clientTest.js b/examples/clientTest.js index 157f450..b523d19 100644 --- a/examples/clientTest.js +++ b/examples/clientTest.js @@ -1,10 +1,13 @@ process.env.DEBUG = 'minecraft-protocol raknet' const { Client } = require('bedrock-protocol') +const { ChunkColumn, Version } = require('bedrock-provider') async function test () { const client = new Client({ hostname: '127.0.0.1', - port: 19132 + port: 19130 + // You can specify version by adding : + // version: '1.16.210' }) client.once('resource_packs_info', (packet) => { @@ -20,23 +23,22 @@ async function test () { }) }) - // client.once('resource_packs_info', (packet) => { - // client.write('resource_pack_client_response', { - // response_status: 'completed', - // resourcepackids: [] - // }) - // }) client.queue('client_cache_status', { enabled: false }) client.queue('request_chunk_radius', { chunk_radius: 1 }) client.queue('tick_sync', { request_time: BigInt(Date.now()), response_time: 0n }) }) - // var read = 0; - // client.on('level_chunk', (packet) => { - // read++ - // fs.writeFileSync(`level_chunk-${read}.json`, JSON.stringify(packet, null, 2)) - // }) + client.on('level_chunk', async packet => { + const cc = new ChunkColumn(Version.v1_4_0, packet.x, packet.z) + await cc.networkDecodeNoCache(packet.payload, packet.sub_chunk_count) + let blocks = [] + for (let x = 0; x < 16; x++) { + for (let z = 0; z < 16; z++) { + blocks.push(cc.getBlock(x, 0, z)) // Read some blocks in this chunk + } + } + }) } test() diff --git a/src/client.js b/src/client.js index bd841b3..829eb0d 100644 --- a/src/client.js +++ b/src/client.js @@ -41,6 +41,7 @@ class Client extends Connection { } this.startGameData = {} + this.clientRuntimeId = null this.startQueue() this.inLog = (...args) => debug('C ->', ...args) @@ -60,6 +61,10 @@ class Client extends Connection { } } + get entityId() { + return this.startGameData.runtime_entity_id + } + onEncapsulated = (encapsulated, inetAddr) => { const buffer = Buffer.from(encapsulated.buffer) this.handle(buffer) @@ -116,7 +121,7 @@ class Client extends Connection { if (this.status === ClientStatus.Initializing && this.options.autoInitPlayer === true) { if (statusPacket.status === 'player_spawn') { this.status = ClientStatus.Initialized - this.write('set_local_player_as_initialized', { runtime_entity_id: this.startGameData.runtime_entity_id }) + this.write('set_local_player_as_initialized', { runtime_entity_id: this.entityId }) this.emit('spawn') } } diff --git a/test/vanilla.js b/test/vanilla.js index 1088396..4f37d6b 100644 --- a/test/vanilla.js +++ b/test/vanilla.js @@ -2,6 +2,7 @@ const vanillaServer = require('../tools/startVanillaServer') const { Client } = require('../src/client') const { waitFor } = require('../src/datatypes/util') +const { ChunkColumn, Version } = require('bedrock-provider') async function test (version) { // Start the server, wait for it to accept clients, throws on timeout @@ -42,6 +43,11 @@ async function test (version) { client.queue('tick_sync', { request_time: BigInt(Date.now()), response_time: BigInt(Date.now()) }) }, 200) + client.on('level_chunk', async packet => { // Chunk read test + const cc = new ChunkColumn(Version.v1_4_0, packet.x, packet.z) + await cc.networkDecodeNoCache(packet.payload, packet.sub_chunk_count) + }) + console.log('Awaiting join') client.on('spawn', () => { diff --git a/tools/genPacketDumps.js b/tools/genPacketDumps.js index 33530d8..5add677 100644 --- a/tools/genPacketDumps.js +++ b/tools/genPacketDumps.js @@ -21,7 +21,7 @@ async function dump (version, force) { const random = ((Math.random() * 100) | 0) const port = 19130 + random - const handle = await vanillaServer.startServerAndWait(version || CURRENT_VERSION, 1000 * 120, { 'server-port': port, path: 'bds_' }) + const handle = await vanillaServer.startServerAndWait(version || CURRENT_VERSION, 1000 * 120, { 'server-port': port }) console.log('Started server') const client = new Client({