Fixes for plax

This commit is contained in:
Daniel Mills 2020-12-28 12:13:47 -05:00
parent a9410d92a2
commit 5994cc66b4
4 changed files with 53 additions and 25 deletions

View File

@ -33,8 +33,9 @@ public class HunkRegionSlice<T>
this.lastUse = new KMap<>(); this.lastUse = new KMap<>();
} }
public void cleanup(long t) public int cleanup(long t)
{ {
int v = 0;
if(loadedChunks.size() != lastUse.size()) if(loadedChunks.size() != lastUse.size())
{ {
Iris.warn("Incorrect chunk use counts in " + key); Iris.warn("Incorrect chunk use counts in " + key);
@ -50,11 +51,15 @@ public class HunkRegionSlice<T>
for(ChunkPosition i : lastUse.k()) for(ChunkPosition i : lastUse.k())
{ {
if(M.ms() - lastUse.get(i) > t) Long l = lastUse.get(i);
if(l == null || M.ms() - l > t)
{ {
v++;
unload(i.getX(), i.getZ()); unload(i.getX(), i.getZ());
} }
} }
return v;
} }
public void clear() public void clear()
@ -109,16 +114,19 @@ public class HunkRegionSlice<T>
compound.getValue().put(key(x, z), hunk.writeByteArrayTag(adapter, key(x, z))); compound.getValue().put(key(x, z), hunk.writeByteArrayTag(adapter, key(x, z)));
} }
public synchronized void unloadAll() public synchronized int unloadAll()
{ {
int v = 0;
for(ChunkPosition i : loadedChunks.k()) for(ChunkPosition i : loadedChunks.k())
{ {
unload(i.getX(), i.getZ()); unload(i.getX(), i.getZ());
v++;
} }
save.clear(); save.clear();
loadedChunks.clear(); loadedChunks.clear();
lastUse.clear(); lastUse.clear();
return v;
} }
public synchronized void save(Hunk<T> region, int x, int z) public synchronized void save(Hunk<T> region, int x, int z)

View File

@ -147,12 +147,12 @@ public class ParallaxRegion extends HunkRegion
super.save(); super.save();
} }
public void unload() public int unload()
{ {
blockSlice.unloadAll();
objectSlice.unloadAll();
updateSlice.unloadAll();
unloadMetaHunk(); unloadMetaHunk();
return blockSlice.unloadAll()+
objectSlice.unloadAll()+
updateSlice.unloadAll();
} }
public HunkRegionSlice<BlockData> getBlockSlice() { public HunkRegionSlice<BlockData> getBlockSlice() {
@ -170,9 +170,9 @@ public class ParallaxRegion extends HunkRegion
return updateSlice; return updateSlice;
} }
public void cleanup(long c) { public int cleanup(long c) {
blockSlice.cleanup(c); return blockSlice.cleanup(c) +
objectSlice.cleanup(c); objectSlice.cleanup(c) +
updateSlice.cleanup(c); updateSlice.cleanup(c);
} }

View File

@ -89,10 +89,10 @@ public class ParallaxWorld implements ParallaxAccess
} }
} }
public void unload(int x, int z) public int unload(int x, int z)
{ {
long key = key(x, z); long key = key(x, z);
int v = 0;
if(isLoaded(x, z)) if(isLoaded(x, z))
{ {
if(save.contains(key)) if(save.contains(key))
@ -101,8 +101,15 @@ public class ParallaxWorld implements ParallaxAccess
save.remove(key); save.remove(key);
} }
loadedRegions.remove(key).unload(); v += loadedRegions.remove(key).unload();
} }
else
{
Iris.warn("Cant unload region " + x + " " + z + " because it's not loaded.");
}
return v;
} }
public ParallaxRegion load(int x, int z) public ParallaxRegion load(int x, int z)
@ -203,21 +210,28 @@ public class ParallaxWorld implements ParallaxAccess
@Override @Override
public void cleanup(long r, long c) { public void cleanup(long r, long c) {
Iris.info("P: c" + Form.f(getChunkCount()) + " / r" + getRegionCount());
J.a(() -> { J.a(() -> {
int rr = 0;
int cc = 0;
for(ParallaxRegion i : loadedRegions.v()) for(ParallaxRegion i : loadedRegions.v())
{ {
if(i.hasBeenIdleLongerThan(r)) if(i.hasBeenIdleLongerThan(r))
{ {
rr++;
unload(i.getX(), i.getZ()); unload(i.getX(), i.getZ());
} }
else else
{ {
i.cleanup(c); cc+= i.cleanup(c);
} }
} }
Iris.info("Unloaded " + rr + " Regions and " + cc + " Chunks");
Iris.info("P: c" + Form.f(getChunkCount()) + " / r" + getRegionCount());
}); });
} }

View File

@ -1,18 +1,13 @@
package com.volmit.iris.util; package com.volmit.iris.util;
import java.util.concurrent.Callable; import com.volmit.iris.Iris;
import java.util.concurrent.ExecutorService; import org.bukkit.Bukkit;
import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.*;
import java.util.concurrent.ThreadFactory;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
import org.bukkit.Bukkit;
import com.volmit.iris.Iris;
public class J public class J
{ {
private static int tid = 0; private static int tid = 0;
@ -56,7 +51,18 @@ public class J
public static void a(Runnable a) public static void a(Runnable a)
{ {
e.submit(a); e.submit(() -> {
try
{
a.run();
}
catch(Throwable e)
{
System.out.println("Failed to run async task");
e.printStackTrace();
}
});
} }
public static <T> Future<T> a(Callable<T> a) public static <T> Future<T> a(Callable<T> a)