From 61f710863c52d3cb5aeeaab7600a060af49a5024 Mon Sep 17 00:00:00 2001 From: makamys Date: Sat, 8 May 2021 23:24:07 +0200 Subject: Unload empty regions --- src/main/java/makamys/lodmod/renderer/LODRegion.java | 19 ++++++++++++++++++- .../java/makamys/lodmod/renderer/LODRenderer.java | 12 ++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) (limited to 'src/main/java/makamys/lodmod/renderer') diff --git a/src/main/java/makamys/lodmod/renderer/LODRegion.java b/src/main/java/makamys/lodmod/renderer/LODRegion.java index 0dc49ea..2aff7a6 100644 --- a/src/main/java/makamys/lodmod/renderer/LODRegion.java +++ b/src/main/java/makamys/lodmod/renderer/LODRegion.java @@ -122,15 +122,32 @@ public class LODRegion { return null; } - public void tick(Entity player) { + public boolean tick(Entity player) { + int visibleChunks = 0; for(int i = 0; i < 32; i++) { for(int j = 0; j < 32; j++) { LODChunk chunk = data[i][j]; if(chunk != null) { chunk.tick(player); + if(chunk.visible) { + visibleChunks++; + } } } } + return visibleChunks > 0; + } + + public void destroy(Path saveDir) { + save(saveDir); + for(int i = 0; i < 32; i++) { + for(int j = 0; j < 32; j++) { + LODChunk chunk = data[i][j]; + if(chunk != null) { + LODMod.renderer.setVisible(chunk, false); + } + } + } } } diff --git a/src/main/java/makamys/lodmod/renderer/LODRenderer.java b/src/main/java/makamys/lodmod/renderer/LODRenderer.java index 91c0aee..6aac74a 100644 --- a/src/main/java/makamys/lodmod/renderer/LODRenderer.java +++ b/src/main/java/makamys/lodmod/renderer/LODRenderer.java @@ -130,8 +130,16 @@ public class LODRenderer { lastSortX = player.posX; lastSortY = player.posY; lastSortZ = player.posZ; - - loadedRegionsMap.forEach((k, v) -> v.tick(player)); + for(Iterator it = loadedRegionsMap.keySet().iterator(); it.hasNext();) { + ChunkCoordIntPair k = it.next(); + LODRegion v = loadedRegionsMap.get(k); + + if(!v.tick(player)) { + System.out.println("unloading " + v); + v.destroy(getSaveDir()); + it.remove(); + } + } } } } -- cgit