Few chunk load optimizations

This commit is contained in:
bm01 2013-01-21 03:07:18 +01:00
parent f7aba26be2
commit c8013421e6
4 changed files with 35 additions and 42 deletions

View File

@ -79,16 +79,13 @@ public class WorldListener implements Listener {
@EventHandler @EventHandler
public void onChunkLoad(ChunkLoadEvent event) { public void onChunkLoad(ChunkLoadEvent event) {
Chunk chunk = event.getChunk(); Chunk chunk = event.getChunk();
Entity[] chunkMobs = chunk.getEntities(); Entity[] entities = chunk.getEntities();
if (chunkMobs.length <= 0) for(Entity entity : entities) {
return;
for(Entity entity : chunkMobs) {
if(!(entity instanceof LivingEntity) && !(entity instanceof FallingBlock)) if(!(entity instanceof LivingEntity) && !(entity instanceof FallingBlock))
continue; continue;
mcMMO.placeStore.loadChunk(chunk.getX(), chunk.getZ(), event.getWorld()); mcMMO.placeStore.loadChunk(chunk.getX(), chunk.getZ(), event.getWorld(), entities);
return; return;
} }
} }

View File

@ -39,7 +39,7 @@ public interface ChunkManager {
* @param cz Chunk Z coordinate that is to be loaded * @param cz Chunk Z coordinate that is to be loaded
* @param world World that the Chunk is in * @param world World that the Chunk is in
*/ */
public void loadChunk(int cx, int cz, World world); public void loadChunk(int cx, int cz, World world, Entity[] entities);
/** /**
* Unload a given Chunk's Chunklet data * Unload a given Chunk's Chunklet data

View File

@ -142,7 +142,7 @@ public class HashChunkManager implements ChunkManager {
@Override @Override
public synchronized void loadChunklet(int cx, int cy, int cz, World world) { public synchronized void loadChunklet(int cx, int cy, int cz, World world) {
loadChunk(cx, cz, world); loadChunk(cx, cz, world, null);
} }
@Override @Override
@ -151,57 +151,51 @@ public class HashChunkManager implements ChunkManager {
} }
@Override @Override
public synchronized void loadChunk(int cx, int cz, World world) { public synchronized void loadChunk(int cx, int cz, World world, Entity[] entities) {
if (world == null) if (world == null || store.containsKey(world.getName() + "," + cx + "," + cz))
return; return;
if (store.containsKey(world.getName() + "," + cx + "," + cz))
return;
ChunkStore in = null;
UUID key = world.getUID(); UUID key = world.getUID();
boolean oldDataHasKey = oldData.containsKey(key);
boolean converted = false;
if (!oldDataHasKey) { if (!oldData.containsKey(key)) {
oldData.put(key, (new File(world.getWorldFolder(), "mcmmo_data")).exists()); oldData.put(key, (new File(world.getWorldFolder(), "mcmmo_data")).exists());
} }
else if (oldData.get(key)) { else if (oldData.get(key)) {
converted = convertChunk(new File(world.getWorldFolder(), "mcmmo_data"), cx, cz, world, true); if (convertChunk(new File(world.getWorldFolder(), "mcmmo_data"), cx, cz, world, true))
return;
} }
ChunkStore chunkStore = null;
try { try {
in = readChunkStore(world, cx, cz); chunkStore = readChunkStore(world, cx, cz);
} }
catch(Exception e) {} catch (Exception e) {}
if (in == null || converted) if (chunkStore == null)
return; return;
store.put(world.getName() + "," + cx + "," + cz, in); store.put(world.getName() + "," + cx + "," + cz, chunkStore);
List<UUID> mobs = in.getSpawnedMobs(); List<UUID> mobs = chunkStore.getSpawnedMobs();
if (mobs.isEmpty()) if (mobs.isEmpty())
return; return;
if (entities == null)
entities = world.getChunkAt(cx, cz).getEntities();
iteratingMobs = true; iteratingMobs = true;
Entity[] chunkMobs = world.getChunkAt(cx, cz).getEntities(); for (Entity entity : entities) {
for (Entity entity : chunkMobs) {
if(!(entity instanceof LivingEntity) && !(entity instanceof FallingBlock))
continue;
if (mobs.contains(entity.getUniqueId())) if (mobs.contains(entity.getUniqueId()))
addSpawnedMob(entity); addSpawnedMob(entity);
} }
if(safeToRemoveMobs) if (safeToRemoveMobs)
iteratingMobs = false; iteratingMobs = false;
in.clearSpawnedMobs(); chunkStore.clearSpawnedMobs();
} }
@Override @Override
@ -238,29 +232,31 @@ public class HashChunkManager implements ChunkManager {
if (world == null) if (world == null)
return; return;
if(savingWorld && savedChunks.contains(world.getName() + "," + cx + "," + cz)) if (savingWorld && savedChunks.contains(world.getName() + "," + cx + "," + cz))
return; return;
boolean unloaded = false; boolean unloaded = false;
if (!store.containsKey(world.getName() + "," + cx + "," + cz)) { String key = world.getName() + "," + cx + "," + cz;
if (!store.containsKey(key)) {
List<Entity> tempSpawnedMobs = new ArrayList<Entity>(spawnedMobs); List<Entity> tempSpawnedMobs = new ArrayList<Entity>(spawnedMobs);
tempSpawnedMobs.removeAll(checkedMobs); tempSpawnedMobs.removeAll(checkedMobs);
for (Entity entity : tempSpawnedMobs) { for (Entity entity : tempSpawnedMobs) {
if (!isEntityInChunk(entity, cx, cz, world)) if (!isEntityInChunk(entity, cx, cz, world))
continue; continue;
loadChunk(cx, cz, world); loadChunk(cx, cz, world, null);
unloaded = true; unloaded = true;
break; break;
} }
} }
if (!store.containsKey(world.getName() + "," + cx + "," + cz) && unloaded) { if (!store.containsKey(key) && unloaded) {
ChunkStore cStore = ChunkStoreFactory.getChunkStore(world, cx, cz); ChunkStore cStore = ChunkStoreFactory.getChunkStore(world, cx, cz);
store.put(world.getName() + "," + cx + "," + cz, cStore); store.put(world.getName() + "," + cx + "," + cz, cStore);
} }
if (store.containsKey(world.getName() + "," + cx + "," + cz)) { if (store.containsKey(key)) {
ChunkStore out = store.get(world.getName() + "," + cx + "," + cz); ChunkStore out = store.get(world.getName() + "," + cx + "," + cz);
List<Entity> tempSpawnedMobs = new ArrayList<Entity>(spawnedMobs); List<Entity> tempSpawnedMobs = new ArrayList<Entity>(spawnedMobs);
@ -453,7 +449,7 @@ public class HashChunkManager implements ChunkManager {
String key = world.getName() + "," + cx + "," + cz; String key = world.getName() + "," + cx + "," + cz;
if (!store.containsKey(key)) { if (!store.containsKey(key)) {
loadChunk(cx, cz, world); loadChunk(cx, cz, world, null);
} }
if (!store.containsKey(key)) { if (!store.containsKey(key)) {
@ -489,7 +485,7 @@ public class HashChunkManager implements ChunkManager {
String key = world.getName() + "," + cx + "," + cz; String key = world.getName() + "," + cx + "," + cz;
if (!store.containsKey(key)) { if (!store.containsKey(key)) {
loadChunk(cx, cz, world); loadChunk(cx, cz, world, null);
} }
ChunkStore cStore = store.get(key); ChunkStore cStore = store.get(key);
@ -524,7 +520,7 @@ public class HashChunkManager implements ChunkManager {
String key = world.getName() + "," + cx + "," + cz; String key = world.getName() + "," + cx + "," + cz;
if (!store.containsKey(key)) { if (!store.containsKey(key)) {
loadChunk(cx, cz, world); loadChunk(cx, cz, world, null);
} }
ChunkStore cStore = store.get(key); ChunkStore cStore = store.get(key);

View File

@ -29,7 +29,7 @@ public class NullChunkManager implements ChunkManager {
public void unloadChunklet(int cx, int cy, int cz, World world) {} public void unloadChunklet(int cx, int cy, int cz, World world) {}
@Override @Override
public void loadChunk(int cx, int cz, World world) {} public void loadChunk(int cx, int cz, World world, Entity[] entities) {}
@Override @Override
public void unloadChunk(int cx, int cz, World world) {} public void unloadChunk(int cx, int cz, World world) {}