PrismarineJS / prismarine-chunk

A class to hold chunk data for Minecraft
MIT License
61 stars 59 forks source link

fix: toJson / fromJson didn't use maxBitsPerBlock #238

Closed zardoy closed 5 months ago

zardoy commented 5 months ago

Fixes the following code:

import PChunk from 'prismarine-chunk'
import { Vec3 } from 'vec3'

const Chunk = PChunk('1.18.1')
const chunk = new Chunk(undefined)

const chunk2 = Chunk.fromJson(chunk.toJson()) as any

for (let i = 0; i < 4096; i++) {
    chunk2.setBlockStateId(new Vec3(0, 0, 0), i) // Decides to switch to Direct pallete at some point
    const blockStateId = chunk2.getBlockStateId(new Vec3(0, 0, 0))
    if (blockStateId !== i) throw new Error(`Expected ${i} but got ${blockStateId}`) // Expected 256 but got 0
}

Also what about indirect pallete I don't really know what is the correct behavior but it just feels like always adding a new block into pallete on the block replace is not right (since the block doesn't exist in chunk anymore).

extremeheat commented 5 months ago

Can you add a test?

zardoy commented 5 months ago

Can you add a test?

Of course, just wanted initial feedback. Also, is it a bug that Pallete doesn't get cleared? I feel its not optimized...