mc-beta-server/server/packets/MapChunk.ts

79 lines
2 KiB
TypeScript
Raw Permalink Normal View History

2023-06-22 12:43:28 +01:00
import { createWriter, IReader, Endian } from "bufferstuff";
2023-04-08 20:52:47 +01:00
import { deflate } from "zlib";
import Chunk from "../Chunk";
import IPacket from "./IPacket";
import Packet from "../enums/Packet";
2023-04-08 20:52:47 +01:00
export default class PacketMapChunk implements IPacket {
2023-04-10 21:52:30 +01:00
public packetId = Packet.MapChunk;
2023-04-08 20:52:47 +01:00
public x:number;
public y:number;
public z:number;
public sizeX:number;
public sizeY:number;
public sizeZ:number;
public chunk:Chunk;
public constructor(x:number, y:number, z:number, sizeX:number, sizeY:number, sizeZ:number, chunk:Chunk) {
this.x = x;
this.y = y;
this.z = z;
this.sizeX = sizeX;
this.sizeY = sizeY;
this.sizeZ = sizeZ;
this.chunk = chunk;
}
public readData(reader:IReader) {
2023-04-09 04:51:30 +01:00
// TODO: Implement MapChunk reading
reader.readBool();
2023-04-08 20:52:47 +01:00
return this;
}
public writeData() {
return new Promise<Buffer>((resolve, reject) => {
2023-05-02 08:50:49 +01:00
// TODO: Use block and sky nibble array buffers
2023-06-19 18:29:16 +01:00
/*const fakeLighting = createWriter(Endian.BE, 16384);
2023-05-02 08:50:49 +01:00
for (let i = 0; i < 16384; i++) {
fakeLighting.writeUByte(0xFF);
2023-06-19 18:29:16 +01:00
}*/
2023-04-08 20:52:47 +01:00
2023-08-20 01:17:37 +01:00
const data = createWriter(Endian.BE)
// Write Chunk Blocks
.writeBuffer(this.chunk.getBlockBuffer())
// Write Chunk Blocks Metadata
.writeBuffer(this.chunk.getMetadataBuffer())
// Write Chunk Block Light
.writeBuffer(this.chunk.blockLight.toBuffer())
// Write Chunk Sky Light
.writeBuffer(this.chunk.skyLight.toBuffer());
2023-04-08 20:52:47 +01:00
2023-05-02 08:50:49 +01:00
deflate(data.toBuffer(), (err, data) => {
2023-04-08 20:52:47 +01:00
if (err) {
return reject(err);
}
2023-08-20 01:17:37 +01:00
resolve(createWriter(Endian.BE, 18)
// Write PacketID
.writeUByte(this.packetId)
// Write Chunk X
.writeInt(this.x << 4)
// Write Chunk Y
.writeShort(this.y)
// Write Chunk Z
.writeInt(this.z << 4)
// Write Chunk Size X
.writeUByte(this.sizeX)
// Write Chunk Size Y
.writeUByte(this.sizeY)
// Write Chunk Size Z
.writeUByte(this.sizeZ)
// Write Compressed Chunk Data Length
.writeInt(data.length)
// Write Compressed Chunk Data
.writeBuffer(data).toBuffer());
2023-04-08 20:52:47 +01:00
});
});
}
}