-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
PatchChunkProviderServer.java
328 lines (286 loc) · 9.58 KB
/
PatchChunkProviderServer.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package me.nallar.tickthreading.minecraft.patched;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import cpw.mods.fml.common.FMLLog;
import me.nallar.tickthreading.minecraft.TickThreading;
import me.nallar.tickthreading.patcher.Declare;
import net.minecraft.crash.CrashReport;
import net.minecraft.crash.CrashReportCategory;
import net.minecraft.util.ChunkCoordinates;
import net.minecraft.util.IProgressUpdate;
import net.minecraft.util.ReportedException;
import net.minecraft.world.ChunkCoordIntPair;
import net.minecraft.world.WorldServer;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.IChunkProvider;
import net.minecraft.world.chunk.storage.IChunkLoader;
import net.minecraft.world.gen.ChunkProviderServer;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.common.ForgeChunkManager;
public abstract class PatchChunkProviderServer extends ChunkProviderServer {
public Object genLock;
public Object chunkLoadLock;
public Map<Long, Object> chunkLoadLocks;
private Chunk lastChunk;
private net.minecraft.util.LongHashMap loadingChunkHashMap;
private int unloadTicks;
public void construct() {
chunkLoadLock = new Object();
chunkLoadLocks = new HashMap<Long, Object>();
genLock = new Object();
loadingChunkHashMap = new net.minecraft.util.LongHashMap();
}
public PatchChunkProviderServer(WorldServer par1WorldServer, IChunkLoader par2IChunkLoader, IChunkProvider par3IChunkProvider) {
super(par1WorldServer, par2IChunkLoader, par3IChunkProvider);
}
@Override
public void unloadChunksIfNotNearSpawn(int par1, int par2) {
long hash = ChunkCoordIntPair.chunkXZ2Int(par1, par2);
if (loadedChunkHashMap.getValueByKey(hash) == null) {
} else if (TickThreading.instance.shouldLoadSpawn && this.currentServer.provider.canRespawnHere() && DimensionManager.shouldLoadSpawn(currentServer.provider.dimensionId)) {
ChunkCoordinates var3 = this.currentServer.getSpawnPoint();
int var4 = par1 * 16 + 8 - var3.posX;
int var5 = par2 * 16 + 8 - var3.posZ;
short var6 = 128;
if (var4 < -var6 || var4 > var6 || var5 < -var6 || var5 > var6) {
synchronized (chunksToUnload) {
this.chunksToUnload.add(hash);
}
}
} else {
synchronized (chunksToUnload) {
this.chunksToUnload.add(hash);
}
}
}
/* 100 chunks?
* I DO WHAT I WANT!
*/
@Override
public boolean unload100OldestChunks() {
if (!this.currentServer.canNotSave) {
for (ChunkCoordIntPair forced : currentServer.getPersistentChunks().keySet()) {
this.chunksToUnload.remove(ChunkCoordIntPair.chunkXZ2Int(forced.chunkXPos, forced.chunkZPos));
}
synchronized (chunksToUnload) {
Iterator<Long> i$ = chunksToUnload.iterator();
for (int i = 0; i < 200 && i$.hasNext(); ++i) {
Long var2 = i$.next();
i$.remove();
Chunk var3 = (Chunk) this.loadedChunkHashMap.getValueByKey(var2);
if (var3 != null) {
if (lastChunk == var3) {
lastChunk = null;
}
this.safeSaveChunk(var3);
this.safeSaveExtraChunkData(var3);
var3.onChunkUnload();
synchronized (loadedChunks) {
this.loadedChunks.remove(var3);
}
}
this.loadedChunkHashMap.remove(var2);
}
}
if (this.currentChunkLoader != null) {
this.currentChunkLoader.chunkTick();
}
}
if (unloadTicks++ > 200 && this.currentServer.provider.dimensionId != 0 && loadedChunks.isEmpty() && ForgeChunkManager.getPersistentChunksFor(currentServer).isEmpty() && (!TickThreading.instance.shouldLoadSpawn || !DimensionManager.shouldLoadSpawn(currentServer.provider.dimensionId))) {
DimensionManager.unloadWorld(currentServer.provider.dimensionId);
}
return this.currentChunkProvider.unload100OldestChunks();
}
@Override
@Declare
public Chunk getChunkIfExists(int x, int z) {
return (Chunk) this.loadedChunkHashMap.getValueByKey(ChunkCoordIntPair.chunkXZ2Int(x, z));
}
@Override
public Chunk provideChunk(int x, int z) {
Chunk chunk = lastChunk;
if (chunk == null || chunk.xPosition != x || chunk.zPosition != z) {
chunk = (Chunk) this.loadedChunkHashMap.getValueByKey(ChunkCoordIntPair.chunkXZ2Int(x, z));
chunk = (chunk == null ? (!this.currentServer.findingSpawnPoint && !this.loadChunkOnProvideRequest ? this.defaultEmptyChunk : this.loadChunk(x, z, false, null)) : chunk);
lastChunk = chunk;
return chunk;
}
return chunk;
}
@Override
protected Chunk safeLoadChunk(int x, int z) {
if (this.currentChunkLoader == null) {
return null;
} else {
try {
Chunk chunk = this.currentChunkLoader.loadChunk(this.currentServer, x, z);
if (chunk != null) {
chunk.lastSaveTime = this.currentServer.getTotalWorldTime();
}
return chunk;
} catch (Exception e) {
FMLLog.severe("Failed to load chunk", e);
return null;
}
}
}
@Override
public Chunk loadChunk(int x, int z) {
return loadChunk(x, x, true, null);
}
@Override
@Declare
public Chunk loadChunk(int x, int z, Runnable runnable) {
return loadChunk(x, z, true, runnable);
}
@Override
@Declare
public Chunk loadChunk(int x, int z, boolean checkAvailable, Runnable runnable) {
long var3 = ChunkCoordIntPair.chunkXZ2Int(x, z);
Chunk var5 = null;
if (checkAvailable) {
synchronized (chunksToUnload) {
this.chunksToUnload.remove(Long.valueOf(var3));
}
var5 = (Chunk) this.loadedChunkHashMap.getValueByKey(var3);
}
if (var5 != null) {
return var5;
}
final Object lock = getLock(x, z);
// Lock on the lock for this chunk - prevent multiple instances of the same chunk
synchronized (lock) {
var5 = (Chunk) this.loadedChunkHashMap.getValueByKey(var3);
if (var5 != null) {
return var5;
}
var5 = (Chunk) loadingChunkHashMap.getValueByKey(var3);
if (var5 == null) {
var5 = this.safeLoadChunk(x, z);
if (var5 != null) {
this.loadingChunkHashMap.add(var3, var5);
}
}
}
// Unlock this chunk - avoids a deadlock
// Thread A - requests chunk A - needs genned
// Thread B - requests chunk B - needs genned
// In thread A, redpower tries to load chunk B
// because its marble gen is buggy.
// Thread B is now waiting for the generate lock,
// Thread A is waiting for the lock on chunk B
// Lock the generation lock - ChunkProviderGenerate isn't threadsafe at all
// TODO: Possibly make ChunkProviderGenerate threadlocal? Would need many changes to
// structure code to get it to work properly.
synchronized (genLock) {
synchronized (lock) {
var5 = (Chunk) this.loadedChunkHashMap.getValueByKey(var3);
if (var5 != null) {
return var5;
}
var5 = (Chunk) this.loadingChunkHashMap.getValueByKey(var3);
if (var5 == null) {
if (this.currentChunkProvider == null) {
var5 = this.defaultEmptyChunk;
} else {
try {
var5 = this.currentChunkProvider.provideChunk(x, z);
} catch (Throwable var9) {
CrashReport var7 = CrashReport.makeCrashReport(var9, "Exception generating new chunk");
CrashReportCategory var8 = var7.makeCategory("Chunk to be generated");
var8.addCrashSection("Location", String.format("%d,%d", x, z));
var8.addCrashSection("Position hash", var3);
var8.addCrashSection("Generator", this.currentChunkProvider.makeString());
throw new ReportedException(var7);
}
}
} else {
if (this.currentChunkProvider != null) {
this.currentChunkProvider.recreateStructures(x, z);
}
}
if (var5 == null) {
throw new IllegalStateException("Null chunk was provided!");
}
this.loadingChunkHashMap.remove(var3);
this.loadedChunkHashMap.add(var3, var5);
synchronized (loadedChunks) {
this.loadedChunks.add(var5);
}
var5.populateChunk(this, this, x, z);
}
}
var5.onChunkLoad();
chunkLoadLocks.remove(hash(x, z));
return var5;
}
@Override
public void unloadAllChunks() {
if (loadedChunks.size() == currentServer.getPersistentChunks().size()) {
return;
}
synchronized (loadedChunks) {
for (Object loadedChunk : this.loadedChunks) {
Chunk var2 = (Chunk) loadedChunk;
this.unloadChunksIfNotNearSpawn(var2.xPosition, var2.zPosition);
}
}
}
@Override
public boolean saveChunks(boolean par1, IProgressUpdate par2IProgressUpdate) {
int var3 = 0;
synchronized (loadedChunks) {
for (Object loadedChunk : this.loadedChunks) {
Chunk var5 = (Chunk) loadedChunk;
if (par1) {
this.safeSaveExtraChunkData(var5);
}
if (var5.needsSaving(par1)) {
this.safeSaveChunk(var5);
var5.isModified = false;
++var3;
if (var3 == 24 && !par1) {
return false;
}
}
}
}
if (par1) {
if (this.currentChunkLoader == null) {
return true;
}
this.currentChunkLoader.saveExtraData();
}
return true;
}
@Override
@Declare
public List<Chunk> getLoadedChunks() {
return loadedChunks;
}
@Override
@Declare
public Set<Long> getChunksToUnloadSet() {
return chunksToUnload;
}
public Object getLock(int x, int z) {
long hash = hash(x, z);
Object lock = chunkLoadLocks.get(hash);
if (lock == null) {
synchronized (chunkLoadLock) {
lock = chunkLoadLocks.get(hash);
if (lock == null) {
lock = new Object();
chunkLoadLocks.put(hash, lock);
}
}
}
return lock;
}
private static long hash(int x, int y) {
return (((long) x) << 32) | (y & 0xffffffffL);
}
}