-
Notifications
You must be signed in to change notification settings - Fork 497
/
MapWorld.java
191 lines (160 loc) · 4.4 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
import gnu.trove.map.hash.TLongObjectHashMap;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import buildcraft.core.lib.utils.NBTUtils;
import buildcraft.core.lib.utils.ThreadSafeUtils;
public class MapWorld {
private final World world;
private final TLongObjectHashMap<MapRegion> regionMap;
private final Set<QueuedXZ> regionUpdateSet = new HashSet<QueuedXZ>();
private final Queue<QueuedXZ> queuedChunks;
private final File location;
private class QueuedXZ {
int x, z, p;
QueuedXZ(int x, int z, int p) {
this.x = x;
this.z = z;
this.p = p;
}
@Override
public boolean equals(Object other) {
if (other == null || !(other instanceof QueuedXZ)) {
return false;
}
return ((QueuedXZ) other).x == x && ((QueuedXZ) other).z == z;
}
@Override
public int hashCode() {
return x * 31 + z;
}
}
public MapWorld(World world, File location) {
this.world = world;
regionMap = new TLongObjectHashMap<MapRegion>();
queuedChunks = new PriorityQueue<QueuedXZ>(11, new Comparator<QueuedXZ>() {
@Override
public int compare(QueuedXZ c1, QueuedXZ c2) {
return (c1 != null ? c1.p : 0) - (c2 != null ? c2.p : 0);
}
});
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 long getXzId(int x, int z) {
return (x << 24) | z;
}
private MapRegion getRegion(int x, int z) {
long id = getXzId(x, z);
MapRegion region = regionMap.get(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.put(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 queueChunkForUpdate(int x, int z, int priority) {
synchronized (queuedChunks) {
queuedChunks.add(new QueuedXZ(x, z, priority));
}
}
public void queueChunkForUpdateIfEmpty(int x, int z, int priority) {
if (!hasChunk(x, z)) {
queueChunkForUpdate(x, z, priority);
}
}
public void updateChunkInQueue() {
synchronized (queuedChunks) {
if (queuedChunks.size() == 0) {
return;
}
QueuedXZ q = queuedChunks.remove();
if (q == null) {
return;
}
if (world.getChunkProvider().chunkExists(q.x, q.z)) {
updateChunk(q.x, q.z);
}
}
}
public void save() {
Iterator<QueuedXZ> i = regionUpdateSet.iterator();
while (i.hasNext()) {
QueuedXZ id = i.next();
i.remove();
if (id == null) {
continue;
}
MapRegion region = regionMap.get(getXzId(id.x, id.z));
if (region == null) {
continue;
}
NBTTagCompound output = new NBTTagCompound();
region.writeToNBT(output);
byte[] data = NBTUtils.save(output);
File file = new File(location, "r" + id.x + "," + id.z + ".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);
}
private void updateChunk(int x, int z) {
MapChunk chunk = getChunk(x, z);
chunk.update(ThreadSafeUtils.getChunk(world, x, z));
regionUpdateSet.add(new QueuedXZ(x >> 4, z >> 4, 0));
// priority does not matter - see equals
synchronized (queuedChunks) {
queuedChunks.remove(new QueuedXZ(x, z, 0));
}
}
}