Clean and finish

This commit is contained in:
mastermc05 2022-03-31 16:18:02 +03:00
parent 790f1f4966
commit da05fc4675
2 changed files with 25 additions and 10 deletions

View File

@ -700,13 +700,13 @@ public abstract class GenericMapChunkCache extends MapChunkCache {
protected abstract GenericChunk getLoadedChunk(DynmapChunk ch); protected abstract GenericChunk getLoadedChunk(DynmapChunk ch);
// Load generic chunk from unloaded chunk // Load generic chunk from unloaded chunk
protected abstract GenericChunk loadChunk(DynmapChunk ch); protected abstract GenericChunk loadChunk(DynmapChunk ch);
// Load generic chunk from existing and already loaded chunk // Load generic chunk from existing and already loaded chunk async
protected Supplier<GenericChunk> getLoadedChunkAsync(DynmapChunk ch) { protected Supplier<GenericChunk> getLoadedChunkAsync(DynmapChunk ch) {
throw new RuntimeException("Not implemeted"); throw new IllegalStateException("Not implemeted");
} }
// Load generic chunks from unloaded chunk async // Load generic chunks from unloaded chunk async
protected Supplier<GenericChunk> loadChunkAsync(DynmapChunk ch){ protected Supplier<GenericChunk> loadChunkAsync(DynmapChunk ch){
throw new RuntimeException("Not implemeted"); throw new IllegalStateException("Not implemeted");
} }
/** /**
@ -764,8 +764,13 @@ public abstract class GenericMapChunkCache extends MapChunkCache {
} }
return cnt; return cnt;
} }
/**
* Read NBT data from loaded chunks - do not needs to be called from server/world
* Will throw {@link IllegalStateException} if not supporting
*/
public void getLoadedChunksAsync() { public void getLoadedChunksAsync() {
class SimplePair{ //simple pair of the supplier that finishes read async, and a consumer that also finish his work async class SimplePair { //simple pair of the supplier that finishes read async, and a consumer that also finish his work async
final Supplier<GenericChunk> supplier; final Supplier<GenericChunk> supplier;
final BiConsumer<GenericChunk, Long> consumer; final BiConsumer<GenericChunk, Long> consumer;
@ -830,6 +835,9 @@ public abstract class GenericMapChunkCache extends MapChunkCache {
return getLoadedChunks() + readChunks(max_to_load); return getLoadedChunks() + readChunks(max_to_load);
} }
/**
* Prepare the chunks async
*/
public void loadChunksAsync() { public void loadChunksAsync() {
getLoadedChunksAsync(); getLoadedChunksAsync();
readChunksAsync(); readChunksAsync();
@ -916,6 +924,15 @@ public abstract class GenericMapChunkCache extends MapChunkCache {
} }
public void readChunksAsync() { public void readChunksAsync() {
class SimplePair {
private final Supplier<GenericChunk> supplier;
private final DynmapChunk chunk;
SimplePair(DynmapChunk chunk) {
this.chunk = chunk;
this.supplier = loadChunkAsync(chunk);
}
}
if (!dw.isLoaded()) { if (!dw.isLoaded()) {
isempty = true; isempty = true;
unloadChunks(); unloadChunks();
@ -937,14 +954,14 @@ public abstract class GenericMapChunkCache extends MapChunkCache {
try { try {
List<DynmapChunk> cached = new ArrayList<>(); List<DynmapChunk> cached = new ArrayList<>();
List<Map.Entry<Supplier<GenericChunk>, DynmapChunk>> notCached = new ArrayList<>(); List<SimplePair> notCached = new ArrayList<>();
iterator.forEachRemaining(chunks::add); iterator.forEachRemaining(chunks::add);
chunks.stream() chunks.stream()
.filter(chunk -> snaparray[(chunk.x - x_min) + (chunk.z - z_min) * x_dim] == null) .filter(chunk -> snaparray[(chunk.x - x_min) + (chunk.z - z_min) * x_dim] == null)
.forEach(chunk -> { .forEach(chunk -> {
if (cache.getSnapshot(dw.getName(), chunk.x, chunk.z) == null) { if (cache.getSnapshot(dw.getName(), chunk.x, chunk.z) == null) {
notCached.add(new AbstractMap.SimpleEntry<>(loadChunkAsync(chunk), chunk)); notCached.add(new SimplePair(chunk));
} else { } else {
cached.add(chunk); cached.add(chunk);
} }
@ -957,8 +974,8 @@ public abstract class GenericMapChunkCache extends MapChunkCache {
}); });
notCached.forEach(chunkSupplier -> { notCached.forEach(chunkSupplier -> {
long startTime = System.nanoTime(); long startTime = System.nanoTime();
GenericChunk chunk = chunkSupplier.getKey().get(); GenericChunk chunk = chunkSupplier.supplier.get();
DynmapChunk dynmapChunk = chunkSupplier.getValue(); DynmapChunk dynmapChunk = chunkSupplier.chunk;
if (chunk != null) { if (chunk != null) {
// If hidden // If hidden
if (isChunkVisible(dynmapChunk)) { if (isChunkVisible(dynmapChunk)) {

View File

@ -563,7 +563,6 @@ public class DynmapPlugin extends JavaPlugin implements DynmapAPI {
} }
} }
} else { } else {
// synchronized (lock) {
if (prev_tick != cur_tick) { if (prev_tick != cur_tick) {
prev_tick = cur_tick; prev_tick = cur_tick;
cur_tick_starttime = System.nanoTime(); cur_tick_starttime = System.nanoTime();
@ -573,7 +572,6 @@ public class DynmapPlugin extends JavaPlugin implements DynmapAPI {
} else { } else {
cc.loadChunks(Integer.MAX_VALUE); cc.loadChunks(Integer.MAX_VALUE);
} }
// }
} }
} }
/* If cancelled due to world unload return nothing */ /* If cancelled due to world unload return nothing */