fix some bugs

This commit is contained in:
Soph :3 2024-05-03 20:46:05 +03:00
parent 59d2db1785
commit 888782c2a9
Signed by: sophie
GPG key ID: EDA5D222A0C270F2

View file

@ -39,18 +39,20 @@ export class Server {
hostname: process.env.HOST || "127.0.0.1", hostname: process.env.HOST || "127.0.0.1",
port: +process.env.PORT!, port: +process.env.PORT!,
socket: { socket: {
data: (socket, data) => { data: async (socket, data) => {
if(socket.data.dataBuffer) { if(socket.data.dataBuffer) {
const newBuffer = Buffer.concat([socket.data.dataBuffer, data]); const newBuffer = Buffer.concat([socket.data.dataBuffer, data]);
socket.data.dataBuffer = newBuffer; socket.data.dataBuffer = newBuffer;
} else { } else {
socket.data.dataBuffer = data; socket.data.dataBuffer = data;
} }
log.debug("Socket", socket.remoteAddress, "has", socket.data.dataBuffer.length, "data for parsing."); log.debug("Socket", socket.remoteAddress, "has", socket.data.dataBuffer.length, "data for parsing.");
if(config.debug) await new Promise(r => setTimeout(r, 100));
const parseBuffer = () => { const parseBuffer = () => {
if(!socket.data.dataBuffer) return; if(!socket.data.dataBuffer) return;
if(socket.data.dataBuffer.length == 0) return;
const packetId = socket.data.dataBuffer.readUint8(0); const packetId = socket.data.dataBuffer.readUint8(0);
const packetLength = this.lengthMap.get(packetId); const packetLength = this.lengthMap.get(packetId);
if(!packetLength) { if(!packetLength) {
@ -59,12 +61,15 @@ export class Server {
}; };
if(socket.data.dataBuffer.byteLength < packetLength) return; if(socket.data.dataBuffer.byteLength < packetLength) return;
this.handlePacket(socket.data.dataBuffer.subarray(1), packetId, socket); this.handlePacket(socket.data.dataBuffer.copyWithin(0, 1, packetLength+1), packetId, socket);
log.debug("Parsed packet", packetId, "with packet length", packetLength) log.debug("Parsed packet", packetId, "with packet length", packetLength)
socket.data.dataBuffer = socket.data.dataBuffer.subarray(packetLength+1); socket.data.dataBuffer = socket.data.dataBuffer.subarray(packetLength+1);
parseBuffer(); parseBuffer();
} }
parseBuffer(); parseBuffer();
}, },
open: (socket) => { open: (socket) => {
@ -318,6 +323,7 @@ export class Server {
} }
if (pluginAnswer.some((e) => e == true)) { if (pluginAnswer.some((e) => e == true)) {
console.log(world.size, position)
PacketDefinitions.setBlock(position, world.getBlock(position), player); PacketDefinitions.setBlock(position, world.getBlock(position), player);
return; return;
} }