Skip to content

Commit f8db403

Browse files
committed
Fix sublevel chunk race cond with onUnloadChunk
1 parent 2401fad commit f8db403

4 files changed

Lines changed: 15 additions & 11 deletions

File tree

common/src/main/java/dev/ryanhcode/sable/SableCommonEvents.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,16 @@ public static void handleBlockChange(final ServerLevel level, final LevelChunk c
4545
if (plotChunk != null) {
4646
final LevelPlot plot = container.getPlot(chunkPos);
4747
final BlockPos blockPos = new BlockPos(x, y, z);
48+
final SubLevel subLevel = plot.getSubLevel();
49+
50+
if (subLevel.isRemoved()) {
51+
return;
52+
}
4853

4954
plotChunk.handleBlockChange(localX, y, localZ, oldState, newState);
5055
plot.updateBoundingBox();
5156
plot.expandIfNecessary(blockPos);
5257

53-
final SubLevel subLevel = plot.getSubLevel();
54-
5558
final WaterOcclusionContainer<?> waterOcclusionContainer = WaterOcclusionContainer.getContainer(level);
5659

5760
if (waterOcclusionContainer != null) {
@@ -78,10 +81,6 @@ public static void handleBlockChange(final ServerLevel level, final LevelChunk c
7881
heatMapManager.onSolidRemoved(blockPos);
7982
}
8083
}
81-
82-
if (subLevel.isRemoved()) {
83-
return;
84-
}
8584
}
8685

8786
final int idx = chunk.getSectionIndex(y);

common/src/main/java/dev/ryanhcode/sable/api/sublevel/SubLevelContainer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ public void removeSubLevel(final int x, final int z, final SubLevelRemovalReason
477477
throw new IllegalStateException("No sub-level at " + x + ", " + z);
478478
}
479479

480+
subLevel.markRemoved();
480481
this.observers.forEach(observer -> observer.onSubLevelRemoved(subLevel, reason));
481482
subLevel.onRemove();
482483

common/src/main/java/dev/ryanhcode/sable/physics/impl/rapier/RapierPhysicsPipeline.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ public void add(final ServerSubLevel subLevel, final Pose3dc pose) {
319319

320320
final int id = Rapier3D.getID(subLevel);
321321
Rapier3D.createSubLevel(this.sceneId, id, new double[]{pos.x(), pos.y(), pos.z(), rot.x(), rot.y(), rot.z(), rot.w()});
322+
this.activeSubLevels.put(id, subLevel);
322323

323324
subLevel.updateMergedMassData(1.0f);
324325
final Vector3dc centerOfMass = subLevel.getMassTracker().getCenterOfMass();
@@ -328,17 +329,17 @@ public void add(final ServerSubLevel subLevel, final Pose3dc pose) {
328329

329330
this.onStatsChanged(subLevel);
330331
}
331-
332-
this.activeSubLevels.put(Rapier3D.getID(subLevel), subLevel);
333332
}
334333

335334
/**
336335
* Removes a {@link SubLevel} from the physics pipeline.
337336
*/
338337
@Override
339338
public void remove(final ServerSubLevel subLevel) {
340-
Rapier3D.removeSubLevel(this.sceneId, Rapier3D.getID(subLevel));
341-
this.activeSubLevels.remove(Rapier3D.getID(subLevel));
339+
final int id = Rapier3D.getID(subLevel);
340+
if (this.activeSubLevels.remove(id) != null) {
341+
Rapier3D.removeSubLevel(this.sceneId, id);
342+
}
342343
}
343344

344345
/**
@@ -529,6 +530,10 @@ public void handleBlockChange(final SectionPos sectionPos, final LevelChunkSecti
529530

530531
@Override
531532
public void onStatsChanged(@NotNull final ServerSubLevel serverSubLevel) {
533+
if (serverSubLevel.isRemoved() || !this.activeSubLevels.containsKey(Rapier3D.getID(serverSubLevel))) {
534+
return;
535+
}
536+
532537
final BoundingBox3ic plotBounds = serverSubLevel.getPlot().getBoundingBox();
533538

534539
final int id = Rapier3D.getID(serverSubLevel);

common/src/main/java/dev/ryanhcode/sable/sublevel/system/SubLevelPhysicsSystem.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,6 @@ public void updateMassDataFromBlockChange(final SubLevel subLevel, final BlockPo
507507

508508
if (notifyPipeline) {
509509
serverSubLevel.updateMergedMassData((float) this.getPartialPhysicsTick());
510-
this.pipeline.onStatsChanged(serverSubLevel);
511510
}
512511
}
513512
}

0 commit comments

Comments
 (0)