Prevent NPE for weightless contraptions#965
Open
lap2ka wants to merge 2 commits into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR targets a recurring server crash in the Create compatibility layer where MassTracker.getCenterOfMass() can be null for certain “weightless” / non-solid contraptions, leading to a NullPointerException during contraption initialization.
Changes:
- Adds a null-check before translating floating block cluster origins using the contraption center of mass.
- Adds a null-check before adding the contraption to the physics pipeline.
Comments suppressed due to low confidence (1)
neoforge/src/main/java/dev/ryanhcode/sable/neoforge/mixin/compatibility/create/contraptions/AbstractContraptionEntityMixin.java:175
- This null-guard prevents the immediate NPE, but leaves sable$massTracker in a state where getCenterOfMass() is null. Several call sites assume a non-null center of mass (e.g., KinematicContraption.sable$getLocalPose() and this mixin's sable$getPosition()), so if this contraption is added to a plot or the physics pipeline later it can still crash. Consider treating a null centerOfMass as an invalid MassTracker and explicitly skipping/removing the contraption from any physics/plot collections (or providing a safe fallback center of mass) to keep invariants consistent.
assert this.sable$localBounds != null;
this.sable$massTracker = MassTracker.build(this.sable$blockGetter(), this.sable$localBounds);
final Vector3dc centerOfMass = this.sable$massTracker.getCenterOfMass();
if (centerOfMass != null) {
final Vector3d temp = centerOfMass.negate(new Vector3d()).add(0.5, 0.5, 0.5);
for (final FloatingBlockCluster cluster : this.sable$floatingClusterContainer.clusters) {
cluster.getBlockData().translateOrigin(temp);
}
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
99
to
106
| if (!this.sable$added && this.level() instanceof final ServerLevel serverLevel) { | ||
| this.sable$buildProperties(); | ||
| this.sable$addToPlot(); | ||
| this.sable$addToPipeline(serverLevel); | ||
| if (this.sable$massTracker.getCenterOfMass() != null) { | ||
| this.sable$addToPipeline(serverLevel); | ||
| } | ||
| this.sable$added = true; | ||
| } |
Contributor
Author
There was a problem hiding this comment.
shi the AI overlord is right
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Fix #457, fix #478, fix #851, fix #882, fix #906, fix #909
lotta duplicates here