From 3ee806c1ea7c745cf97868c0b594db9513f108c1 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Sun, 3 Sep 2023 00:10:05 +0200 Subject: [PATCH] Ability to clean up unused loaded temporary game worlds --- .../pandacube/lib/paper/util/GameWorldUtils.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/util/GameWorldUtils.java b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/util/GameWorldUtils.java index f688012..cd40d22 100644 --- a/pandalib-paper/src/main/java/fr/pandacube/lib/paper/util/GameWorldUtils.java +++ b/pandalib-paper/src/main/java/fr/pandacube/lib/paper/util/GameWorldUtils.java @@ -2,12 +2,14 @@ package fr.pandacube.lib.paper.util; import java.io.File; import java.io.IOException; +import java.util.ArrayList; import java.util.function.Consumer; import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.World.Environment; import org.bukkit.WorldCreator; +import org.bukkit.entity.Player; import org.bukkit.event.Listener; import fr.pandacube.lib.util.BiMap; @@ -52,10 +54,23 @@ public class GameWorldUtils implements Listener { boolean ret = Bukkit.unloadWorld(rem, false); if (ret) FileUtils.delete(new File(Bukkit.getWorldContainer(), copiedName)); + else + Log.warning("Unable to unload game world " + copiedName + " for some reason."); return ret; } return true; } + + + + public static void unloadUnusedGameWorlds() { + for (String world : new ArrayList<>(gameWorld.keySet())) { + World rem = gameWorld.get(world); + if (rem.getPlayers().stream().noneMatch(Player::isOnline)) { + unloadGameWorld(world); + } + } + }