Merge pull request #506 from Glitchfinder/master

Further reducing lag when saving or unloading a world.
This commit is contained in:
Glitchfinder 2013-01-14 14:25:18 -08:00
commit a92843533b

View File

@ -32,9 +32,9 @@ public class HashChunkManager implements ChunkManager {
private List<Entity> spawnedMobs = new ArrayList<Entity>(); private List<Entity> spawnedMobs = new ArrayList<Entity>();
private List<Entity> spawnedPets = new ArrayList<Entity>(); private List<Entity> spawnedPets = new ArrayList<Entity>();
private List<Entity> mobsToRemove = new ArrayList<Entity>(); private List<Entity> mobsToRemove = new ArrayList<Entity>();
private List<Entity> tempSpawnedMobs = new ArrayList<Entity>();
private List<Entity> tempSpawnedPets = new ArrayList<Entity>();
private List<String> savedChunks = new ArrayList<String>(); private List<String> savedChunks = new ArrayList<String>();
private List<Entity> checkedMobs = new ArrayList<Entity>();
private List<Entity> removalCheckedMobs = new ArrayList<Entity>();
private boolean safeToRemoveMobs = true; private boolean safeToRemoveMobs = true;
private boolean savingWorld = false; private boolean savingWorld = false;
private boolean iteratingMobs = false; private boolean iteratingMobs = false;
@ -204,24 +204,33 @@ public class HashChunkManager implements ChunkManager {
iteratingMobs = true; iteratingMobs = true;
for (Entity entity : spawnedMobs) { List<Entity> tempSpawnedMobs = new ArrayList<Entity>(spawnedMobs);
tempSpawnedMobs.remove(removalCheckedMobs);
tempSpawnedMobs.remove(checkedMobs);
for (Entity entity : tempSpawnedMobs) {
if (!isEntityInChunk(entity, cx, cz, world)) if (!isEntityInChunk(entity, cx, cz, world))
continue; continue;
mobsToRemove.add(entity); mobsToRemove.add(entity);
removalCheckedMobs.add(entity);
} }
for (Entity entity : spawnedPets) { List<Entity> tempSpawnedPets = new ArrayList<Entity>(spawnedPets);
tempSpawnedPets.remove(removalCheckedMobs);
tempSpawnedPets.remove(checkedMobs);
for (Entity entity : tempSpawnedPets) {
if (!isEntityInChunk(entity, cx, cz, world)) if (!isEntityInChunk(entity, cx, cz, world))
continue; continue;
mobsToRemove.add(entity); mobsToRemove.add(entity);
removalCheckedMobs.add(entity);
} }
if (safeToRemoveMobs) { if (safeToRemoveMobs) {
spawnedMobs.remove(mobsToRemove); spawnedMobs.remove(mobsToRemove);
spawnedPets.remove(mobsToRemove); spawnedPets.remove(mobsToRemove);
mobsToRemove.clear(); mobsToRemove.clear();
removalCheckedMobs.clear();
iteratingMobs = false; iteratingMobs = false;
} }
} }
@ -237,7 +246,8 @@ public class HashChunkManager implements ChunkManager {
boolean unloaded = false; boolean unloaded = false;
if (!store.containsKey(world.getName() + "," + cx + "," + cz)) { if (!store.containsKey(world.getName() + "," + cx + "," + cz)) {
tempSpawnedMobs = new ArrayList<Entity>(spawnedMobs); List<Entity> tempSpawnedMobs = new ArrayList<Entity>(spawnedMobs);
tempSpawnedMobs.remove(checkedMobs);
for (Entity entity : tempSpawnedMobs) { for (Entity entity : tempSpawnedMobs) {
if (!isEntityInChunk(entity, cx, cz, world)) if (!isEntityInChunk(entity, cx, cz, world))
continue; continue;
@ -248,7 +258,8 @@ public class HashChunkManager implements ChunkManager {
} }
if (!unloaded) { if (!unloaded) {
tempSpawnedPets = new ArrayList<Entity>(spawnedPets); List<Entity> tempSpawnedPets = new ArrayList<Entity>(spawnedPets);
tempSpawnedPets.remove(checkedMobs);
for (Entity entity : tempSpawnedPets) { for (Entity entity : tempSpawnedPets) {
if (!isEntityInChunk(entity, cx, cz, world)) if (!isEntityInChunk(entity, cx, cz, world))
continue; continue;
@ -268,20 +279,24 @@ public class HashChunkManager implements ChunkManager {
if (store.containsKey(world.getName() + "," + cx + "," + cz)) { if (store.containsKey(world.getName() + "," + cx + "," + cz)) {
ChunkStore out = store.get(world.getName() + "," + cx + "," + cz); ChunkStore out = store.get(world.getName() + "," + cx + "," + cz);
tempSpawnedMobs = new ArrayList<Entity>(spawnedMobs); List<Entity> tempSpawnedMobs = new ArrayList<Entity>(spawnedMobs);
tempSpawnedMobs.remove(checkedMobs);
for (Entity entity : tempSpawnedMobs) { for (Entity entity : tempSpawnedMobs) {
if (!isEntityInChunk(entity, cx, cz, world)) if (!isEntityInChunk(entity, cx, cz, world))
continue; continue;
out.addSpawnedMob(entity.getUniqueId()); out.addSpawnedMob(entity.getUniqueId());
checkedMobs.add(entity);
} }
tempSpawnedPets = new ArrayList<Entity>(spawnedPets); List<Entity> tempSpawnedPets = new ArrayList<Entity>(spawnedPets);
tempSpawnedPets.remove(checkedMobs);
for (Entity entity : tempSpawnedPets) { for (Entity entity : tempSpawnedPets) {
if (!isEntityInChunk(entity, cx, cz, world)) if (!isEntityInChunk(entity, cx, cz, world))
continue; continue;
out.addSpawnedPet(entity.getUniqueId()); out.addSpawnedPet(entity.getUniqueId());
checkedMobs.add(entity);
} }
if (!out.isDirty()) if (!out.isDirty())
@ -292,6 +307,8 @@ public class HashChunkManager implements ChunkManager {
if(savingWorld) if(savingWorld)
savedChunks.add(world.getName() + "," + cx + "," + cz); savedChunks.add(world.getName() + "," + cx + "," + cz);
else
checkedMobs.clear();
} }
private boolean isEntityInChunk(Entity entity, int cx, int cz, World world) { private boolean isEntityInChunk(Entity entity, int cx, int cz, World world) {
@ -356,7 +373,8 @@ public class HashChunkManager implements ChunkManager {
} }
} }
tempSpawnedMobs = new ArrayList<Entity>(spawnedMobs); List<Entity> tempSpawnedMobs = new ArrayList<Entity>(spawnedMobs);
tempSpawnedMobs.remove(checkedMobs);
for (Entity entity : tempSpawnedMobs) { for (Entity entity : tempSpawnedMobs) {
World entityWorld = entity.getWorld(); World entityWorld = entity.getWorld();
@ -369,7 +387,8 @@ public class HashChunkManager implements ChunkManager {
saveChunk(cx, cz, world); saveChunk(cx, cz, world);
} }
tempSpawnedPets = new ArrayList<Entity>(spawnedPets); List<Entity> tempSpawnedPets = new ArrayList<Entity>(spawnedPets);
tempSpawnedPets.remove(checkedMobs);
for (Entity entity : tempSpawnedPets) { for (Entity entity : tempSpawnedPets) {
World entityWorld = entity.getWorld(); World entityWorld = entity.getWorld();
@ -384,6 +403,7 @@ public class HashChunkManager implements ChunkManager {
savingWorld = false; savingWorld = false;
savedChunks.clear(); savedChunks.clear();
checkedMobs.clear();
} }
@Override @Override
@ -415,7 +435,9 @@ public class HashChunkManager implements ChunkManager {
safeToRemoveMobs = false; safeToRemoveMobs = false;
tempSpawnedMobs = new ArrayList<Entity>(spawnedMobs); List<Entity> tempSpawnedMobs = new ArrayList<Entity>(spawnedMobs);
tempSpawnedMobs.remove(checkedMobs);
tempSpawnedMobs.remove(removalCheckedMobs);
for (Entity entity : tempSpawnedMobs) { for (Entity entity : tempSpawnedMobs) {
World entityWorld = entity.getWorld(); World entityWorld = entity.getWorld();
@ -428,7 +450,9 @@ public class HashChunkManager implements ChunkManager {
unloadChunk(cx, cz, world); unloadChunk(cx, cz, world);
} }
tempSpawnedPets = new ArrayList<Entity>(spawnedPets); List<Entity> tempSpawnedPets = new ArrayList<Entity>(spawnedPets);
tempSpawnedPets.remove(checkedMobs);
tempSpawnedMobs.remove(removalCheckedMobs);
for (Entity entity : tempSpawnedPets) { for (Entity entity : tempSpawnedPets) {
World entityWorld = entity.getWorld(); World entityWorld = entity.getWorld();
@ -446,6 +470,8 @@ public class HashChunkManager implements ChunkManager {
spawnedMobs.remove(mobsToRemove); spawnedMobs.remove(mobsToRemove);
spawnedPets.remove(mobsToRemove); spawnedPets.remove(mobsToRemove);
mobsToRemove.clear(); mobsToRemove.clear();
checkedMobs.clear();
removalCheckedMobs.clear();
savingWorld = false; savingWorld = false;
savedChunks.clear(); savedChunks.clear();
} }
@ -643,7 +669,7 @@ public class HashChunkManager implements ChunkManager {
mobsToRemove.clear(); mobsToRemove.clear();
tempSpawnedMobs = new ArrayList<Entity>(spawnedMobs); List<Entity> tempSpawnedMobs = new ArrayList<Entity>(spawnedMobs);
for (Entity entity : tempSpawnedMobs) { for (Entity entity : tempSpawnedMobs) {
if (entity.isDead()) if (entity.isDead())
mobsToRemove.add(entity); mobsToRemove.add(entity);
@ -652,7 +678,7 @@ public class HashChunkManager implements ChunkManager {
mobsToRemove.add(entity); mobsToRemove.add(entity);
} }
tempSpawnedPets = new ArrayList<Entity>(spawnedPets); List<Entity> tempSpawnedPets = new ArrayList<Entity>(spawnedPets);
for (Entity entity : tempSpawnedPets) { for (Entity entity : tempSpawnedPets) {
if (entity.isDead()) if (entity.isDead())
mobsToRemove.add(entity); mobsToRemove.add(entity);