From 5e3306276a8663f14a704abd9c0f68042d0404b3 Mon Sep 17 00:00:00 2001 From: yourfriend Date: Sun, 10 Jul 2022 18:19:17 +0300 Subject: [PATCH] Fix error, make removeUser async, re-enable world saving --- classes/Player.ts | 6 ++---- classes/Server.ts | 20 ++++++++++++++------ plugins/world.ts | 3 +++ 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/classes/Player.ts b/classes/Player.ts index 75f9562..c8200e8 100644 --- a/classes/Player.ts +++ b/classes/Player.ts @@ -37,9 +37,9 @@ export class Player { } async writeToSocket(ar: Uint8Array) { - await this.socket.write(ar).catch((e) => { + await this.socket.write(ar).catch(async (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."); - - //await world.save(); TODO: this causes way too many issues } } diff --git a/classes/Server.ts b/classes/Server.ts index 093464f..3bc3308 100644 --- a/classes/Server.ts +++ b/classes/Server.ts @@ -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); if (!player) return; @@ -151,6 +151,8 @@ export class Server { this.broadcast(`${player.username} has &cleft`); + await this.worlds.find(e => e.name == player.world)!.save(); + this.broadcastPacket( (e) => PacketDefinitions.despawn(player.id, e), player, @@ -314,7 +316,7 @@ export class Server { try { packetIDReadAttempt = await connection.read(packetID); } catch { - this.removeUser(connection); // TODO: add a reason to this + await this.removeUser(connection); // TODO: add a reason to this break; } @@ -323,7 +325,7 @@ export class Server { if (!packetLength) { 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; } @@ -333,21 +335,27 @@ export class Server { try { packetReadAttempt = await connection.read(rawPacket); } catch { - this.removeUser(connection); // TODO: add a reason to this + await this.removeUser(connection); // TODO: add a reason to this break; } + let fullRead = packetReadAttempt!; while (fullRead < packetLength) { const halfPacket = new Uint8Array(packetLength - fullRead); 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); } else { - this.removeUser(connection); + await this.removeUser(connection); break; } } diff --git a/plugins/world.ts b/plugins/world.ts index 1d396cd..e0878a6 100644 --- a/plugins/world.ts +++ b/plugins/world.ts @@ -26,9 +26,12 @@ export default class CommandPlugin extends Plugin { }); this.on("command", async (command, player, args) => { if (command == "g") { + await server.worlds.find(e => e.name == player.world)!.save(); + const requestedWorld = server.worlds.find((e) => e.name.toLowerCase() == args.join(" ").toLowerCase() ); + if (requestedWorld) { player.toWorld(requestedWorld); } else {