From 2a11a56b2cc6b9cc8d14f833018bbf3ccd0bad4d Mon Sep 17 00:00:00 2001 From: ForestOfLight Date: Thu, 19 Dec 2024 09:15:45 -0700 Subject: [PATCH] simPlayers now log out when they die. removed respawn command --- scripts/classes/GameTestManager.js | 20 ++++++++++++++++++++ scripts/classes/Understudy.js | 2 +- scripts/commands/player.js | 8 ++++---- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/scripts/classes/GameTestManager.js b/scripts/classes/GameTestManager.js index bd85471..9ea3e75 100644 --- a/scripts/classes/GameTestManager.js +++ b/scripts/classes/GameTestManager.js @@ -18,6 +18,7 @@ class GameTestManager { throw new Error(`[Understudy] GameTestManager has already been started`); gametest.register(extension.name, this.testName, (test) => { this.test = test; + this.subscribeToEvents(); this.startPlayerLoop(); }).maxTicks(TEST_MAX_TICKS).structureName(`${extension.name}:${this.testName}`); this.placeGametestStructure(); @@ -53,6 +54,25 @@ class GameTestManager { }, 2); } + static subscribeToEvents() { + world.afterEvents.entityDie.subscribe((event) => { + if (event.deadEntity.typeId === 'minecraft:player') { + const player = UnderstudyManager.getPlayer(event.deadEntity?.name); + if (player !== undefined) { + this.leaveAction(player); + UnderstudyManager.removePlayer(player); + } + } + }); + + world.afterEvents.playerGameModeChange.subscribe((event) => { + const player = UnderstudyManager.getPlayer(event.player?.name); + if (player !== undefined) { + player.savePlayerInfo(); + } + }); + } + static startPlayerLoop() { system.runInterval(() => { if (!this.#startupComplete) return; diff --git a/scripts/classes/Understudy.js b/scripts/classes/Understudy.js index 1a2d011..73ea386 100644 --- a/scripts/classes/Understudy.js +++ b/scripts/classes/Understudy.js @@ -53,6 +53,7 @@ class Understudy { }; world.setDynamicProperty(`${this.name}:playerinfo`, JSON.stringify(dynamicInfo)); this.saveItems(); + console.warn(`[Understudy] Player ${this.name} info saved`); } loadPlayerInfo() { @@ -187,7 +188,6 @@ class Understudy { gameMode: gameMode }; this.nextActions.push(actionData); - this.savePlayerInfo(actionData); } leave() { diff --git a/scripts/commands/player.js b/scripts/commands/player.js index e13343c..726a3a2 100644 --- a/scripts/commands/player.js +++ b/scripts/commands/player.js @@ -21,7 +21,7 @@ const commandPlayerCommand = new Command({ { usage: `player join`, description: `Make a new player join at your location.` }, { usage: `player leave`, description: `Make a player leave the game.` }, { usage: `player rejoin`, description: `Make a player rejoin at its last location.` }, - { usage: `player respawn`, description: `Make a player respawn after dying.` }, + // { usage: `player respawn`, description: `Make a player respawn after dying.` }, { usage: `player tp`, description: `Make a player teleport to you.` }, { usage: `player look [up/down/north/south/east/west/block/entity/me/x y z/pitch yaw]`, description: `Make a player look in specified directions.` }, { usage: `player move [forward/back/left/right/block/entity/me/x y z]`, description: `Make a player move in specified directions.` }, @@ -75,9 +75,9 @@ function playerCommand(sender, args) { case 'rejoin': rejoinAction(sender, name); break; - case 'respawn': - respawnAction(sender, name); - break; + // case 'respawn': + // respawnAction(sender, name); + // break; case 'tp': tpAction(sender, name); break;