Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add v0 support to the decoder #230

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"prismarine-block": "^1.14.1",
"prismarine-nbt": "^2.2.1",
"prismarine-registry": "^1.1.0",
"minecraft-data": "^3.43.0",
"smart-buffer": "^4.1.0",
"uint4": "^0.1.2",
"vec3": "^0.1.3",
Expand Down
39 changes: 39 additions & 0 deletions src/bedrock/1.3/SubChunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { getChecksum } = require('../common/util')
const neededBits = require('../../pc/common/neededBits')
const Stream = require('../common/Stream')
const nbt = require('prismarine-nbt')
const legacyBlockIdMap = require('minecraft-data').legacy.bedrock.blocks

class SubChunk {
constructor (registry, Block, options = {}) {
Expand All @@ -12,6 +13,23 @@ class SubChunk {
throw new Error('registry is required')
}
this.Block = Block
this.legacyBlockIdToNewStateId = Object.fromEntries(Object.entries(legacyBlockIdMap).map(([k, v]) => [k, ((str) => {
str = str.substring(10)
const name = str.split('[', 1)[0]
if (!registry.blocksByName[name]) {
return null
}
let propertiesArr = []
if (str.slice(name.length + 1, -1) !== '') {
propertiesArr = str.slice(name.length + 1, -1).split(',')
}
const properties = Object.fromEntries(propertiesArr.map(property => {
const [key, value] = property.split('=')
const intValue = parseInt(value)
return [key, isNaN(intValue) ? { true: 1, false: 0 }[value] ?? value : intValue]
}))
return this.Block.fromProperties(name, properties, 0)?.stateId
})(v)]))
this.y = options.y
this.palette = options.palette || []
this.blocks = []
Expand Down Expand Up @@ -47,6 +65,27 @@ class SubChunk {
let storageCount = 1

switch (this.subChunkVersion) {
case 0: {
const blockIds = stream.readBuffer(4096)
const metas = stream.readBuffer(2048)

for (let x = 0; x < 16; x++) {
for (let y = 0; y < 16; y++) {
for (let z = 0; z < 16; z++) {
const index = (x << 8) + (z << 4) + y
const id = blockIds[index]
const meta = metas[index >> 1] >> (index & 1) * 4 & 15

const stateId = this.legacyBlockIdToNewStateId[id + ':' + meta] ??
this.legacyBlockIdToNewStateId[id + ':0'] ??
this.legacyBlockIdToNewStateId['248:0'] // minecraft:info_update
this.setBlockStateId(0, x, y, z, stateId)
}
}
}
this.subChunkVersion = 8
return
}
case 1:
// This is a old SubChunk format that only has one layer - no need to read storage count
// But when re-encoding, we want to use v8 to not loose data
Expand Down
29 changes: 16 additions & 13 deletions test/bedrock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,19 +175,22 @@ for (const version of versions) {

describe('special bedrock tests', () => {
// Test for some special cases that are not covered by the normal tests
it('can load v1 subchunks in level_chunk', async () => {
// SubChunk v1 is only sent by 3rd party servers
const ChunkColumn = require('prismarine-chunk')('bedrock_1.17.10')
const packet = require('./bedrock_1.17.10/subchunkv1.json').params
const column = new ChunkColumn({ x: packet.x, z: packet.z })
const payload = Buffer.from(packet.payload)
await column.networkDecodeNoCache(payload, packet.sub_chunk_count)
await column.networkEncodeNoCache()
const blocks = column.getBlocks()
assert(blocks.length > 0, 'No blocks in column')
console.log('Unique blocks', blocks.map(e => e.name))
// No error is OK
})
const subchunkVersions = { v0: './bedrock_1.17.10/subchunkv0.json', v1: './bedrock_1.17.10/subchunkv1.json' }
for (const [subchunkVersion, path] of Object.entries(subchunkVersions)) {
it(`can load ${subchunkVersion} subchunks in level_chunk`, async () => {
// SubChunk v1 is only sent by 3rd party servers
const ChunkColumn = require('prismarine-chunk')('bedrock_1.17.10')
const packet = require(path).params
const column = new ChunkColumn({ x: packet.x, z: packet.z })
const payload = Buffer.from(packet.payload)
await column.networkDecodeNoCache(payload, packet.sub_chunk_count)
await column.networkEncodeNoCache()
const blocks = column.getBlocks()
assert(blocks.length > 0, 'No blocks in column')
console.log('Unique blocks', blocks.map(e => e.name))
// No error is OK
})
}
})

const dbdiff = (last, now) => {
Expand Down
1 change: 1 addition & 0 deletions test/bedrock_1.17.10/subchunkv0.json

Large diffs are not rendered by default.

Loading