Fix NPE when rendering turtle's label

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
This commit is contained in:
Jonathan Coates 2024-04-06 08:44:03 +01:00
parent 8b2516abb5
commit 825d45eb26
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
2 changed files with 7 additions and 1 deletions

View File

@ -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;

View File

@ -7,6 +7,7 @@
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
/**
@ -34,4 +35,9 @@ override fun nonNullReturns(): ImmutableSet<LibraryModels.MethodRef> = Immutable
// 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"),
)
}