From dd8d1752ed90985a4c09d1a274172f94393ab2b8 Mon Sep 17 00:00:00 2001 From: Melledy <52122272+Melledy@users.noreply.github.com> Date: Mon, 18 Apr 2022 23:00:12 -0700 Subject: [PATCH] Deregister empty scenes if a player leaves them --- src/main/java/emu/grasscutter/game/GenshinScene.java | 6 ++++++ src/main/java/emu/grasscutter/game/World.java | 10 +++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/emu/grasscutter/game/GenshinScene.java b/src/main/java/emu/grasscutter/game/GenshinScene.java index fc25546b9..668902d87 100644 --- a/src/main/java/emu/grasscutter/game/GenshinScene.java +++ b/src/main/java/emu/grasscutter/game/GenshinScene.java @@ -117,6 +117,7 @@ public class GenshinScene { } public void removePlayer(GenshinPlayer player) { + // Remove player from scene getPlayers().remove(player); player.setScene(null); @@ -127,6 +128,11 @@ public class GenshinScene { for (EntityGadget gadget : player.getTeamManager().getGadgets()) { this.removeEntity(gadget); } + + // Deregister scene if not in use + if (this.getEntities().size() <= 0) { + this.getWorld().deregisterScene(this); + } } private void setupPlayerAvatars(GenshinPlayer player) { diff --git a/src/main/java/emu/grasscutter/game/World.java b/src/main/java/emu/grasscutter/game/World.java index b3fb3e150..802ab6ba0 100644 --- a/src/main/java/emu/grasscutter/game/World.java +++ b/src/main/java/emu/grasscutter/game/World.java @@ -111,7 +111,7 @@ public class World implements Iterable { SceneData sceneData = GenshinData.getSceneDataMap().get(sceneId); if (sceneData != null) { scene = new GenshinScene(this, sceneData); - this.getScenes().put(sceneId, scene); + this.registerScene(scene); return scene; } @@ -198,6 +198,14 @@ public class World implements Iterable { } } + public void registerScene(GenshinScene scene) { + this.getScenes().put(scene.getId(), scene); + } + + public void deregisterScene(GenshinScene scene) { + this.getScenes().remove(scene.getId()); + } + public boolean transferPlayerToScene(GenshinPlayer player, int sceneId, Position pos) { if (player.getScene().getId() == sceneId || GenshinData.getSceneDataMap().get(sceneId) == null) { return false;