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

67 lines
1.9 KiB
TypeScript
Raw Normal View History

2023-04-08 20:52:47 +01:00
import { deflate } from "zlib";
import { Reader, Writer } from "../../bufferStuff";
2023-04-10 21:52:30 +01:00
import { Packet } from "../enums/Packet";
2023-04-08 20:52:47 +01:00
import { IPacket } from "./IPacket";
import { Chunk } from "../Chunk";
export 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:Reader) {
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) => {
const blocks = new Writer(32768);
const lighting = new Writer(32768);
let blockMeta = false;
for (let x = 0; x < 16; x++) {
for (let z = 0; z < 16; z++) {
for (let y = 0; y < 128; y++) {
blocks.writeUByte(this.chunk.getBlockId(x, y, z));
if (blockMeta) {
// Light level 15 for 2 blocks (1111 1111)
lighting.writeUByte(0xff); // TODO: Lighting (Client seems to do it's own (when a block update happens) so it's not top priority)
lighting.writeUByte(0xff);
}
// Hack for nibble stuff
blockMeta = !blockMeta;
}
}
}
// Write meta and lighting data into block buffer for compression
2023-04-11 07:47:56 +01:00
blocks.writeBuffer(this.chunk.getMetadataBuffer()).writeBuffer(lighting.toBuffer());
2023-04-08 20:52:47 +01:00
deflate(blocks.toBuffer(), (err, data) => {
if (err) {
return reject(err);
}
2023-04-09 04:19:10 +01:00
resolve(new Writer(18).writeUByte(this.packetId).writeInt(this.x << 4).writeShort(this.y).writeInt(this.z << 4).writeUByte(this.sizeX).writeUByte(this.sizeY).writeUByte(this.sizeZ).writeInt(data.length).writeBuffer(data).toBuffer());
2023-04-08 20:52:47 +01:00
});
});
}
}