-
Notifications
You must be signed in to change notification settings - Fork 497
/
MapWorld.java
159 lines (137 loc) · 3.99 KB
/
MapWorld.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package buildcraft.robotics.map;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import gnu.trove.map.hash.TLongLongHashMap;
import gnu.trove.set.hash.TLongHashSet;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.LongHashMap;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import buildcraft.core.lib.utils.NBTUtils;
public class MapWorld {
private final LongHashMap regionMap;
private final HashMap<Chunk, Integer> timeToUpdate = new HashMap<Chunk, Integer>();
private final TLongLongHashMap regionUpdateTime;
private final TLongHashSet updatedChunks;
private final File location;
public MapWorld(World world, File location) {
regionMap = new LongHashMap();
regionUpdateTime = new TLongLongHashMap();
updatedChunks = new TLongHashSet();
String saveFolder = world.provider.getSaveFolder();
if (saveFolder == null) {
saveFolder = "world";
}
this.location = new File(location, saveFolder);
try {
this.location.mkdirs();
} catch (Exception e) {
e.printStackTrace();
}
}
private MapRegion getRegion(int x, int z) {
long id = MapUtils.getIDFromCoords(x, z);
MapRegion region = (MapRegion) regionMap.getValueByKey(id);
if (region == null) {
region = new MapRegion(x, z);
// Check in the location first
File target = new File(location, "r" + x + "," + z + ".nbt");
if (target.exists()) {
try {
FileInputStream f = new FileInputStream(target);
byte [] data = new byte [(int) target.length()];
f.read(data);
f.close();
region.readFromNBT(NBTUtils.load(data));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
regionMap.add(id, region);
}
return region;
}
private MapChunk getChunk(int x, int z) {
MapRegion region = getRegion(x >> 4, z >> 4);
return region.getChunk(x & 15, z & 15);
}
public boolean hasChunk(int x, int z) {
MapRegion region = getRegion(x >> 4, z >> 4);
return region.hasChunk(x & 15, z & 15);
}
public void save() {
long[] chunkList;
synchronized (updatedChunks) {
chunkList = updatedChunks.toArray();
updatedChunks.clear();
}
for (long id : chunkList) {
MapRegion region = (MapRegion) regionMap.getValueByKey(id);
if (region == null) {
continue;
}
NBTTagCompound output = new NBTTagCompound();
region.writeToNBT(output);
byte[] data = NBTUtils.save(output);
File file = new File(location, "r" + MapUtils.getXFromID(id) + "," + MapUtils.getZFromID(id) + ".nbt");
try {
FileOutputStream f = new FileOutputStream(file);
f.write(data);
f.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public int getColor(int x, int z) {
MapChunk chunk = getChunk(x >> 4, z >> 4);
return chunk.getColor(x & 15, z & 15);
}
public void tick() {
if (timeToUpdate.size() > 0) {
synchronized (timeToUpdate) {
Set<Chunk> chunks = new HashSet<Chunk>();
chunks.addAll(timeToUpdate.keySet());
for (Chunk c : chunks) {
int v = timeToUpdate.get(c);
if (v > 1) {
timeToUpdate.put(c, v - 1);
} else {
try {
updateChunk(c);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
public void updateChunk(Chunk rchunk) {
long id = MapUtils.getIDFromCoords(rchunk.xPosition, rchunk.zPosition);
MapChunk chunk = getChunk(rchunk.xPosition, rchunk.zPosition);
chunk.update(rchunk);
updatedChunks.add(id);
synchronized (timeToUpdate) {
timeToUpdate.remove(rchunk);
}
regionUpdateTime.put(id, (new Date()).getTime());
}
public long getUpdateTime(int x, int z) {
return regionUpdateTime.get(MapUtils.getIDFromCoords(x, z));
}
public void updateChunkDelayed(Chunk chunk, byte time) {
synchronized (timeToUpdate) {
timeToUpdate.put(chunk, (int) time);
}
}
}