improve zone map code, fix #2820
This commit is contained in:
parent
1144acd930
commit
8e357d961c
2 changed files with 20 additions and 17 deletions
|
@ -1,13 +1,12 @@
|
|||
package buildcraft.robotics.map;
|
||||
|
||||
import gnu.trove.map.hash.TIntObjectHashMap;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.util.IntHashMap;
|
||||
|
||||
import buildcraft.api.core.INBTStoreable;
|
||||
|
||||
public class MapRegion implements INBTStoreable {
|
||||
private final TIntObjectHashMap<MapChunk> chunks = new TIntObjectHashMap<MapChunk>();
|
||||
private final IntHashMap chunks = new IntHashMap();
|
||||
private final int x, z;
|
||||
|
||||
public MapRegion(int x, int z) {
|
||||
|
@ -24,28 +23,28 @@ public class MapRegion implements INBTStoreable {
|
|||
}
|
||||
|
||||
public boolean hasChunk(int x, int z) {
|
||||
return chunks.contains((z << 4) | x);
|
||||
return chunks.containsItem((z << 4) | x);
|
||||
}
|
||||
|
||||
public MapChunk getChunk(int x, int z) {
|
||||
int id = (z << 4) | x;
|
||||
MapChunk chunk = chunks.get(id);
|
||||
MapChunk chunk = (MapChunk) chunks.lookup(id);
|
||||
if (chunk == null) {
|
||||
chunk = new MapChunk(x, z);
|
||||
chunks.put(id, chunk);
|
||||
chunks.addKey(id, chunk);
|
||||
}
|
||||
return chunk;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFromNBT(NBTTagCompound tag) {
|
||||
chunks.clear();
|
||||
chunks.clearMap();
|
||||
|
||||
if (tag != null) {
|
||||
for (int i = 0; i < 256; i++) {
|
||||
if (tag.hasKey("r" + i)) {
|
||||
MapChunk chunk = new MapChunk(tag.getCompoundTag("r" + i));
|
||||
chunks.put(i, chunk);
|
||||
chunks.addKey(i, chunk);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -54,7 +53,7 @@ public class MapRegion implements INBTStoreable {
|
|||
@Override
|
||||
public void writeToNBT(NBTTagCompound tag) {
|
||||
for (int i = 0; i < 256; i++) {
|
||||
MapChunk chunk = chunks.get(i);
|
||||
MapChunk chunk = (MapChunk) chunks.lookup(i);
|
||||
if (chunk != null) {
|
||||
NBTTagCompound chunkNBT = new NBTTagCompound();
|
||||
synchronized (chunk) {
|
||||
|
|
|
@ -10,7 +10,6 @@ import java.util.HashMap;
|
|||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import gnu.trove.iterator.TLongIterator;
|
||||
import gnu.trove.map.hash.TLongLongHashMap;
|
||||
import gnu.trove.set.hash.TLongHashSet;
|
||||
|
||||
|
@ -86,12 +85,13 @@ public class MapWorld {
|
|||
}
|
||||
|
||||
public void save() {
|
||||
TLongIterator i = updatedChunks.iterator();
|
||||
|
||||
while (i.hasNext()) {
|
||||
long id = i.next();
|
||||
i.remove();
|
||||
long[] chunkList;
|
||||
synchronized (updatedChunks) {
|
||||
chunkList = updatedChunks.toArray();
|
||||
updatedChunks.clear();
|
||||
}
|
||||
|
||||
for (long id : chunkList) {
|
||||
MapRegion region = (MapRegion) regionMap.getValueByKey(id);
|
||||
if (region == null) {
|
||||
continue;
|
||||
|
@ -140,7 +140,9 @@ public class MapWorld {
|
|||
MapChunk chunk = getChunk(rchunk.xPosition, rchunk.zPosition);
|
||||
chunk.update(rchunk);
|
||||
updatedChunks.add(id);
|
||||
synchronized (timeToUpdate) {
|
||||
timeToUpdate.remove(rchunk);
|
||||
}
|
||||
regionUpdateTime.put(id, (new Date()).getTime());
|
||||
}
|
||||
|
||||
|
@ -149,6 +151,8 @@ public class MapWorld {
|
|||
}
|
||||
|
||||
public void updateChunkDelayed(Chunk chunk, byte time) {
|
||||
synchronized (timeToUpdate) {
|
||||
timeToUpdate.put(chunk, new Integer(time));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue