Add support for throttling chunk load rate, spreading over ticks

This commit is contained in:
Mike Primm 2011-06-18 19:43:47 -05:00
parent 8b35f4d3b7
commit 2bc9b410a6
8 changed files with 188 additions and 102 deletions

View file

@ -3,6 +3,7 @@ package org.dynmap.utils;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import org.bukkit.World;
import org.bukkit.Chunk;
@ -18,6 +19,9 @@ import org.dynmap.Log;
public class NewMapChunkCache implements MapChunkCache {
private static Method poppreservedchunk = null;
private World w;
private List<DynmapChunk> chunks;
private ListIterator<DynmapChunk> iterator;
private int x_min, x_max, z_min, z_max;
private int x_dim;
private HiddenChunkStyle hidestyle = HiddenChunkStyle.FILL_AIR;
@ -189,16 +193,10 @@ public class NewMapChunkCache implements MapChunkCache {
*/
public NewMapChunkCache() {
}
/**
* Create chunk cache container
* @param w - world
* @param x_min - minimum chunk x coordinate
* @param z_min - minimum chunk z coordinate
* @param x_max - maximum chunk x coordinate
* @param z_max - maximum chunk z coordinate
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public void loadChunks(World w, DynmapChunk[] chunks) {
public void setChunks(World w, List<DynmapChunk> chunks) {
this.w = w;
this.chunks = chunks;
if(poppreservedchunk == null) {
/* Get CraftWorld.popPreservedChunk(x,z) - reduces memory bloat from map traversals (optional) */
try {
@ -209,7 +207,7 @@ public class NewMapChunkCache implements MapChunkCache {
}
}
/* Compute range */
if(chunks.length == 0) {
if(chunks.size() == 0) {
this.x_min = 0;
this.x_max = 0;
this.z_min = 0;
@ -217,24 +215,32 @@ public class NewMapChunkCache implements MapChunkCache {
x_dim = 1;
}
else {
x_min = x_max = chunks[0].x;
z_min = z_max = chunks[0].z;
for(int i = 1; i < chunks.length; i++) {
if(chunks[i].x > x_max)
x_max = chunks[i].x;
if(chunks[i].x < x_min)
x_min = chunks[i].x;
if(chunks[i].z > z_max)
z_max = chunks[i].z;
if(chunks[i].z < z_min)
z_min = chunks[i].z;
x_min = x_max = chunks.get(0).x;
z_min = z_max = chunks.get(0).z;
for(DynmapChunk c : chunks) {
if(c.x > x_max)
x_max = c.x;
if(c.x < x_min)
x_min = c.x;
if(c.z > z_max)
z_max = c.z;
if(c.z < z_min)
z_min = c.z;
}
x_dim = x_max - x_min + 1;
}
snaparray = new ChunkSnapshot[x_dim * (z_max-z_min+1)];
}
public int loadChunks(int max_to_load) {
int cnt = 0;
if(iterator == null)
iterator = chunks.listIterator();
// Load the required chunks.
for (DynmapChunk chunk : chunks) {
while((cnt < max_to_load) && iterator.hasNext()) {
DynmapChunk chunk = iterator.next();
boolean vis = true;
if(visible_limits != null) {
vis = false;
@ -286,12 +292,24 @@ public class NewMapChunkCache implements MapChunkCache {
Log.severe("Cannot pop preserved chunk - " + x.toString());
}
}
cnt++;
}
/* Fill missing chunks with empty dummy chunk */
for(int i = 0; i < snaparray.length; i++) {
if(snaparray[i] == null)
snaparray[i] = EMPTY;
if(iterator.hasNext() == false) { /* If we're done */
/* Fill missing chunks with empty dummy chunk */
for(int i = 0; i < snaparray.length; i++) {
if(snaparray[i] == null)
snaparray[i] = EMPTY;
}
}
return cnt;
}
/**
* Test if done loading
*/
public boolean isDoneLoading() {
if(iterator != null)
return !iterator.hasNext();
return false;
}
/**
* Unload chunks