Condensing entity tracking into a single set of storage, to reduce CPU use. Also handily causes invalid falling blocks to be removed. (Fell out of the world, etc)

This commit is contained in:
Glitchfinder 2013-01-18 14:43:14 -08:00
parent d09526e470
commit ca2673f258
4 changed files with 28 additions and 151 deletions

View File

@ -58,20 +58,15 @@ public class EntityListener implements Listener {
Entity entity = event.getEntity(); Entity entity = event.getEntity();
if (entity instanceof FallingBlock) { if (entity instanceof FallingBlock) {
int entityID = entity.getEntityId();
Block block = event.getBlock(); Block block = event.getBlock();
Material type = block.getType();
if (type == Material.SAND || type == Material.GRAVEL) { if (mcMMO.placeStore.isTrue(block) && !mcMMO.placeStore.isSpawnedMob(entity)) {
if (mcMMO.placeStore.isTrue(block)) { mcMMO.placeStore.setFalse(block);
mcMMO.placeStore.setFalse(block); mcMMO.placeStore.addSpawnedMob(entity);
plugin.addToFallingBlockTracker(entityID, block); }
} else if (mcMMO.placeStore.isSpawnedMob(entity)) {
mcMMO.placeStore.setTrue(block);
if (plugin.fallingBlockIsTracked(entityID)) { mcMMO.placeStore.removeSpawnedMob(entity);
mcMMO.placeStore.setTrue(block);
plugin.removeFromFallingBlockTracker(entityID);
}
} }
} }
} }

View File

@ -97,7 +97,6 @@ public class mcMMO extends JavaPlugin {
private HashMap<String, String> aliasMap = new HashMap<String, String>(); //Alias - Command private HashMap<String, String> aliasMap = new HashMap<String, String>(); //Alias - Command
private HashMap<Integer, String> tntTracker = new HashMap<Integer, String>(); private HashMap<Integer, String> tntTracker = new HashMap<Integer, String>();
private HashMap<Integer, Block> fallingBlockTracker = new HashMap<Integer, Block>();
private static Database database; private static Database database;
public static mcMMO p; public static mcMMO p;
@ -521,44 +520,6 @@ public class mcMMO extends JavaPlugin {
tntTracker.remove(tntID); tntTracker.remove(tntID);
} }
/**
* Add an ID value to the FallingBlock tracker.
*
* @param fallingBlockID The EntityID of the FallingBlock
*/
public void addToFallingBlockTracker(int fallingBlockID, Block sourceBlock) {
fallingBlockTracker.put(fallingBlockID, sourceBlock);
}
/**
* Check to see if a given FallingBlock Entity is tracked.
*
* @param tntID The EntityID of the FallingBlock
* @return true if the FallingBlock is being tracked, false otherwise
*/
public boolean fallingBlockIsTracked(int fallingBlockID) {
return fallingBlockTracker.containsKey(fallingBlockID);
}
/**
* Get the initial location of the FallingBlock.
*
* @param fallingBlockID The EntityID of the FallingBlock
* @return the Player who detonated it
*/
public Block getSourceBlock(int fallingBlockID) {
return fallingBlockTracker.get(fallingBlockID);
}
/**
* Remove FallingBlock from the tracker after it lands.
*
* @param fallingBlockID The EntityID of the FallingBlock
*/
public void removeFromFallingBlockTracker(int fallingBlockID) {
fallingBlockTracker.remove(fallingBlockID);
}
public static String getMainDirectory() { public static String getMainDirectory() {
return mainDirectory; return mainDirectory;
} }

View File

@ -29,7 +29,6 @@ public class HashChunkManager implements ChunkManager {
public ArrayList<BlockStoreConversionZDirectory> converters = new ArrayList<BlockStoreConversionZDirectory>(); public ArrayList<BlockStoreConversionZDirectory> converters = new ArrayList<BlockStoreConversionZDirectory>();
private HashMap<UUID, Boolean> oldData = new HashMap<UUID, Boolean>(); private HashMap<UUID, Boolean> oldData = new HashMap<UUID, Boolean>();
private List<Entity> spawnedMobs = new ArrayList<Entity>(); private List<Entity> spawnedMobs = 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<String> savedChunks = new ArrayList<String>(); private List<String> savedChunks = new ArrayList<String>();
private List<Entity> checkedMobs = new ArrayList<Entity>(); private List<Entity> checkedMobs = new ArrayList<Entity>();
@ -174,26 +173,21 @@ public class HashChunkManager implements ChunkManager {
store.put(world.getName() + "," + cx + "," + cz, in); store.put(world.getName() + "," + cx + "," + cz, in);
List<UUID> mobs = in.getSpawnedMobs(); List<UUID> mobs = in.getSpawnedMobs();
List<UUID> pets = in.getSpawnedPets();
if (mobs.isEmpty() && pets.isEmpty()) if (mobs.isEmpty())
return; return;
iteratingMobs = true; iteratingMobs = true;
for (LivingEntity entity : world.getLivingEntities()) { for (Entity entity : world.getEntities()) {
if (mobs.contains(entity.getUniqueId())) if (mobs.contains(entity.getUniqueId()))
addSpawnedMob(entity); addSpawnedMob(entity);
if (pets.contains(entity.getUniqueId()))
addSpawnedPet(entity);
} }
if(safeToRemoveMobs) if(safeToRemoveMobs)
iteratingMobs = false; iteratingMobs = false;
in.clearSpawnedMobs(); in.clearSpawnedMobs();
in.clearSpawnedPets();
} }
} }
@ -217,20 +211,8 @@ public class HashChunkManager implements ChunkManager {
removalCheckedMobs.add(entity); removalCheckedMobs.add(entity);
} }
List<Entity> tempSpawnedPets = new ArrayList<Entity>(spawnedPets);
tempSpawnedPets.remove(removalCheckedMobs);
tempSpawnedPets.remove(checkedMobs);
for (Entity entity : tempSpawnedPets) {
if (!isEntityInChunk(entity, cx, cz, world))
continue;
mobsToRemove.add(entity);
removalCheckedMobs.add(entity);
}
if (safeToRemoveMobs) { if (safeToRemoveMobs) {
spawnedMobs.remove(mobsToRemove); spawnedMobs.remove(mobsToRemove);
spawnedPets.remove(mobsToRemove);
mobsToRemove.clear(); mobsToRemove.clear();
removalCheckedMobs.clear(); removalCheckedMobs.clear();
iteratingMobs = false; iteratingMobs = false;
@ -258,19 +240,6 @@ public class HashChunkManager implements ChunkManager {
unloaded = true; unloaded = true;
break; break;
} }
if (!unloaded) {
List<Entity> tempSpawnedPets = new ArrayList<Entity>(spawnedPets);
tempSpawnedPets.remove(checkedMobs);
for (Entity entity : tempSpawnedPets) {
if (!isEntityInChunk(entity, cx, cz, world))
continue;
loadChunk(cx, cz, world);
unloaded = true;
break;
}
}
} }
if (!store.containsKey(world.getName() + "," + cx + "," + cz) && unloaded) { if (!store.containsKey(world.getName() + "," + cx + "," + cz) && unloaded) {
@ -291,16 +260,6 @@ public class HashChunkManager implements ChunkManager {
checkedMobs.add(entity); checkedMobs.add(entity);
} }
List<Entity> tempSpawnedPets = new ArrayList<Entity>(spawnedPets);
tempSpawnedPets.remove(checkedMobs);
for (Entity entity : tempSpawnedPets) {
if (!isEntityInChunk(entity, cx, cz, world))
continue;
out.addSpawnedPet(entity.getUniqueId());
checkedMobs.add(entity);
}
if (!out.isDirty()) if (!out.isDirty())
return; return;
@ -389,20 +348,6 @@ public class HashChunkManager implements ChunkManager {
saveChunk(cx, cz, world); saveChunk(cx, cz, world);
} }
List<Entity> tempSpawnedPets = new ArrayList<Entity>(spawnedPets);
tempSpawnedPets.remove(checkedMobs);
for (Entity entity : tempSpawnedPets) {
World entityWorld = entity.getWorld();
if (world != entityWorld)
continue;
int cx = entity.getLocation().getChunk().getX();
int cz = entity.getLocation().getChunk().getZ();
saveChunk(cx, cz, world);
}
savingWorld = false; savingWorld = false;
savedChunks.clear(); savedChunks.clear();
checkedMobs.clear(); checkedMobs.clear();
@ -452,25 +397,9 @@ public class HashChunkManager implements ChunkManager {
unloadChunk(cx, cz, world); unloadChunk(cx, cz, world);
} }
List<Entity> tempSpawnedPets = new ArrayList<Entity>(spawnedPets);
tempSpawnedPets.remove(checkedMobs);
tempSpawnedMobs.remove(removalCheckedMobs);
for (Entity entity : tempSpawnedPets) {
World entityWorld = entity.getWorld();
if (world != entityWorld)
continue;
int cx = entity.getLocation().getChunk().getX();
int cz = entity.getLocation().getChunk().getZ();
unloadChunk(cx, cz, world);
}
safeToRemoveMobs = true; safeToRemoveMobs = true;
spawnedMobs.remove(mobsToRemove); spawnedMobs.remove(mobsToRemove);
spawnedPets.remove(mobsToRemove);
mobsToRemove.clear(); mobsToRemove.clear();
checkedMobs.clear(); checkedMobs.clear();
removalCheckedMobs.clear(); removalCheckedMobs.clear();
@ -642,7 +571,7 @@ public class HashChunkManager implements ChunkManager {
} }
public boolean isSpawnedPet(Entity entity) { public boolean isSpawnedPet(Entity entity) {
return spawnedPets.contains(entity); return spawnedMobs.contains(entity);
} }
public void addSpawnedMob(Entity entity) { public void addSpawnedMob(Entity entity) {
@ -651,8 +580,8 @@ public class HashChunkManager implements ChunkManager {
} }
public void addSpawnedPet(Entity entity) { public void addSpawnedPet(Entity entity) {
if (!isSpawnedPet(entity)) if (!isSpawnedMob(entity))
spawnedPets.add(entity); spawnedMobs.add(entity);
} }
public void removeSpawnedMob(Entity entity) { public void removeSpawnedMob(Entity entity) {
@ -661,8 +590,8 @@ public class HashChunkManager implements ChunkManager {
} }
public void removeSpawnedPet(Entity entity) { public void removeSpawnedPet(Entity entity) {
if (isSpawnedPet(entity)) if (isSpawnedMob(entity))
spawnedPets.remove(entity); spawnedMobs.remove(entity);
} }
public synchronized void cleanMobLists() { public synchronized void cleanMobLists() {
@ -680,17 +609,7 @@ public class HashChunkManager implements ChunkManager {
mobsToRemove.add(entity); mobsToRemove.add(entity);
} }
List<Entity> tempSpawnedPets = new ArrayList<Entity>(spawnedPets);
for (Entity entity : tempSpawnedPets) {
if (entity.isDead())
mobsToRemove.add(entity);
if (!entity.isValid())
mobsToRemove.add(entity);
}
spawnedMobs.remove(mobsToRemove); spawnedMobs.remove(mobsToRemove);
spawnedPets.remove(mobsToRemove);
mobsToRemove.clear(); mobsToRemove.clear();
} }
} }

View File

@ -17,13 +17,12 @@ public class PrimitiveChunkStore implements ChunkStore {
transient private boolean dirty = false; transient private boolean dirty = false;
/** X, Z, Y */ /** X, Z, Y */
public boolean[][][] store; public boolean[][][] store;
private static final int CURRENT_VERSION = 6; private static final int CURRENT_VERSION = 7;
private static final int MAGIC_NUMBER = 0xEA5EDEBB; private static final int MAGIC_NUMBER = 0xEA5EDEBB;
private int cx; private int cx;
private int cz; private int cz;
private UUID worldUid; private UUID worldUid;
private List<UUID> spawnedMobs = new ArrayList<UUID>(); private List<UUID> spawnedMobs = new ArrayList<UUID>();
private List<UUID> spawnedPets = new ArrayList<UUID>();
transient private int worldHeight; transient private int worldHeight;
transient private int xBitShifts; transient private int xBitShifts;
@ -110,7 +109,7 @@ public class PrimitiveChunkStore implements ChunkStore {
} }
public boolean isSpawnedPet(UUID id) { public boolean isSpawnedPet(UUID id) {
return spawnedPets.contains(id); return spawnedMobs.contains(id);
} }
public void addSpawnedMob(UUID id) { public void addSpawnedMob(UUID id) {
@ -122,7 +121,7 @@ public class PrimitiveChunkStore implements ChunkStore {
public void addSpawnedPet(UUID id) { public void addSpawnedPet(UUID id) {
if (!isSpawnedPet(id)) { if (!isSpawnedPet(id)) {
spawnedPets.add(id); spawnedMobs.add(id);
dirty = true; dirty = true;
} }
} }
@ -136,7 +135,7 @@ public class PrimitiveChunkStore implements ChunkStore {
public void removeSpawnedPet(UUID id) { public void removeSpawnedPet(UUID id) {
if (isSpawnedPet(id)) { if (isSpawnedPet(id)) {
spawnedPets.remove(id); spawnedMobs.remove(id);
dirty = true; dirty = true;
} }
} }
@ -149,8 +148,8 @@ public class PrimitiveChunkStore implements ChunkStore {
} }
public void clearSpawnedPets() { public void clearSpawnedPets() {
if (!spawnedPets.isEmpty()) { if (!spawnedMobs.isEmpty()) {
spawnedPets.clear(); spawnedMobs.clear();
dirty = true; dirty = true;
} }
} }
@ -160,7 +159,7 @@ public class PrimitiveChunkStore implements ChunkStore {
} }
public List<UUID> getSpawnedPets() { public List<UUID> getSpawnedPets() {
return spawnedPets; return spawnedMobs;
} }
private void writeObject(ObjectOutputStream out) throws IOException { private void writeObject(ObjectOutputStream out) throws IOException {
@ -174,7 +173,6 @@ public class PrimitiveChunkStore implements ChunkStore {
out.writeObject(store); out.writeObject(store);
out.writeObject(spawnedMobs); out.writeObject(spawnedMobs);
out.writeObject(spawnedPets);
dirty = false; dirty = false;
} }
@ -209,15 +207,19 @@ public class PrimitiveChunkStore implements ChunkStore {
fixArray(); fixArray();
if (fileVersionNumber < 6) { if (fileVersionNumber < 6) {
spawnedMobs = new ArrayList<UUID>(); spawnedMobs = new ArrayList<UUID>();
spawnedPets = new ArrayList<UUID>();
} }
dirty = true; dirty = true;
} }
if (fileVersionNumber >= 6) { if (fileVersionNumber == 6) {
//What do we want to do about this? These casts are unchecked. //What do we want to do about this? These casts are unchecked.
spawnedMobs = (ArrayList<UUID>) in.readObject(); spawnedMobs = (ArrayList<UUID>) in.readObject();
spawnedPets = (ArrayList<UUID>) in.readObject(); List<UUID> spawnedPets = (ArrayList<UUID>) in.readObject();
spawnedMobs.addAll(spawnedPets);
}
if(fileVersionNumber >= 7) {
spawnedMobs = (ArrayList<UUID>) in.readObject();
} }
} }