client example updates

This commit is contained in:
extremeheat 2021-03-26 04:38:25 -04:00
commit 0bdd071876
4 changed files with 27 additions and 14 deletions

View file

@ -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()

View file

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

View file

@ -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', () => {

View file

@ -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({