Fix error, make removeUser async, re-enable world saving

This commit is contained in:
yourfriend 2022-07-10 18:19:17 +03:00
parent 41a19c26a6
commit 5e3306276a
No known key found for this signature in database
GPG key ID: C28FFD8607DAC4DE
3 changed files with 19 additions and 10 deletions

View file

@ -37,9 +37,9 @@ export class Player {
} }
async writeToSocket(ar: Uint8Array) { async writeToSocket(ar: Uint8Array) {
await this.socket.write(ar).catch((e) => { await this.socket.write(ar).catch(async (e) => {
log.critical(e); log.critical(e);
this.server.removeUser(this.socket); await this.server.removeUser(this.socket);
}); });
} }
@ -75,7 +75,5 @@ export class Player {
); );
this.message("You have been moved."); this.message("You have been moved.");
//await world.save(); TODO: this causes way too many issues
} }
} }

View file

@ -136,7 +136,7 @@ export class Server {
} }
} }
} }
removeUser(conn: Deno.Conn) { async removeUser(conn: Deno.Conn) {
const player = this.players.find((e) => e.socket == conn); const player = this.players.find((e) => e.socket == conn);
if (!player) return; if (!player) return;
@ -151,6 +151,8 @@ export class Server {
this.broadcast(`${player.username} has &cleft`); this.broadcast(`${player.username} has &cleft`);
await this.worlds.find(e => e.name == player.world)!.save();
this.broadcastPacket( this.broadcastPacket(
(e) => PacketDefinitions.despawn(player.id, e), (e) => PacketDefinitions.despawn(player.id, e),
player, player,
@ -314,7 +316,7 @@ export class Server {
try { try {
packetIDReadAttempt = await connection.read(packetID); packetIDReadAttempt = await connection.read(packetID);
} catch { } catch {
this.removeUser(connection); // TODO: add a reason to this await this.removeUser(connection); // TODO: add a reason to this
break; break;
} }
@ -323,7 +325,7 @@ export class Server {
if (!packetLength) { if (!packetLength) {
log.critical("Unknown Packet: " + packetID[0]); log.critical("Unknown Packet: " + packetID[0]);
this.removeUser(connection); // TODO: add a reason to this await this.removeUser(connection); // TODO: add a reason to this
break; break;
} }
@ -333,21 +335,27 @@ export class Server {
try { try {
packetReadAttempt = await connection.read(rawPacket); packetReadAttempt = await connection.read(rawPacket);
} catch { } catch {
this.removeUser(connection); // TODO: add a reason to this await this.removeUser(connection); // TODO: add a reason to this
break; break;
} }
let fullRead = packetReadAttempt!; let fullRead = packetReadAttempt!;
while (fullRead < packetLength) { while (fullRead < packetLength) {
const halfPacket = new Uint8Array(packetLength - fullRead); const halfPacket = new Uint8Array(packetLength - fullRead);
rawPacket = new Uint8Array([...rawPacket, ...halfPacket]); rawPacket = new Uint8Array([...rawPacket, ...halfPacket]);
fullRead += (await connection.read(halfPacket))!; try {
fullRead += (await connection.read(halfPacket))!;
} catch {
await this.removeUser(connection); // TODO: add a reason to this
break;
}
} }
this.handlePacket(rawPacket, packetID[0], connection); this.handlePacket(rawPacket, packetID[0], connection);
} else { } else {
this.removeUser(connection); await this.removeUser(connection);
break; break;
} }
} }

View file

@ -26,9 +26,12 @@ export default class CommandPlugin extends Plugin {
}); });
this.on("command", async (command, player, args) => { this.on("command", async (command, player, args) => {
if (command == "g") { if (command == "g") {
await server.worlds.find(e => e.name == player.world)!.save();
const requestedWorld = server.worlds.find((e) => const requestedWorld = server.worlds.find((e) =>
e.name.toLowerCase() == args.join(" ").toLowerCase() e.name.toLowerCase() == args.join(" ").toLowerCase()
); );
if (requestedWorld) { if (requestedWorld) {
player.toWorld(requestedWorld); player.toWorld(requestedWorld);
} else { } else {