From c8013421e6dc23d455894151f03fecf0a14fe01c Mon Sep 17 00:00:00 2001 From: bm01 Date: Mon, 21 Jan 2013 03:07:18 +0100 Subject: [PATCH] Few chunk load optimizations --- .../nossr50/listeners/WorldListener.java | 9 +-- .../blockmeta/chunkmeta/ChunkManager.java | 2 +- .../blockmeta/chunkmeta/HashChunkManager.java | 64 +++++++++---------- .../blockmeta/chunkmeta/NullChunkManager.java | 2 +- 4 files changed, 35 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/listeners/WorldListener.java b/src/main/java/com/gmail/nossr50/listeners/WorldListener.java index 9783c0086..0f0fdcb8c 100644 --- a/src/main/java/com/gmail/nossr50/listeners/WorldListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/WorldListener.java @@ -79,16 +79,13 @@ public class WorldListener implements Listener { @EventHandler public void onChunkLoad(ChunkLoadEvent event) { Chunk chunk = event.getChunk(); - Entity[] chunkMobs = chunk.getEntities(); + Entity[] entities = chunk.getEntities(); - if (chunkMobs.length <= 0) - return; - - for(Entity entity : chunkMobs) { + for(Entity entity : entities) { if(!(entity instanceof LivingEntity) && !(entity instanceof FallingBlock)) continue; - mcMMO.placeStore.loadChunk(chunk.getX(), chunk.getZ(), event.getWorld()); + mcMMO.placeStore.loadChunk(chunk.getX(), chunk.getZ(), event.getWorld(), entities); return; } } diff --git a/src/main/java/com/gmail/nossr50/util/blockmeta/chunkmeta/ChunkManager.java b/src/main/java/com/gmail/nossr50/util/blockmeta/chunkmeta/ChunkManager.java index db262e546..9fee57617 100755 --- a/src/main/java/com/gmail/nossr50/util/blockmeta/chunkmeta/ChunkManager.java +++ b/src/main/java/com/gmail/nossr50/util/blockmeta/chunkmeta/ChunkManager.java @@ -39,7 +39,7 @@ public interface ChunkManager { * @param cz Chunk Z coordinate that is to be loaded * @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 diff --git a/src/main/java/com/gmail/nossr50/util/blockmeta/chunkmeta/HashChunkManager.java b/src/main/java/com/gmail/nossr50/util/blockmeta/chunkmeta/HashChunkManager.java index 7de158767..206688836 100755 --- a/src/main/java/com/gmail/nossr50/util/blockmeta/chunkmeta/HashChunkManager.java +++ b/src/main/java/com/gmail/nossr50/util/blockmeta/chunkmeta/HashChunkManager.java @@ -142,7 +142,7 @@ public class HashChunkManager implements ChunkManager { @Override public synchronized void loadChunklet(int cx, int cy, int cz, World world) { - loadChunk(cx, cz, world); + loadChunk(cx, cz, world, null); } @Override @@ -151,57 +151,51 @@ public class HashChunkManager implements ChunkManager { } @Override - public synchronized void loadChunk(int cx, int cz, World world) { - if (world == null) + public synchronized void loadChunk(int cx, int cz, World world, Entity[] entities) { + if (world == null || store.containsKey(world.getName() + "," + cx + "," + cz)) return; - if (store.containsKey(world.getName() + "," + cx + "," + cz)) - return; - - ChunkStore in = null; - 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()); } 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 { - in = readChunkStore(world, cx, cz); + chunkStore = readChunkStore(world, cx, cz); } - catch(Exception e) {} + catch (Exception e) {} - if (in == null || converted) + if (chunkStore == null) return; - store.put(world.getName() + "," + cx + "," + cz, in); + store.put(world.getName() + "," + cx + "," + cz, chunkStore); - List mobs = in.getSpawnedMobs(); + List mobs = chunkStore.getSpawnedMobs(); if (mobs.isEmpty()) return; + if (entities == null) + entities = world.getChunkAt(cx, cz).getEntities(); + iteratingMobs = true; - Entity[] chunkMobs = world.getChunkAt(cx, cz).getEntities(); - - for (Entity entity : chunkMobs) { - if(!(entity instanceof LivingEntity) && !(entity instanceof FallingBlock)) - continue; - + for (Entity entity : entities) { if (mobs.contains(entity.getUniqueId())) addSpawnedMob(entity); } - if(safeToRemoveMobs) + if (safeToRemoveMobs) iteratingMobs = false; - in.clearSpawnedMobs(); + chunkStore.clearSpawnedMobs(); } @Override @@ -238,29 +232,31 @@ public class HashChunkManager implements ChunkManager { if (world == null) return; - if(savingWorld && savedChunks.contains(world.getName() + "," + cx + "," + cz)) + if (savingWorld && savedChunks.contains(world.getName() + "," + cx + "," + cz)) return; boolean unloaded = false; - if (!store.containsKey(world.getName() + "," + cx + "," + cz)) { + String key = world.getName() + "," + cx + "," + cz; + + if (!store.containsKey(key)) { List tempSpawnedMobs = new ArrayList(spawnedMobs); tempSpawnedMobs.removeAll(checkedMobs); for (Entity entity : tempSpawnedMobs) { if (!isEntityInChunk(entity, cx, cz, world)) continue; - loadChunk(cx, cz, world); + loadChunk(cx, cz, world, null); unloaded = true; break; } } - if (!store.containsKey(world.getName() + "," + cx + "," + cz) && unloaded) { + if (!store.containsKey(key) && unloaded) { ChunkStore cStore = ChunkStoreFactory.getChunkStore(world, cx, cz); 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); List tempSpawnedMobs = new ArrayList(spawnedMobs); @@ -453,7 +449,7 @@ public class HashChunkManager implements ChunkManager { String key = world.getName() + "," + cx + "," + cz; if (!store.containsKey(key)) { - loadChunk(cx, cz, world); + loadChunk(cx, cz, world, null); } if (!store.containsKey(key)) { @@ -489,7 +485,7 @@ public class HashChunkManager implements ChunkManager { String key = world.getName() + "," + cx + "," + cz; if (!store.containsKey(key)) { - loadChunk(cx, cz, world); + loadChunk(cx, cz, world, null); } ChunkStore cStore = store.get(key); @@ -524,7 +520,7 @@ public class HashChunkManager implements ChunkManager { String key = world.getName() + "," + cx + "," + cz; if (!store.containsKey(key)) { - loadChunk(cx, cz, world); + loadChunk(cx, cz, world, null); } ChunkStore cStore = store.get(key); @@ -580,7 +576,7 @@ public class HashChunkManager implements ChunkManager { converters.add(converter); } - return true; + return true; } public boolean isSpawnedMob(Entity entity) { diff --git a/src/main/java/com/gmail/nossr50/util/blockmeta/chunkmeta/NullChunkManager.java b/src/main/java/com/gmail/nossr50/util/blockmeta/chunkmeta/NullChunkManager.java index 6f0caa90c..a7daec5e6 100755 --- a/src/main/java/com/gmail/nossr50/util/blockmeta/chunkmeta/NullChunkManager.java +++ b/src/main/java/com/gmail/nossr50/util/blockmeta/chunkmeta/NullChunkManager.java @@ -29,7 +29,7 @@ public class NullChunkManager implements ChunkManager { public void unloadChunklet(int cx, int cy, int cz, World world) {} @Override - public void loadChunk(int cx, int cz, World world) {} + public void loadChunk(int cx, int cz, World world, Entity[] entities) {} @Override public void unloadChunk(int cx, int cz, World world) {}