Ability to clean up unused loaded temporary game worlds

This commit is contained in:
Marc Baloup 2023-09-03 00:10:05 +02:00
parent 69a4f2fe6f
commit 3ee806c1ea

View File

@ -2,12 +2,14 @@ package fr.pandacube.lib.paper.util;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.function.Consumer; import java.util.function.Consumer;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.World.Environment; import org.bukkit.World.Environment;
import org.bukkit.WorldCreator; import org.bukkit.WorldCreator;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import fr.pandacube.lib.util.BiMap; import fr.pandacube.lib.util.BiMap;
@ -52,10 +54,23 @@ public class GameWorldUtils implements Listener {
boolean ret = Bukkit.unloadWorld(rem, false); boolean ret = Bukkit.unloadWorld(rem, false);
if (ret) if (ret)
FileUtils.delete(new File(Bukkit.getWorldContainer(), copiedName)); FileUtils.delete(new File(Bukkit.getWorldContainer(), copiedName));
else
Log.warning("Unable to unload game world " + copiedName + " for some reason.");
return ret; return ret;
} }
return true; 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);
}
}
}