Skip to content

Commit

Permalink
Fix NPE when rendering turtle's label
Browse files Browse the repository at this point in the history
Minecraft.hitResult may /technically/ be null when rendering a turtle.
In vanilla, this doesn't appear to happen, but other mods (e.g.
Immersive Portals) may still take advantage of this.

This hitResult is then propagated to BlockEntityRenderDispatcher, where
the field was /not/ marked as nullable. This meant we didn't even notice
the potential of an NPE!

Closes #1775
  • Loading branch information
SquidDev committed Apr 6, 2024
1 parent 8b2516a commit 825d45e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void render(TurtleBlockEntity turtle, float partialTicks, PoseStack trans
// Render the label
var label = turtle.getLabel();
var hit = renderer.cameraHitResult;
if (label != null && hit.getType() == HitResult.Type.BLOCK && turtle.getBlockPos().equals(((BlockHitResult) hit).getBlockPos())) {
if (label != null && hit != null && hit.getType() == HitResult.Type.BLOCK && turtle.getBlockPos().equals(((BlockHitResult) hit).getBlockPos())) {
var mc = Minecraft.getInstance();
var font = this.font;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package cc.tweaked.linter
import com.google.common.collect.ImmutableSet
import com.google.common.collect.ImmutableSetMultimap
import com.uber.nullaway.LibraryModels
import com.uber.nullaway.LibraryModels.FieldRef.fieldRef
import com.uber.nullaway.LibraryModels.MethodRef.methodRef

/**
Expand Down Expand Up @@ -34,4 +35,9 @@ class MinecraftLibraryModel : LibraryModels {
// Reasoning about nullability of BlockEntity.getLevel() is awkward. For now, assume it's non-null.
methodRef("net.minecraft.world.level.block.entity.BlockEntity", "getLevel()"),
)

override fun nullableFields(): ImmutableSet<LibraryModels.FieldRef> = ImmutableSet.of(
// This inherits from Minecraft.hitResult, and so can also be null.
fieldRef("net.minecraft.client.renderer.blockentity.BlockEntityRenderDispatcher", "cameraHitResult"),
)
}

0 comments on commit 825d45e

Please sign in to comment.