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
|
--- a/net/minecraft/server/level/ServerChunkCache.java
+++ b/net/minecraft/server/level/ServerChunkCache.java
@@ -7,10 +7,11 @@
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
+import java.util.Iterator;
import java.util.List;
+import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
-import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;
import java.util.function.BooleanSupplier;
import java.util.function.Consumer;
@@ -27,9 +28,9 @@
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.ai.village.poi.PoiManager;
import net.minecraft.world.level.ChunkPos;
+import net.minecraft.world.level.EnumSkyBlock;
import net.minecraft.world.level.GameRules;
import net.minecraft.world.level.Level;
-import net.minecraft.world.level.LightLayer;
import net.minecraft.world.level.LocalMobCapCalculator;
import net.minecraft.world.level.NaturalSpawner;
import net.minecraft.world.level.chunk.ChunkAccess;
@@ -47,6 +48,7 @@
import net.minecraft.world.level.storage.LevelStorageSource;
public class ServerChunkCache extends ChunkSource {
+
private static final List<ChunkStatus> CHUNK_STATUSES = ChunkStatus.getStatusList();
private final DistanceManager distanceManager;
final ServerLevel level;
@@ -56,8 +58,8 @@
public final ChunkMap chunkMap;
private final DimensionDataStorage dataStorage;
private long lastInhabitedUpdate;
- private boolean spawnEnemies = true;
- private boolean spawnFriendlies = true;
+ public boolean spawnEnemies = true;
+ public boolean spawnFriendlies = true;
private static final int CACHE_SIZE = 4;
private final long[] lastChunkPos = new long[4];
private final ChunkStatus[] lastChunkStatus = new ChunkStatus[4];
@@ -66,47 +68,31 @@
@VisibleForDebug
private NaturalSpawner.SpawnState lastSpawnState;
- public ServerChunkCache(
- ServerLevel level,
- LevelStorageSource.LevelStorageAccess levelStorageAccess,
- DataFixer fixerUpper,
- StructureTemplateManager structureManager,
- Executor dispatcher,
- ChunkGenerator generator,
- int viewDistance,
- int simulationDistance,
- boolean sync,
- ChunkProgressListener progressListener,
- ChunkStatusUpdateListener chunkStatusListener,
- Supplier<DimensionDataStorage> overworldDataStorage
- ) {
+ public ServerChunkCache(ServerLevel level, LevelStorageSource.LevelStorageAccess levelStorageAccess, DataFixer fixerUpper, StructureTemplateManager structureManager, Executor dispatcher, ChunkGenerator generator, int viewDistance, int simulationDistance, boolean sync, ChunkProgressListener progressListener, ChunkStatusUpdateListener chunkStatusListener, Supplier<DimensionDataStorage> overworldDataStorage) {
this.level = level;
this.mainThreadProcessor = new ServerChunkCache.MainThreadExecutor(level);
this.mainThread = Thread.currentThread();
File file = levelStorageAccess.getDimensionPath(level.dimension()).resolve("data").toFile();
+
file.mkdirs();
this.dataStorage = new DimensionDataStorage(file, fixerUpper);
- this.chunkMap = new ChunkMap(
- level,
- levelStorageAccess,
- fixerUpper,
- structureManager,
- dispatcher,
- this.mainThreadProcessor,
- this,
- generator,
- progressListener,
- chunkStatusListener,
- overworldDataStorage,
- viewDistance,
- sync
- );
+ this.chunkMap = new ChunkMap(level, levelStorageAccess, fixerUpper, structureManager, dispatcher, this.mainThreadProcessor, this, generator, progressListener, chunkStatusListener, overworldDataStorage, viewDistance, sync);
this.lightEngine = this.chunkMap.getLightEngine();
this.distanceManager = this.chunkMap.getDistanceManager();
this.distanceManager.updateSimulationDistance(simulationDistance);
this.clearCache();
}
+ // CraftBukkit start - properly implement isChunkLoaded
+ public boolean isChunkLoaded(int chunkX, int chunkZ) {
+ ChunkHolder chunk = this.chunkMap.getUpdatingChunkIfPresent(ChunkPos.asLong(chunkX, chunkZ));
+ if (chunk == null) {
+ return false;
+ }
+ return chunk.getFullChunkNow() != null;
+ }
+ // CraftBukkit end
+
@Override
public ThreadedLevelLightEngine getLightEngine() {
return this.lightEngine;
@@ -121,51 +107,59 @@
return this.chunkMap.getTickingGenerated();
}
- private void storeInCache(long chunkPos, ChunkAccess chunk, ChunkStatus chunkStatus) {
- for (int i = 3; i > 0; i--) {
- this.lastChunkPos[i] = this.lastChunkPos[i - 1];
- this.lastChunkStatus[i] = this.lastChunkStatus[i - 1];
- this.lastChunk[i] = this.lastChunk[i - 1];
+ private void storeInCache(long chunkPos, ChunkAccess ichunkaccess, ChunkStatus chunk) {
+ for (int j = 3; j > 0; --j) {
+ this.lastChunkPos[j] = this.lastChunkPos[j - 1];
+ this.lastChunkStatus[j] = this.lastChunkStatus[j - 1];
+ this.lastChunk[j] = this.lastChunk[j - 1];
}
this.lastChunkPos[0] = chunkPos;
- this.lastChunkStatus[0] = chunkStatus;
- this.lastChunk[0] = chunk;
+ this.lastChunkStatus[0] = chunk;
+ this.lastChunk[0] = ichunkaccess;
}
@Nullable
@Override
public ChunkAccess getChunk(int chunkX, int chunkZ, ChunkStatus requiredStatus, boolean load) {
if (Thread.currentThread() != this.mainThread) {
- return CompletableFuture.<ChunkAccess>supplyAsync(() -> this.getChunk(chunkX, chunkZ, requiredStatus, load), this.mainThreadProcessor).join();
+ return (ChunkAccess) CompletableFuture.supplyAsync(() -> {
+ return this.getChunk(chunkX, chunkZ, requiredStatus, load);
+ }, this.mainThreadProcessor).join();
} else {
- ProfilerFiller profiler = this.level.getProfiler();
- profiler.incrementCounter("getChunk");
- long _long = ChunkPos.asLong(chunkX, chunkZ);
+ ProfilerFiller gameprofilerfiller = this.level.getProfiler();
- for (int i = 0; i < 4; i++) {
- if (_long == this.lastChunkPos[i] && requiredStatus == this.lastChunkStatus[i]) {
- ChunkAccess chunkAccess = this.lastChunk[i];
- if (chunkAccess != null || !load) {
- return chunkAccess;
+ gameprofilerfiller.incrementCounter("getChunk");
+ long k = ChunkPos.asLong(chunkX, chunkZ);
+
+ ChunkAccess ichunkaccess;
+
+ for (int l = 0; l < 4; ++l) {
+ if (k == this.lastChunkPos[l] && requiredStatus == this.lastChunkStatus[l]) {
+ ichunkaccess = this.lastChunk[l];
+ if (ichunkaccess != null) { // CraftBukkit - the chunk can become accessible in the meantime TODO for non-null chunks it might also make sense to check that the chunk's state hasn't changed in the meantime
+ return ichunkaccess;
}
}
}
- profiler.incrementCounter("getChunkCacheMiss");
- CompletableFuture<Either<ChunkAccess, ChunkHolder.ChunkLoadingFailure>> chunkFutureMainThread = this.getChunkFutureMainThread(
- chunkX, chunkZ, requiredStatus, load
- );
- this.mainThreadProcessor.managedBlock(chunkFutureMainThread::isDone);
- ChunkAccess chunkAccess = chunkFutureMainThread.join().map(chunkAccess1 -> (ChunkAccess)chunkAccess1, chunkLoadingFailure -> {
+ gameprofilerfiller.incrementCounter("getChunkCacheMiss");
+ CompletableFuture<Either<ChunkAccess, ChunkHolder.Failure>> completablefuture = this.getChunkFutureMainThread(chunkX, chunkZ, requiredStatus, load);
+ ServerChunkCache.MainThreadExecutor chunkproviderserver_b = this.mainThreadProcessor;
+
+ Objects.requireNonNull(completablefuture);
+ chunkproviderserver_b.managedBlock(completablefuture::isDone);
+ ichunkaccess = (ChunkAccess) ((Either) completablefuture.join()).map((ichunkaccess1) -> {
+ return ichunkaccess1;
+ }, (playerchunk_failure) -> {
if (load) {
- throw (IllegalStateException)Util.pauseInIde(new IllegalStateException("Chunk not there when requested: " + chunkLoadingFailure));
+ throw (IllegalStateException) Util.pauseInIde(new IllegalStateException("Chunk not there when requested: " + playerchunk_failure));
} else {
return null;
}
});
- this.storeInCache(_long, chunkAccess, requiredStatus);
- return chunkAccess;
+ this.storeInCache(k, ichunkaccess, requiredStatus);
+ return ichunkaccess;
}
}
@@ -176,28 +170,32 @@
return null;
} else {
this.level.getProfiler().incrementCounter("getChunkNow");
- long _long = ChunkPos.asLong(chunkX, chunkZ);
+ long k = ChunkPos.asLong(chunkX, chunkZ);
- for (int i = 0; i < 4; i++) {
- if (_long == this.lastChunkPos[i] && this.lastChunkStatus[i] == ChunkStatus.FULL) {
- ChunkAccess chunkAccess = this.lastChunk[i];
- return chunkAccess instanceof LevelChunk ? (LevelChunk)chunkAccess : null;
+ for (int l = 0; l < 4; ++l) {
+ if (k == this.lastChunkPos[l] && this.lastChunkStatus[l] == ChunkStatus.FULL) {
+ ChunkAccess ichunkaccess = this.lastChunk[l];
+
+ return ichunkaccess instanceof LevelChunk ? (LevelChunk) ichunkaccess : null;
}
}
- ChunkHolder visibleChunkIfPresent = this.getVisibleChunkIfPresent(_long);
- if (visibleChunkIfPresent == null) {
+ ChunkHolder playerchunk = this.getVisibleChunkIfPresent(k);
+
+ if (playerchunk == null) {
return null;
} else {
- Either<ChunkAccess, ChunkHolder.ChunkLoadingFailure> either = visibleChunkIfPresent.getFutureIfPresent(ChunkStatus.FULL).getNow(null);
+ Either<ChunkAccess, ChunkHolder.Failure> either = (Either) playerchunk.getFutureIfPresent(ChunkStatus.FULL).getNow(null); // CraftBukkit - decompile error
+
if (either == null) {
return null;
} else {
- ChunkAccess chunkAccess1 = either.left().orElse(null);
- if (chunkAccess1 != null) {
- this.storeInCache(_long, chunkAccess1, ChunkStatus.FULL);
- if (chunkAccess1 instanceof LevelChunk) {
- return (LevelChunk)chunkAccess1;
+ ChunkAccess ichunkaccess1 = (ChunkAccess) either.left().orElse(null); // CraftBukkit - decompile error
+
+ if (ichunkaccess1 != null) {
+ this.storeInCache(k, ichunkaccess1, ChunkStatus.FULL);
+ if (ichunkaccess1 instanceof LevelChunk) {
+ return (LevelChunk) ichunkaccess1;
}
}
@@ -209,85 +207,99 @@
private void clearCache() {
Arrays.fill(this.lastChunkPos, ChunkPos.INVALID_CHUNK_POS);
- Arrays.fill(this.lastChunkStatus, null);
- Arrays.fill(this.lastChunk, null);
+ Arrays.fill(this.lastChunkStatus, (Object) null);
+ Arrays.fill(this.lastChunk, (Object) null);
}
- public CompletableFuture<Either<ChunkAccess, ChunkHolder.ChunkLoadingFailure>> getChunkFuture(int x, int y, ChunkStatus chunkStatus, boolean load) {
- boolean flag = Thread.currentThread() == this.mainThread;
- CompletableFuture<Either<ChunkAccess, ChunkHolder.ChunkLoadingFailure>> chunkFutureMainThread;
- if (flag) {
- chunkFutureMainThread = this.getChunkFutureMainThread(x, y, chunkStatus, load);
- this.mainThreadProcessor.managedBlock(chunkFutureMainThread::isDone);
+ public CompletableFuture<Either<ChunkAccess, ChunkHolder.Failure>> getChunkFuture(int x, int y, ChunkStatus chunkStatus, boolean load) {
+ boolean flag1 = Thread.currentThread() == this.mainThread;
+ CompletableFuture completablefuture;
+
+ if (flag1) {
+ completablefuture = this.getChunkFutureMainThread(x, y, chunkStatus, load);
+ ServerChunkCache.MainThreadExecutor chunkproviderserver_b = this.mainThreadProcessor;
+
+ Objects.requireNonNull(completablefuture);
+ chunkproviderserver_b.managedBlock(completablefuture::isDone);
} else {
- chunkFutureMainThread = CompletableFuture.<CompletableFuture<Either<ChunkAccess, ChunkHolder.ChunkLoadingFailure>>>supplyAsync(
- () -> this.getChunkFutureMainThread(x, y, chunkStatus, load), this.mainThreadProcessor
- )
- .thenCompose(completableFuture -> (CompletionStage<Either<ChunkAccess, ChunkHolder.ChunkLoadingFailure>>)completableFuture);
+ completablefuture = CompletableFuture.supplyAsync(() -> {
+ return this.getChunkFutureMainThread(x, y, chunkStatus, load);
+ }, this.mainThreadProcessor).thenCompose((completablefuture1) -> {
+ return completablefuture1;
+ });
}
- return chunkFutureMainThread;
+ return completablefuture;
}
- private CompletableFuture<Either<ChunkAccess, ChunkHolder.ChunkLoadingFailure>> getChunkFutureMainThread(
- int x, int y, ChunkStatus chunkStatus, boolean load
- ) {
- ChunkPos chunkPos = new ChunkPos(x, y);
- long l = chunkPos.toLong();
- int i = ChunkLevel.byStatus(chunkStatus);
- ChunkHolder visibleChunkIfPresent = this.getVisibleChunkIfPresent(l);
- if (load) {
- this.distanceManager.addTicket(TicketType.UNKNOWN, chunkPos, i, chunkPos);
- if (this.chunkAbsent(visibleChunkIfPresent, i)) {
- ProfilerFiller profiler = this.level.getProfiler();
- profiler.push("chunkLoad");
+ private CompletableFuture<Either<ChunkAccess, ChunkHolder.Failure>> getChunkFutureMainThread(int x, int y, ChunkStatus chunkStatus, boolean load) {
+ ChunkPos chunkcoordintpair = new ChunkPos(x, y);
+ long k = chunkcoordintpair.toLong();
+ int l = ChunkLevel.byStatus(chunkStatus);
+ ChunkHolder playerchunk = this.getVisibleChunkIfPresent(k);
+
+ // CraftBukkit start - don't add new ticket for currently unloading chunk
+ boolean currentlyUnloading = false;
+ if (playerchunk != null) {
+ FullChunkStatus oldChunkState = ChunkLevel.fullStatus(playerchunk.oldTicketLevel);
+ FullChunkStatus currentChunkState = ChunkLevel.fullStatus(playerchunk.getTicketLevel());
+ currentlyUnloading = (oldChunkState.isOrAfter(FullChunkStatus.FULL) && !currentChunkState.isOrAfter(FullChunkStatus.FULL));
+ }
+ if (load && !currentlyUnloading) {
+ // CraftBukkit end
+ this.distanceManager.addTicket(TicketType.UNKNOWN, chunkcoordintpair, l, chunkcoordintpair);
+ if (this.chunkAbsent(playerchunk, l)) {
+ ProfilerFiller gameprofilerfiller = this.level.getProfiler();
+
+ gameprofilerfiller.push("chunkLoad");
this.runDistanceManagerUpdates();
- visibleChunkIfPresent = this.getVisibleChunkIfPresent(l);
- profiler.pop();
- if (this.chunkAbsent(visibleChunkIfPresent, i)) {
- throw (IllegalStateException)Util.pauseInIde(new IllegalStateException("No chunk holder after ticket has been added"));
+ playerchunk = this.getVisibleChunkIfPresent(k);
+ gameprofilerfiller.pop();
+ if (this.chunkAbsent(playerchunk, l)) {
+ throw (IllegalStateException) Util.pauseInIde(new IllegalStateException("No chunk holder after ticket has been added"));
}
}
}
- return this.chunkAbsent(visibleChunkIfPresent, i)
- ? ChunkHolder.UNLOADED_CHUNK_FUTURE
- : visibleChunkIfPresent.getOrScheduleFuture(chunkStatus, this.chunkMap);
+ return this.chunkAbsent(playerchunk, l) ? ChunkHolder.UNLOADED_CHUNK_FUTURE : playerchunk.getOrScheduleFuture(chunkStatus, this.chunkMap);
}
private boolean chunkAbsent(@Nullable ChunkHolder chunkHolder, int status) {
- return chunkHolder == null || chunkHolder.getTicketLevel() > status;
+ return chunkHolder == null || chunkHolder.oldTicketLevel > status; // CraftBukkit using oldTicketLevel for isLoaded checks
}
@Override
public boolean hasChunk(int x, int z) {
- ChunkHolder visibleChunkIfPresent = this.getVisibleChunkIfPresent(new ChunkPos(x, z).toLong());
- int i = ChunkLevel.byStatus(ChunkStatus.FULL);
- return !this.chunkAbsent(visibleChunkIfPresent, i);
+ ChunkHolder playerchunk = this.getVisibleChunkIfPresent((new ChunkPos(x, z)).toLong());
+ int k = ChunkLevel.byStatus(ChunkStatus.FULL);
+
+ return !this.chunkAbsent(playerchunk, k);
}
@Nullable
@Override
public LightChunk getChunkForLighting(int chunkX, int chunkZ) {
- long _long = ChunkPos.asLong(chunkX, chunkZ);
- ChunkHolder visibleChunkIfPresent = this.getVisibleChunkIfPresent(_long);
- if (visibleChunkIfPresent == null) {
+ long k = ChunkPos.asLong(chunkX, chunkZ);
+ ChunkHolder playerchunk = this.getVisibleChunkIfPresent(k);
+
+ if (playerchunk == null) {
return null;
} else {
- int i = CHUNK_STATUSES.size() - 1;
+ int l = ServerChunkCache.CHUNK_STATUSES.size() - 1;
while (true) {
- ChunkStatus chunkStatus = CHUNK_STATUSES.get(i);
- Optional<ChunkAccess> optional = visibleChunkIfPresent.getFutureIfPresentUnchecked(chunkStatus).getNow(ChunkHolder.UNLOADED_CHUNK).left();
+ ChunkStatus chunkstatus = (ChunkStatus) ServerChunkCache.CHUNK_STATUSES.get(l);
+ Optional<ChunkAccess> optional = ((Either) playerchunk.getFutureIfPresentUnchecked(chunkstatus).getNow(ChunkHolder.UNLOADED_CHUNK)).left();
+
if (optional.isPresent()) {
- return optional.get();
+ return (LightChunk) optional.get();
}
- if (chunkStatus == ChunkStatus.INITIALIZE_LIGHT.getParent()) {
+ if (chunkstatus == ChunkStatus.INITIALIZE_LIGHT.getParent()) {
return null;
}
- i--;
+ --l;
}
}
}
@@ -304,6 +316,7 @@
boolean runDistanceManagerUpdates() {
boolean flag = this.distanceManager.runAllUpdates(this.chunkMap);
boolean flag1 = this.chunkMap.promoteChunkMap();
+
if (!flag && !flag1) {
return false;
} else {
@@ -313,13 +326,15 @@
}
public boolean isPositionTicking(long chunkPos) {
- ChunkHolder visibleChunkIfPresent = this.getVisibleChunkIfPresent(chunkPos);
- if (visibleChunkIfPresent == null) {
+ ChunkHolder playerchunk = this.getVisibleChunkIfPresent(chunkPos);
+
+ if (playerchunk == null) {
return false;
} else if (!this.level.shouldTickBlocksAt(chunkPos)) {
return false;
} else {
- Either<LevelChunk, ChunkHolder.ChunkLoadingFailure> either = visibleChunkIfPresent.getTickingChunkFuture().getNow(null);
+ Either<LevelChunk, ChunkHolder.Failure> either = (Either) playerchunk.getTickingChunkFuture().getNow(null); // CraftBukkit - decompile error
+
return either != null && either.left().isPresent();
}
}
@@ -331,11 +346,31 @@
@Override
public void close() throws IOException {
- this.save(true);
+ // CraftBukkit start
+ close(true);
+ }
+
+ public void close(boolean save) throws IOException {
+ if (save) {
+ this.save(true);
+ }
+ // CraftBukkit end
this.lightEngine.close();
this.chunkMap.close();
}
+ // CraftBukkit start - modelled on below
+ public void purgeUnload() {
+ this.level.getProfiler().push("purge");
+ this.distanceManager.purgeStaleTickets();
+ this.runDistanceManagerUpdates();
+ this.level.getProfiler().popPush("unload");
+ this.chunkMap.tick(() -> true);
+ this.level.getProfiler().pop();
+ this.clearCache();
+ }
+ // CraftBukkit end
+
@Override
public void tick(BooleanSupplier hasTimeLeft, boolean tickChunks) {
this.level.getProfiler().push("purge");
@@ -354,68 +389,80 @@
}
private void tickChunks() {
- long gameTime = this.level.getGameTime();
- long l = gameTime - this.lastInhabitedUpdate;
- this.lastInhabitedUpdate = gameTime;
+ long i = this.level.getGameTime();
+ long j = i - this.lastInhabitedUpdate;
+
+ this.lastInhabitedUpdate = i;
if (!this.level.isDebug()) {
- ProfilerFiller profiler = this.level.getProfiler();
- profiler.push("pollingChunks");
- profiler.push("filteringLoadedChunks");
- List<ServerChunkCache.ChunkAndHolder> list = Lists.newArrayListWithCapacity(this.chunkMap.size());
+ ProfilerFiller gameprofilerfiller = this.level.getProfiler();
- for (ChunkHolder chunkHolder : this.chunkMap.getChunks()) {
- LevelChunk tickingChunk = chunkHolder.getTickingChunk();
- if (tickingChunk != null) {
- list.add(new ServerChunkCache.ChunkAndHolder(tickingChunk, chunkHolder));
+ gameprofilerfiller.push("pollingChunks");
+ gameprofilerfiller.push("filteringLoadedChunks");
+ List<ServerChunkCache.a> list = Lists.newArrayListWithCapacity(this.chunkMap.size());
+ Iterator iterator = this.chunkMap.getChunks().iterator();
+
+ while (iterator.hasNext()) {
+ ChunkHolder playerchunk = (ChunkHolder) iterator.next();
+ LevelChunk chunk = playerchunk.getTickingChunk();
+
+ if (chunk != null) {
+ list.add(new ServerChunkCache.a(chunk, playerchunk));
}
}
if (this.level.getServer().tickRateManager().runsNormally()) {
- profiler.popPush("naturalSpawnCount");
- int naturalSpawnChunkCount = this.distanceManager.getNaturalSpawnChunkCount();
- NaturalSpawner.SpawnState spawnState = NaturalSpawner.createState(
- naturalSpawnChunkCount, this.level.getAllEntities(), this::getFullChunk, new LocalMobCapCalculator(this.chunkMap)
- );
- this.lastSpawnState = spawnState;
- profiler.popPush("spawnAndTick");
- boolean _boolean = this.level.getGameRules().getBoolean(GameRules.RULE_DOMOBSPAWNING);
+ gameprofilerfiller.popPush("naturalSpawnCount");
+ int k = this.distanceManager.getNaturalSpawnChunkCount();
+ NaturalSpawner.SpawnState spawnercreature_d = NaturalSpawner.createState(k, this.level.getAllEntities(), this::getFullChunk, new LocalMobCapCalculator(this.chunkMap));
+
+ this.lastSpawnState = spawnercreature_d;
+ gameprofilerfiller.popPush("spawnAndTick");
+ boolean flag = this.level.getGameRules().getBoolean(GameRules.RULE_DOMOBSPAWNING) && !this.level.players().isEmpty(); // CraftBukkit
+
Util.shuffle(list, this.level.random);
- int _int = this.level.getGameRules().getInt(GameRules.RULE_RANDOMTICKING);
- boolean flag = this.level.getLevelData().getGameTime() % 400L == 0L;
+ int l = this.level.getGameRules().getInt(GameRules.RULE_RANDOMTICKING);
+ boolean flag1 = this.level.ticksPerSpawnCategory.getLong(org.bukkit.entity.SpawnCategory.ANIMAL) != 0L && this.level.getLevelData().getGameTime() % this.level.ticksPerSpawnCategory.getLong(org.bukkit.entity.SpawnCategory.ANIMAL) == 0L; // CraftBukkit
+ Iterator iterator1 = list.iterator();
- for (ServerChunkCache.ChunkAndHolder chunkAndHolder : list) {
- LevelChunk levelChunk = chunkAndHolder.chunk;
- ChunkPos pos = levelChunk.getPos();
- if (this.level.isNaturalSpawningAllowed(pos) && this.chunkMap.anyPlayerCloseEnoughForSpawning(pos)) {
- levelChunk.incrementInhabitedTime(l);
- if (_boolean && (this.spawnEnemies || this.spawnFriendlies) && this.level.getWorldBorder().isWithinBounds(pos)) {
- NaturalSpawner.spawnForChunk(this.level, levelChunk, spawnState, this.spawnFriendlies, this.spawnEnemies, flag);
+ while (iterator1.hasNext()) {
+ ServerChunkCache.a chunkproviderserver_a = (ServerChunkCache.a) iterator1.next();
+ LevelChunk chunk1 = chunkproviderserver_a.chunk;
+ ChunkPos chunkcoordintpair = chunk1.getPos();
+
+ if (this.level.isNaturalSpawningAllowed(chunkcoordintpair) && this.chunkMap.anyPlayerCloseEnoughForSpawning(chunkcoordintpair)) {
+ chunk1.incrementInhabitedTime(j);
+ if (flag && (this.spawnEnemies || this.spawnFriendlies) && this.level.getWorldBorder().isWithinBounds(chunkcoordintpair)) {
+ NaturalSpawner.spawnForChunk(this.level, chunk1, spawnercreature_d, this.spawnFriendlies, this.spawnEnemies, flag1);
}
- if (this.level.shouldTickBlocksAt(pos.toLong())) {
- this.level.tickChunk(levelChunk, _int);
+ if (this.level.shouldTickBlocksAt(chunkcoordintpair.toLong())) {
+ this.level.tickChunk(chunk1, l);
}
}
}
- profiler.popPush("customSpawners");
- if (_boolean) {
+ gameprofilerfiller.popPush("customSpawners");
+ if (flag) {
this.level.tickCustomSpawners(this.spawnEnemies, this.spawnFriendlies);
}
}
- profiler.popPush("broadcast");
- list.forEach(chunkAndHolder1 -> chunkAndHolder1.holder.broadcastChanges(chunkAndHolder1.chunk));
- profiler.pop();
- profiler.pop();
+ gameprofilerfiller.popPush("broadcast");
+ list.forEach((chunkproviderserver_a1) -> {
+ chunkproviderserver_a1.holder.broadcastChanges(chunkproviderserver_a1.chunk);
+ });
+ gameprofilerfiller.pop();
+ gameprofilerfiller.pop();
}
}
- private void getFullChunk(long chunkPos, Consumer<LevelChunk> fullChunkGetter) {
- ChunkHolder visibleChunkIfPresent = this.getVisibleChunkIfPresent(chunkPos);
- if (visibleChunkIfPresent != null) {
- visibleChunkIfPresent.getFullChunkFuture().getNow(ChunkHolder.UNLOADED_LEVEL_CHUNK).left().ifPresent(fullChunkGetter);
+ private void getFullChunk(long chunkPos, Consumer<LevelChunk> consumer) {
+ ChunkHolder playerchunk = this.getVisibleChunkIfPresent(chunkPos);
+
+ if (playerchunk != null) {
+ ((Either) playerchunk.getFullChunkFuture().getNow(ChunkHolder.UNLOADED_LEVEL_CHUNK)).left().ifPresent(consumer);
}
+
}
@Override
@@ -447,20 +494,24 @@
public void blockChanged(BlockPos pos) {
int i = SectionPos.blockToSectionCoord(pos.getX());
- int i1 = SectionPos.blockToSectionCoord(pos.getZ());
- ChunkHolder visibleChunkIfPresent = this.getVisibleChunkIfPresent(ChunkPos.asLong(i, i1));
- if (visibleChunkIfPresent != null) {
- visibleChunkIfPresent.blockChanged(pos);
+ int j = SectionPos.blockToSectionCoord(pos.getZ());
+ ChunkHolder playerchunk = this.getVisibleChunkIfPresent(ChunkPos.asLong(i, j));
+
+ if (playerchunk != null) {
+ playerchunk.blockChanged(pos);
}
+
}
@Override
- public void onLightUpdate(LightLayer type, SectionPos pos) {
+ public void onLightUpdate(EnumSkyBlock type, SectionPos pos) {
this.mainThreadProcessor.execute(() -> {
- ChunkHolder visibleChunkIfPresent = this.getVisibleChunkIfPresent(pos.chunk().toLong());
- if (visibleChunkIfPresent != null) {
- visibleChunkIfPresent.sectionLightChanged(type, pos.y());
+ ChunkHolder playerchunk = this.getVisibleChunkIfPresent(pos.chunk().toLong());
+
+ if (playerchunk != null) {
+ playerchunk.sectionLightChanged(type, pos.y());
}
+
});
}
@@ -481,6 +532,7 @@
if (!player.isRemoved()) {
this.chunkMap.move(player);
}
+
}
public void removeEntity(Entity entity) {
@@ -539,12 +591,10 @@
this.distanceManager.removeTicketsOnClosing();
}
- static record ChunkAndHolder(LevelChunk chunk, ChunkHolder holder) {
- }
+ private final class MainThreadExecutor extends BlockableEventLoop<Runnable> {
- final class MainThreadExecutor extends BlockableEventLoop<Runnable> {
- MainThreadExecutor(Level level) {
- super("Chunk source main thread executor for " + level.dimension().location());
+ MainThreadExecutor(Level world) {
+ super("Chunk source main thread executor for " + world.dimension().location());
}
@Override
@@ -574,13 +624,23 @@
}
@Override
+ // CraftBukkit start - process pending Chunk loadCallback() and unloadCallback() after each run task
public boolean pollTask() {
+ try {
if (ServerChunkCache.this.runDistanceManagerUpdates()) {
return true;
} else {
ServerChunkCache.this.lightEngine.tryScheduleUpdate();
return super.pollTask();
}
+ } finally {
+ chunkMap.callbackExecutor.run();
}
+ // CraftBukkit end
+ }
}
+
+ private static record a(LevelChunk chunk, ChunkHolder holder) {
+
+ }
}
|