diff --git a/common/src/main/java/dev/ryanhcode/sable/SableCommonEvents.java b/common/src/main/java/dev/ryanhcode/sable/SableCommonEvents.java index cd57d9d9..909eb11f 100644 --- a/common/src/main/java/dev/ryanhcode/sable/SableCommonEvents.java +++ b/common/src/main/java/dev/ryanhcode/sable/SableCommonEvents.java @@ -45,13 +45,16 @@ public static void handleBlockChange(final ServerLevel level, final LevelChunk c if (plotChunk != null) { final LevelPlot plot = container.getPlot(chunkPos); final BlockPos blockPos = new BlockPos(x, y, z); + final SubLevel subLevel = plot.getSubLevel(); + + if (subLevel.isRemoved()) { + return; + } plotChunk.handleBlockChange(localX, y, localZ, oldState, newState); plot.updateBoundingBox(); plot.expandIfNecessary(blockPos); - final SubLevel subLevel = plot.getSubLevel(); - final WaterOcclusionContainer waterOcclusionContainer = WaterOcclusionContainer.getContainer(level); if (waterOcclusionContainer != null) { @@ -78,10 +81,6 @@ public static void handleBlockChange(final ServerLevel level, final LevelChunk c heatMapManager.onSolidRemoved(blockPos); } } - - if (subLevel.isRemoved()) { - return; - } } final int idx = chunk.getSectionIndex(y); diff --git a/common/src/main/java/dev/ryanhcode/sable/api/sublevel/SubLevelContainer.java b/common/src/main/java/dev/ryanhcode/sable/api/sublevel/SubLevelContainer.java index 70168e2e..3435c9c7 100644 --- a/common/src/main/java/dev/ryanhcode/sable/api/sublevel/SubLevelContainer.java +++ b/common/src/main/java/dev/ryanhcode/sable/api/sublevel/SubLevelContainer.java @@ -477,6 +477,7 @@ public void removeSubLevel(final int x, final int z, final SubLevelRemovalReason throw new IllegalStateException("No sub-level at " + x + ", " + z); } + subLevel.markRemoved(); this.observers.forEach(observer -> observer.onSubLevelRemoved(subLevel, reason)); subLevel.onRemove(); diff --git a/common/src/main/java/dev/ryanhcode/sable/physics/impl/rapier/RapierPhysicsPipeline.java b/common/src/main/java/dev/ryanhcode/sable/physics/impl/rapier/RapierPhysicsPipeline.java index 70a8ac36..4f468a7f 100644 --- a/common/src/main/java/dev/ryanhcode/sable/physics/impl/rapier/RapierPhysicsPipeline.java +++ b/common/src/main/java/dev/ryanhcode/sable/physics/impl/rapier/RapierPhysicsPipeline.java @@ -319,6 +319,7 @@ public void add(final ServerSubLevel subLevel, final Pose3dc pose) { final int id = Rapier3D.getID(subLevel); Rapier3D.createSubLevel(this.sceneId, id, new double[]{pos.x(), pos.y(), pos.z(), rot.x(), rot.y(), rot.z(), rot.w()}); + this.activeSubLevels.put(id, subLevel); subLevel.updateMergedMassData(1.0f); final Vector3dc centerOfMass = subLevel.getMassTracker().getCenterOfMass(); @@ -328,8 +329,6 @@ public void add(final ServerSubLevel subLevel, final Pose3dc pose) { this.onStatsChanged(subLevel); } - - this.activeSubLevels.put(Rapier3D.getID(subLevel), subLevel); } /** @@ -337,8 +336,10 @@ public void add(final ServerSubLevel subLevel, final Pose3dc pose) { */ @Override public void remove(final ServerSubLevel subLevel) { - Rapier3D.removeSubLevel(this.sceneId, Rapier3D.getID(subLevel)); - this.activeSubLevels.remove(Rapier3D.getID(subLevel)); + final int id = Rapier3D.getID(subLevel); + if (this.activeSubLevels.remove(id) != null) { + Rapier3D.removeSubLevel(this.sceneId, id); + } } /** @@ -529,6 +530,10 @@ public void handleBlockChange(final SectionPos sectionPos, final LevelChunkSecti @Override public void onStatsChanged(@NotNull final ServerSubLevel serverSubLevel) { + if (serverSubLevel.isRemoved() || !this.activeSubLevels.containsKey(Rapier3D.getID(serverSubLevel))) { + return; + } + final BoundingBox3ic plotBounds = serverSubLevel.getPlot().getBoundingBox(); final int id = Rapier3D.getID(serverSubLevel); diff --git a/common/src/main/java/dev/ryanhcode/sable/sublevel/system/SubLevelPhysicsSystem.java b/common/src/main/java/dev/ryanhcode/sable/sublevel/system/SubLevelPhysicsSystem.java index 33e57553..cdf8116c 100644 --- a/common/src/main/java/dev/ryanhcode/sable/sublevel/system/SubLevelPhysicsSystem.java +++ b/common/src/main/java/dev/ryanhcode/sable/sublevel/system/SubLevelPhysicsSystem.java @@ -507,7 +507,6 @@ public void updateMassDataFromBlockChange(final SubLevel subLevel, final BlockPo if (notifyPipeline) { serverSubLevel.updateMergedMassData((float) this.getPartialPhysicsTick()); - this.pipeline.onStatsChanged(serverSubLevel); } } } diff --git a/common/src/main/resources/natives/sable_rapier/sable_rapier_binaries.zip.l4z b/common/src/main/resources/natives/sable_rapier/sable_rapier_binaries.zip.l4z index 3d11cc17..6b403dd2 100644 Binary files a/common/src/main/resources/natives/sable_rapier/sable_rapier_binaries.zip.l4z and b/common/src/main/resources/natives/sable_rapier/sable_rapier_binaries.zip.l4z differ diff --git a/common/src/main/rust/rapier/src/lib.rs b/common/src/main/rust/rapier/src/lib.rs index 4f98c695..f056643e 100644 --- a/common/src/main/rust/rapier/src/lib.rs +++ b/common/src/main/rust/rapier/src/lib.rs @@ -517,6 +517,7 @@ pub extern "system" fn Java_dev_ryanhcode_sable_physics_impl_rapier_Rapier3D_ste } } + #[unsafe(no_mangle)] pub extern "system" fn Java_dev_ryanhcode_sable_physics_impl_rapier_Rapier3D_getPose<'local>( env: JNIEnv<'local>,