-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
ThreadedChunkProvider.java
909 lines (813 loc) · 28 KB
/
ThreadedChunkProvider.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
package me.nallar.patched.storage;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import com.google.common.collect.ImmutableSetMultimap;
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.common.registry.GameRegistry;
import me.nallar.collections.ConcurrentQueueSet;
import me.nallar.patched.annotation.FakeExtend;
import me.nallar.tickthreading.Log;
import me.nallar.tickthreading.minecraft.ChunkGarbageCollector;
import me.nallar.tickthreading.minecraft.DeadLockDetector;
import me.nallar.tickthreading.minecraft.TickThreading;
import me.nallar.tickthreading.patcher.Declare;
import me.nallar.tickthreading.util.BooleanThreadLocal;
import me.nallar.tickthreading.util.DoNothingRunnable;
import me.nallar.tickthreading.util.ServerThreadFactory;
import me.nallar.tickthreading.util.concurrent.NativeMutex;
import me.nallar.unsafe.UnsafeUtil;
import net.minecraft.entity.EnumCreatureType;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.management.PlayerManager;
import net.minecraft.util.IProgressUpdate;
import net.minecraft.util.LongHashMap;
import net.minecraft.world.ChunkCoordIntPair;
import net.minecraft.world.ChunkPosition;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.EmptyChunk;
import net.minecraft.world.chunk.IChunkProvider;
import net.minecraft.world.chunk.storage.AnvilChunkLoader;
import net.minecraft.world.chunk.storage.IChunkLoader;
import net.minecraft.world.gen.ChunkProviderServer;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.common.ForgeChunkManager;
import org.cliffc.high_scale_lib.NonBlockingHashMapLong;
/**
* This is a replacement for ChunkProviderServer
* Instead of attempting to patch a class with many different implementations,
* this replaces it with an implementation which is intended to be compatible
* with both Forge and MCPC+.
*/
@FakeExtend
public abstract class ThreadedChunkProvider extends ChunkProviderServer implements IChunkProvider {
/**
* You may also use a synchronized block on generateLock,
* and are encouraged to unless tryLock() is required.
* This works as NativeMutex uses JVM monitors internally.
*/
public final NativeMutex generateLock = new NativeMutex();
private static final ThreadPoolExecutor chunkLoadThreadPool;
static {
chunkLoadThreadPool = new ThreadPoolExecutor(1, Runtime.getRuntime().availableProcessors(), 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new ServerThreadFactory("Async ChunkLoader"));
chunkLoadThreadPool.allowCoreThreadTimeOut(true);
}
private static final Runnable doNothingRunnable = new DoNothingRunnable();
private static final int populationRange = 1;
private final NonBlockingHashMapLong<AtomicInteger> chunkLoadLocks = new NonBlockingHashMapLong<AtomicInteger>();
private final LongHashMap<Chunk> chunks = new LongHashMap<Chunk>();
private final LongHashMap<Chunk> loadingChunks = new LongHashMap<Chunk>();
private final LongHashMap<Chunk> unloadingChunks = new LongHashMap<Chunk>();
private final ConcurrentQueueSet<Long> unloadStage0 = new ConcurrentQueueSet<Long>();
private final ConcurrentLinkedQueue<QueuedUnload> unloadStage1 = new ConcurrentLinkedQueue<QueuedUnload>();
private final IChunkProvider generator; // Mojang shouldn't use the same interface for :(
private final IChunkLoader loader;
private final WorldServer world;
private final Chunk defaultEmptyChunk;
private final BooleanThreadLocal inUnload = new BooleanThreadLocal();
private final BooleanThreadLocal worldGenInProgress;
private boolean loadChunksInProvideChunk = true;
private int loadChunksInProvideChunkTicks = 0;
private int overloadCount = 0;
private int saveTicks = 0;
private int maxChunksToSave = 192;
private int lastChunksSaved = -1;
private Chunk lastChunk;
// Mojang compatiblity fields.
public final IChunkProvider currentChunkProvider;
public final Set<Long> chunksToUnload = null;
public final List<Chunk> loadedChunks;
public final IChunkLoader currentChunkLoader;
@SuppressWarnings ("UnusedDeclaration")
public boolean loadChunkOnProvideRequest;
public ThreadedChunkProvider(WorldServer world, IChunkLoader loader, IChunkProvider generator) {
super(world, loader, generator); // This call will be removed by javassist.
currentChunkProvider = this.generator = generator;
this.world = world;
currentChunkLoader = this.loader = loader;
loadedChunks = Collections.synchronizedList(new ArrayList<Chunk>());
world.emptyChunk = defaultEmptyChunk = new EmptyChunk(world, 0, 0);
world.worldGenInProgress = worldGenInProgress = new BooleanThreadLocal();
world.inImmediateBlockUpdate = new BooleanThreadLocal();
}
@Declare
public static void onChunkLoad(Chunk chunk, Runnable runnable) {
if (runnable instanceof me.nallar.tickthreading.util.ChunkLoadRunnable) {
((me.nallar.tickthreading.util.ChunkLoadRunnable) runnable).onLoad(chunk);
} else {
runnable.run();
}
}
@Override
@Declare
public WorldServer getWorld() {
return world;
}
@Override
@Declare
public List<Chunk> getLoadedChunks() {
return loadedChunks;
}
@Override
public boolean unload100OldestChunks() {
return generator.unload100OldestChunks();
}
@SuppressWarnings ({"ConstantConditions", "FieldRepeatedlyAccessedInMethod"})
@Override
@Declare
public void tick() {
int ticks = world.tickCount;
// Handle unload requests
final ConcurrentQueueSet<Long> unloadStage0 = this.unloadStage0;
if (!unloadStage0.isEmpty()) {
ImmutableSetMultimap<ChunkCoordIntPair, ForgeChunkManager.Ticket> persistentChunks = world.getPersistentChunks();
PlayerManager playerManager = world.getPlayerManager();
ChunkCoordIntPair chunkCoordIntPair = new ChunkCoordIntPair(0, 0);
Long key_;
int done = 0;
while (++done != 75 && (key_ = unloadStage0.take()) != null) {
long key = key_;
int x = (int) key;
int z = (int) (key >> 32);
chunkCoordIntPair.chunkXPos = x;
chunkCoordIntPair.chunkZPos = z;
Chunk chunk = chunks.getValueByKey(key);
if (chunk == null || chunk.partiallyUnloaded || !chunk.queuedUnload) {
continue;
}
if (persistentChunks.containsKey(chunkCoordIntPair) || unloadingChunks.containsItem(key) || playerManager.getOrCreateChunkWatcher(x, z, false) != null || !fireBukkitUnloadEvent(chunk)) {
chunk.queuedUnload = false;
continue;
}
if (lastChunk == chunk) {
lastChunk = null;
}
chunk.partiallyUnloaded = true;
chunk.onChunkUnload();
chunk.pendingBlockUpdates = world.getPendingBlockUpdates(chunk, false);
loadedChunks.remove(chunk);
chunks.remove(key);
synchronized (unloadingChunks) {
unloadingChunks.put(key, chunk);
unloadStage1.add(new QueuedUnload(key, ticks));
}
}
if (loader != null) {
loader.chunkTick();
}
}
handleUnloadQueue(ticks - 3);
if (ticks > 1200 && !world.multiverseWorld && world.provider.dimensionId != 0 && TickThreading.instance.allowWorldUnloading
&& loadedChunks.isEmpty() && ForgeChunkManager.getPersistentChunksFor(world).isEmpty()
&& (!TickThreading.instance.shouldLoadSpawn || !DimensionManager.shouldLoadSpawn(world.getDimension()))) {
DimensionManager.unloadWorld(world.getDimension());
}
if (ticks % TickThreading.instance.chunkGCInterval == 0) {
ChunkGarbageCollector.garbageCollect(world);
}
if (loadChunksInProvideChunkTicks++ == 200) {
loadChunksInProvideChunk = false;
int loaded = 0;
int possible = world.getPersistentChunks().size();
for (ChunkCoordIntPair chunkCoordIntPair : world.getPersistentChunks().keySet()) {
if (getChunkAt(chunkCoordIntPair.chunkXPos, chunkCoordIntPair.chunkZPos, doNothingRunnable) == null) {
loaded++;
}
possible++;
}
if (loaded > 0) {
Log.info("Loaded " + loaded + '/' + possible + " persistent chunks in " + Log.name(world));
}
}
}
private void handleUnloadQueue(long queueThreshold) {
handleUnloadQueue(queueThreshold, false);
}
private void handleUnloadQueue(long queueThreshold, boolean full) {
int done = 0;
// Handle unloading stage 1
{
QueuedUnload queuedUnload;
while ((queuedUnload = unloadStage1.peek()) != null && queuedUnload.ticks <= queueThreshold && (full || ++done <= 200)) {
long key = queuedUnload.key;
synchronized (unloadingChunks) {
if (!unloadStage1.remove(queuedUnload)) {
continue;
}
}
finalizeUnload(key);
}
}
}
private boolean finalizeUnload(long key) {
Chunk chunk;
synchronized (unloadingChunks) {
// Don't remove here, as the chunk should be in the unloadingChunks map so that getChunkIfExists will still return it if used by a mod during chunk saving.
chunk = unloadingChunks.getValueByKey(key);
}
if (chunk == null) {
return false;
}
if (!chunk.partiallyUnloaded) {
return false;
}
synchronized (chunk) {
if (chunk.alreadySavedAfterUnload) {
return false;
}
chunk.alreadySavedAfterUnload = true;
try {
boolean notInUnload = !inUnload.getAndSet(true);
boolean notWorldGen = !worldGenInProgress.getAndSet(true);
safeSaveChunk(chunk);
safeSaveExtraChunkData(chunk);
if (notWorldGen) {
worldGenInProgress.set(false);
}
if (notInUnload) {
inUnload.set(false);
}
if (chunks.containsItem(key)) {
Log.severe("Failed to unload chunk " + key + ", it was reloaded during unloading");
}
} finally {
if (unloadingChunks.remove(key) != chunk) {
Log.severe("While unloading " + chunk + " it was replaced/removed from the unloadingChunks map.");
}
}
}
return true;
}
// Public visibility as it will be accessed from net.minecraft.whatever, not actually this class
// (Inner classes are not remapped in patching)
public static class QueuedUnload {
public final int ticks;
public final long key;
public QueuedUnload(long key, int ticks) {
this.key = key;
this.ticks = ticks;
}
}
@Override
@Declare
public boolean safeToGenerate() {
return worldGenInProgress.get() == Boolean.FALSE && !Thread.holdsLock(generateLock);
}
@Override
public boolean chunkExists(int x, int z) {
long key = key(x, z);
return chunks.containsItem(key) || (worldGenInProgress.get() == Boolean.TRUE && (loadingChunks.containsItem(key) || (inUnload.get() == Boolean.TRUE && unloadingChunks.containsItem(key))));
}
@Override
@Declare
public boolean chunksExist(int minX, int minZ, int maxX, int maxZ) {
boolean worldGenInProgress = this.worldGenInProgress.get() == Boolean.TRUE;
boolean inUnload = worldGenInProgress && this.inUnload.get() == Boolean.TRUE;
for (int x = minX; x <= maxX; ++x) {
for (int z = minZ; z <= maxZ; ++z) {
long key = key(x, z);
if (!(chunks.containsItem(key) || (worldGenInProgress && (loadingChunks.containsItem(key) || (inUnload && unloadingChunks.containsItem(key)))))) {
return false;
}
}
}
return true;
}
@Override
@Declare
public boolean unloadChunk(int x, int z) {
if (world.getPersistentChunks().keySet().contains(new ChunkCoordIntPair(x, z))) {
return false;
}
long hash = key(x, z);
Chunk chunk = chunks.getValueByKey(hash);
if (chunk == null) {
return false;
}
chunk.queuedUnload = true;
return unloadStage0.add(hash);
}
@Override
@Declare
public void unloadChunkForce(long hash) {
if (unloadStage0.add(hash)) {
Chunk chunk = chunks.getValueByKey(hash);
if (chunk != null) {
chunk.queuedUnload = true;
}
}
}
@Override
public void unloadChunksIfNotNearSpawn(int x, int z) {
unloadChunk(x, z);
}
@Override
public void unloadAllChunks() {
if (loadedChunks.size() > world.getPersistentChunks().size()) {
synchronized (loadedChunks) {
for (Chunk chunk : loadedChunks) {
unloadStage0.add(key(chunk.xPosition, chunk.zPosition));
}
}
}
}
@Deprecated
public void unloadChunkImmediately(int x, int z, boolean save) {
long key = key(x, z);
finalizeUnload(key);
Chunk chunk = getChunkIfExists(x, z);
if (chunk == null || !fireBukkitUnloadEvent(chunk)) {
return;
}
synchronized (chunk) {
chunk.queuedUnload = true;
chunk.partiallyUnloaded = true;
chunk.onChunkUnload();
chunk.isChunkLoaded = false;
if (save || chunk.isModified) {
safeSaveChunk(chunk);
safeSaveExtraChunkData(chunk);
}
loadedChunks.remove(chunk);
chunks.remove(key);
}
}
@Deprecated
public Chunk regenerateChunk(int x, int z) {
unloadChunkImmediately(x, z, false);
return getChunkAtInternal(x, z, true, true);
}
@Override
@Declare
public void cacheChunk(int x, int z) {
if (!((AnvilChunkLoader) loader).isChunkCacheFull()) {
chunkLoadThreadPool.execute(new ChunkCacheRunnable(this, x, z));
}
}
@Override
@Declare
public void cacheChunkInternal(int x, int z) {
AnvilChunkLoader anvilChunkLoader = (AnvilChunkLoader) loader;
if (anvilChunkLoader.isChunkCacheFull()) {
return;
}
long key = key(x, z);
final AtomicInteger lock = getLock(key);
try {
synchronized (lock) {
if (chunks.containsItem(key) || loadingChunks.containsItem(key) || unloadingChunks.containsItem(key)) {
return;
}
anvilChunkLoader.cacheChunk(world, x, z);
}
} finally {
if (lock.decrementAndGet() == 0) {
loadingChunks.remove(key);
}
}
}
@Override
public final Chunk provideChunk(int x, int z) {
Chunk chunk = getChunkIfExists(x, z);
if (chunk != null) {
return chunk;
}
if (loadChunksInProvideChunk) {
return getChunkAtInternal(x, z, true, false);
}
/* else {
Log.warning("Didn't load a chunk at " + x + ',' + z, new Throwable());
} */
return defaultEmptyChunk;
}
@Override
public final Chunk loadChunk(int x, int z) {
Chunk chunk = getChunkAt(x, z, true, false, null);
chunk.queuedUnload = false;
return chunk;
}
@Override
@Declare
public final Chunk getChunkAt(final int x, final int z, final Runnable runnable) {
return getChunkAt(x, z, true, false, runnable);
}
@Override
@Declare
public final Chunk getChunkAt(final int x, final int z, boolean allowGenerate, final Runnable runnable) {
return getChunkAt(x, z, allowGenerate, false, runnable);
}
@Override
@Declare
public final Chunk getChunkIfExists(int x, int z) {
Chunk chunk = lastChunk;
if (chunk != null && chunk.xPosition == x && chunk.zPosition == z) {
return chunk;
}
long key = key(x, z);
chunk = chunks.getValueByKey(key);
if (chunk == null && worldGenInProgress.get() == Boolean.TRUE) {
chunk = loadingChunks.getValueByKey(key);
if (chunk == null && inUnload.get() == Boolean.TRUE) {
chunk = unloadingChunks.getValueByKey(key);
}
}
if (chunk == null) {
return null;
}
lastChunk = chunk;
return chunk;
}
@Override
@Declare
public final Chunk getChunkAt(final int x, final int z, boolean allowGenerate, boolean regenerate, final Runnable runnable) {
Chunk chunk = getChunkIfExists(x, z);
if (chunk != null) {
if (runnable != null) {
ThreadedChunkProvider.onChunkLoad(chunk, runnable);
}
return chunk;
}
if (runnable != null) {
if (world.unloaded) {
throw new IllegalStateException("Trying to load chunks in an unloaded world.");
}
chunkLoadThreadPool.execute(new ChunkLoadRunnable(x, z, allowGenerate, regenerate, runnable, this));
return null;
}
return getChunkAtInternal(x, z, allowGenerate, regenerate);
}
@SuppressWarnings ("ConstantConditions")
private Chunk getChunkAtInternal(final int x, final int z, boolean allowGenerate, boolean regenerate) {
if (!safeToGenerate()) {
return defaultEmptyChunk;
}
long key = key(x, z);
Chunk chunk;
final AtomicInteger lock = getLock(key);
boolean wasGenerated = false;
try {
boolean inLoadingMap = false;
// Lock on the lock for this chunk - prevent multiple instances of the same chunk
synchronized (lock) {
chunk = chunks.getValueByKey(key);
if (chunk != null) {
return chunk;
}
chunk = loadingChunks.getValueByKey(key);
if (chunk == null) {
finalizeUnload(key);
chunk = regenerate ? null : safeLoadChunk(x, z);
if (chunk != null && (chunk.xPosition != x || chunk.zPosition != z)) {
Log.severe("Chunk at " + chunk.xPosition + ',' + chunk.zPosition + " was stored at " + x + ',' + z + "\nResetting this chunk.");
chunk = null;
}
if (chunk == null) {
loadingChunks.put(key, defaultEmptyChunk);
if (!allowGenerate || generator == null) {
return defaultEmptyChunk;
}
} else {
loadingChunks.put(key, chunk);
inLoadingMap = true;
if (!world.loadEventFired) {
Log.warning("Loaded chunk before world load event fired, this can cause many issues, including loss of multiblock data.", new Throwable());
}
}
} else {
inLoadingMap = true;
}
}
// 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
boolean locked = true;
generateLock.lock();
try {
synchronized (lock) {
chunk = chunks.getValueByKey(key);
if (chunk != null) {
return chunk;
}
worldGenInProgress.set(Boolean.TRUE);
try {
chunk = loadingChunks.getValueByKey(key);
if (chunk == null) {
Log.severe("Failed to load chunk " + chunk + " at " + x + ',' + z + " as it is missing from the loading chunks map.");
return defaultEmptyChunk;
}
if (chunk == defaultEmptyChunk) {
try {
chunk = generator.provideChunk(x, z);
if (chunk == null) {
Log.severe("Null chunk was generated for " + x + ',' + z + " by " + generator);
return defaultEmptyChunk;
}
chunk.isTerrainPopulated = false;
wasGenerated = true;
} catch (Throwable t) {
Log.severe("Failed to generate a chunk in " + Log.name(world) + " at chunk coords " + x + ',' + z);
throw UnsafeUtil.throwIgnoreChecked(t);
}
} else {
if (generator != null) {
generator.recreateStructures(x, z);
}
}
if (!inLoadingMap) {
loadingChunks.put(key, chunk);
}
locked = false;
generateLock.unlock();
chunk.threadUnsafeChunkLoad();
chunks.put(key, chunk);
} finally {
worldGenInProgress.set(Boolean.FALSE);
}
}
} finally {
if (locked) {
generateLock.unlock();
}
}
} finally {
if (lock.decrementAndGet() == 0) {
loadingChunks.remove(key);
}
}
loadedChunks.add(chunk);
chunk.onChunkLoad();
fireBukkitLoadEvent(chunk, wasGenerated);
chunkLoadLocks.remove(key);
synchronized (generateLock) {
tryPopulateChunks(chunk);
}
return chunk;
}
private void populate(Chunk chunk) {
synchronized (chunk) {
int x = chunk.xPosition;
int z = chunk.zPosition;
if (chunk.isTerrainPopulated) {
Log.warning("Attempted to populate chunk " + x + ',' + z + " which is already populated.");
return;
}
if (generator != null) {
generator.populate(this, x, z);
fireBukkitPopulateEvent(chunk);
GameRegistry.generateWorld(x, z, world, generator, this);
chunk.setChunkModified(); // It may have been modified in generator.populate/GameRegistry.generateWorld.
}
//noinspection ConstantConditions
if (chunk.isTerrainPopulated) {
Log.warning("Chunk " + chunk + " had its isTerrainPopulated field set to true incorrectly by external code while populating");
}
chunk.isTerrainPopulated = true;
}
}
private void tryPopulateChunks(Chunk centerChunk) {
int cX = centerChunk.xPosition;
int cZ = centerChunk.zPosition;
for (int x = cX - populationRange; x <= cX + populationRange; x++) {
for (int z = cZ - populationRange; z <= cZ + populationRange; z++) {
Chunk chunk = getChunkFastUnsafe(x, z);
if (chunk != null && !chunk.queuedUnload && !chunk.partiallyUnloaded && !chunk.isTerrainPopulated && checkChunksExistLoadedNormally(x - populationRange, x + populationRange, z - populationRange, z + populationRange)) {
populate(chunk);
}
}
}
}
public boolean checkChunksExistLoadedNormally(int minX, int maxX, int minZ, int maxZ) {
for (int x = minX; x <= maxX; ++x) {
for (int z = minZ; z <= maxZ; ++z) {
Chunk chunk = getChunkFastUnsafe(x, z);
if (chunk == null || chunk.queuedUnload || chunk.partiallyUnloaded) {
return false;
}
}
}
return true;
}
@Override
@Declare
public Chunk getChunkFastUnsafe(int x, int z) {
return chunks.getValueByKey(key(x, z));
}
private AtomicInteger getLock(long key) {
AtomicInteger lock = chunkLoadLocks.get(key);
if (lock != null) {
lock.incrementAndGet();
return lock;
}
AtomicInteger newLock = new AtomicInteger(1);
lock = chunkLoadLocks.putIfAbsent(key, newLock);
if (lock != null) {
lock.incrementAndGet();
return lock;
}
return newLock;
}
@Override
@Declare
public void fireBukkitLoadEvent(Chunk chunk, boolean newlyGenerated) {
}
@Override
@Declare
public boolean fireBukkitUnloadEvent(Chunk chunk) {
return true;
}
@Override
protected Chunk safeLoadChunk(int x, int z) {
if (loader == null) {
return null;
}
try {
Chunk chunk = loader.loadChunk(world, x, z);
if (chunk != null) {
chunk.lastSaveTime = world.getTotalWorldTime();
}
return chunk;
} catch (Exception e) {
FMLLog.log(Level.SEVERE, e, "Failed to load chunk at " + x + ',' + z);
return null;
}
}
@Override
protected void safeSaveExtraChunkData(Chunk chunk) {
if (loader == null) {
return;
}
try {
loader.saveExtraChunkData(world, chunk);
} catch (Exception e) {
FMLLog.log(Level.SEVERE, e, "Failed to save extra chunk data for " + chunk);
}
}
@Override
protected void safeSaveChunk(Chunk chunk) {
if (loader == null) {
return;
}
try {
chunk.lastSaveTime = world.getTotalWorldTime();
loader.saveChunk(world, chunk);
} catch (Exception e) {
FMLLog.log(Level.SEVERE, e, "Failed to save chunk " + chunk);
}
}
@Deprecated
@Override
public void populate(IChunkProvider chunkProvider, int x, int z) {
throw new UnsupportedOperationException("Unused, inefficient parameter choice.");
}
@Override
@Declare
public void fireBukkitPopulateEvent(Chunk chunk) {
}
@Override
public boolean saveChunks(boolean fullSaveRequired, IProgressUpdate progressUpdate) {
boolean saveAll = fullSaveRequired;
if (saveTicks++ % 512 == 0) {
int loadedChunks = chunks.getNumHashElements();
if (loadedChunks > 1536) {
DeadLockDetector.tickAhead(5);
DeadLockDetector.sendChatSafely("Fully saving world " + world.getName() + " with " + loadedChunks + " chunks, expect a short lag spike.");
} else {
DeadLockDetector.tickAhead(1);
}
saveAll = true;
}
int savedChunks = 0;
long worldTime = world.getTotalWorldTime();
List<Chunk> chunksToSave = new ArrayList<Chunk>();
synchronized (loadedChunks) {
for (Chunk chunk : loadedChunks) {
if (!chunk.partiallyUnloaded && chunk.needsSaving(saveAll, worldTime)) {
if (++savedChunks == maxChunksToSave && !saveAll) {
if ((++overloadCount) > 50) {
Log.warning("Partial save queue overloaded in " + Log.name(world) + ". You should consider decreasing saveInterval. Only saved " + savedChunks + " out of " + chunksToSave.size());
maxChunksToSave = (maxChunksToSave * 3) / 2;
overloadCount -= 2;
}
break;
}
chunk.isModified = false;
chunksToSave.add(chunk);
}
}
}
for (Chunk chunk : chunksToSave) {
if (chunk.partiallyUnloaded) {
continue;
}
if (chunks.getValueByKey(key(chunk.xPosition, chunk.zPosition)) != chunk) {
if (MinecraftServer.getServer().isServerRunning()) {
Log.warning("Not saving " + chunk + ", not in correct location in chunks map.");
}
continue;
}
if (fullSaveRequired) {
safeSaveExtraChunkData(chunk);
}
safeSaveChunk(chunk);
}
lastChunksSaved = savedChunks;
if (overloadCount > 0 && savedChunks != maxChunksToSave) {
overloadCount--;
}
if (fullSaveRequired) {
handleUnloadQueue(Long.MAX_VALUE, true);
if (loader != null) {
loader.saveExtraData();
}
}
return true;
}
@Override
public boolean canSave() {
return !world.canNotSave;
}
@Override
public String makeString() {
return "Loaded " + loadedChunks.size() + " Loading " + loadingChunks.getNumHashElements() + " Unload " + unloadStage0.size() + " UnloadSave " + unloadStage1.size() + " Locks " + chunkLoadLocks.size() + " PartialSave " + lastChunksSaved + " Forced " + world.getPersistentChunks().size() + " Cached " + ((AnvilChunkLoader) loader).getCachedChunks();
}
@Override
public List getPossibleCreatures(EnumCreatureType creatureType, int x, int y, int z) {
return generator.getPossibleCreatures(creatureType, x, y, z);
}
@Override
public ChunkPosition findClosestStructure(World world, String name, int x, int y, int z) {
return generator.findClosestStructure(world, name, x, y, z);
}
@Override
public int getLoadedChunkCount() {
return loadedChunks.size();
}
@Override
public void recreateStructures(int x, int z) {
}
private static long key(int x, int z) {
return (((long) z) << 32) | (x & 0xffffffffL);
}
public static class ChunkLoadRunnable implements Runnable {
private final int x;
private final int z;
private final Runnable runnable;
private final ChunkProviderServer provider;
private final boolean allowGenerate;
private final boolean regenerate;
public ChunkLoadRunnable(int x, int z, boolean allowGenerate, boolean regenerate, Runnable runnable, ChunkProviderServer provider) {
this.x = x;
this.z = z;
this.allowGenerate = allowGenerate;
this.regenerate = regenerate;
this.runnable = runnable;
this.provider = provider;
}
@Override
public void run() {
try {
WorldServer worldServer = provider.getWorld();
if (worldServer.unloaded) {
FMLLog.warning("Failed to load chunk at " + worldServer.getDimension() + ':' + x + ',' + z + " asynchronously. The world is no longer loaded.");
return;
}
Chunk chunk = provider.getChunkAt(x, z, allowGenerate, regenerate, null);
if (chunk == null || (allowGenerate && chunk instanceof EmptyChunk)) {
FMLLog.warning("Failed to load chunk at " + Log.name(worldServer) + ':' + x + ',' + z + " asynchronously. Provided " + chunk);
} else {
ChunkProviderServer.onChunkLoad(chunk, runnable);
}
} catch (Throwable t) {
FMLLog.log(Level.SEVERE, t, "Exception loading chunk asynchronously.");
}
}
}
public static class ChunkCacheRunnable implements Runnable {
private final ChunkProviderServer chunkProviderServer;
private final int x;
private final int z;
public ChunkCacheRunnable(final ChunkProviderServer chunkProviderServer, final int x, final int z) {
this.chunkProviderServer = chunkProviderServer;
this.x = x;
this.z = z;
}
@Override
public void run() {
chunkProviderServer.cacheChunkInternal(x, z);
}
}
}