Fix chunk loading

This commit is contained in:
Holly Stubbs 2023-04-12 23:37:51 +01:00
parent b474d35775
commit f8a52dfc98
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E
2 changed files with 13 additions and 2 deletions

View file

@ -13,6 +13,15 @@ export class Reader {
return value;
}
public readUint8Array(bytes:number) {
const croppedBuffer = this.readBuffer(bytes);
const newArray = new Uint8Array(croppedBuffer.length);
for (let i = 0; i < croppedBuffer.length; i++) {
newArray[i] = croppedBuffer[i];
}
return newArray;
}
public readByte() {
const value = this.buffer.readInt8(this.offset);
this.offset++;

View file

@ -96,7 +96,9 @@ export class WorldSaveManager {
chunkFileWriter.writeUByte(128); // Chunk Y
chunkFileWriter.writeUByte(16); // Chunk Z
const chunkData = new Writer().writeBuffer(Buffer.from(chunk.getData())).writeBuffer(chunk.getMetadataBuffer()).toBuffer();
const chunkData = new Writer()
.writeBuffer(Buffer.from(chunk.getData()))
.writeBuffer(chunk.getMetadataBuffer()).toBuffer();
if (saveType === SaveCompressionType.NONE) {
chunkFileWriter.writeInt(chunkData.length); // Data length
@ -159,7 +161,7 @@ export class WorldSaveManager {
const contentLength = chunkFileReader.readInt();
if (saveCompressionType === SaveCompressionType.NONE) {
const chunkData = new Reader(chunkFileReader.readBuffer(contentLength));
const chunk = new Chunk(world, x, z, chunkData.readBuffer(totalByteSize).buffer, chunkData.readBuffer(totalByteSize / 2).buffer);
const chunk = new Chunk(world, x, z, chunkData.readUint8Array(totalByteSize), chunkData.readUint8Array(totalByteSize / 2));
resolve(chunk);
} else if (saveCompressionType === SaveCompressionType.DEFLATE) {
inflate(chunkFileReader.readBuffer(contentLength), (err, data) => {